Xy·Z 2019-12-05 08:56
浏览 136

在TODO: Add the code here处添加一怎样的段代码,使其变成一个完整的生成哈夫曼树的代码

#include "HuffmanTree.h"
#include

int main(int argc, char* argv[])
{
int i;

//
// Initializing array
//
int WeightArray[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41};
int Count = sizeof(WeightArray) / sizeof(WeightArray[0]);
int HuffmanLength = 2 * Count - 1;

//
// Initialize the Huffman tree
//
PHtTree pTree = (PHtTree)malloc(sizeof(HtTree));
pTree->HtArray = (HtNode*)malloc(sizeof(HtNode) * HuffmanLength);

for (i = 0; i < HuffmanLength; i++)
{
    pTree->HtArray[i].lchild = -1;
    pTree->HtArray[i].rchild = -1;
    pTree->HtArray[i].parent = -1;
    pTree->HtArray[i].Weight = i < Count ? WeightArray[i] : -1; 
}

//
// Construction of Huffman tree
//
pTree = HuffmanTree(pTree, Count);

OutputResult(pTree, HuffmanLength);

//
// Destruction tree hoffman
//
free(pTree->HtArray);
free(pTree);

return 0;

}

/*
function:
Hoffman tree is constructed using a given set of weights.

parameter:
pTree -- Hoffman tree structure pointer
Count -- The count of array elements

returned value:
Returns the pointer to the Huffman tree structure.
*/
PHtTree HuffmanTree(PHtTree pTree, int Count)

{
// Cursor, which is mainly used to find the index of the smallest node and the second smallest node
int i, j;

int Index1, Index2; // Holds the subscript of the smallest and subsmallest nodes

int Number1, Number2; // Store minimum and sub - small node weights

//
// TODO: Add the code here
//  

return pTree;

}

void OutputResult(PHtTree pTree, int Length)
{
int i;
printf("subscript\tweight\tparent\tlchild\trchild\n");
for (i = 0; i < Length; i++)
{
printf("%d", i);
printf("\t\t%d", pTree->HtArray[i].Weight);
printf("\t\t%d", pTree->HtArray[i].parent);
printf("\t\t%d", pTree->HtArray[i].lchild);
printf("\t\t%d\n", pTree->HtArray[i].rchild);

}
}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换