在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);

}
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

1
MFC点击button确定按钮如何打开新的窗体?
1
用vc6.0做FFT图像处理,得到的频谱是彩色的,而且频谱大小不能完全将原来的图像覆盖,为什么???急急急
2
VC++6.0想在单文档中随机输入26个小写字母,为什么调用了TextOut却什么也没显示出来呢?
1
VC++6.0在CXXXView的构造函数中用了GetDC(),不报错,但运行之后提示错误
2
VC++6.0 双击左键函数不响应
1
MFC 文件操作出现 an unnamed file contains a n ivvalid path 实在没钱了 求救
1
MFC 代码问题 关于绘制的
3
请问下边这段代码的运行流程是啥?
1
c++,没有error和warning,一运行就出现这个
4
关于多字节和unicode字符节的区别
2
非模态对话框创建的时候出错
1
非模态对话框出错,非模态对话框
2
VC++6.0 MFC创建新的dialog报错
1
请问怎么在动态创建的CStatic类控件里,用CDC画图?(请问我的代码怎么不能显示)
2
MFC聊天室:不允许指针指向不完整的类类型
1
MFC调用动态链接库问题
2
未声明标识符?强制转化也不行
0
基于MFC做的音乐播放器,新手,不知道问什么调试了没有声音
1
求问,用tkinter画一个界面,如何改变菜单栏的state属性?求大神!求!
1
MFC用了一个EDIT控件和一个SPIN控件绑定,想通过点击向上箭头增大m_top的值,重新设置了spin控件的范围,点一次向上箭头数值是+1了,但是其余的功能均没有反应,不知道为何