当前位置:网站首页>Construction of Huffman tree
Construction of Huffman tree
2022-07-25 18:02:00 【Advanced Xiaoxin】
【 Huffman tree 】 When used n Nodes ( All of them are leaf nodes and each has its own weight ) When trying to build a tree , If the weighted path length of the constructed tree is the smallest , Call this tree “ The best binary tree ”, Sometimes called the “ Huffman tree ” perhaps “ Huffman tree ”.
【 Building principles 】 The node with large weight is close to the root , The node with small weight is far from the root .
【 Preliminary knowledge 】 Huffman tree is a binary tree , Only 0 Degree node sum 2 Degree node .
For having n0 A Huffman tree with leaf nodes , share 2*n0-1 Nodes .
【 Algorithm steps 】
1. initialization ,ht An array of former n individual weight The member variable is assigned a weight , All parents and left and right sons are assigned -1.
2. before n In the forest composed of nodes, the two nodes with the smallest weight are selected to form a new binary tree to replace the original two nodes .
3. repeat 2 Until there is only one binary tree left in the forest .
【 Case study 】
Input :
7
5 2 9 11 8 3 7Output :
Please enter the number of nodes :7
Please enter the weight of the node :5 2 9 11 8 3 7
Minimum subscript :1 Minor subscript :5
Minimum subscript :0 Minor subscript :7
Minimum subscript :6 Minor subscript :4
Minimum subscript :2 Minor subscript :8
Minimum subscript :3 Minor subscript :9
Minimum subscript :10 Minor subscript :11
Print the contents of the constructed Huffman array :
weiht parent lchild rchild
5 8 -1 -1
2 7 -1 -1
9 10 -1 -1
11 11 -1 -1
8 9 -1 -1
3 7 -1 -1
7 9 -1 -1
5 8 1 5
10 10 0 7
15 11 6 4
19 12 2 8
26 12 3 9
45 -1 10 11The manual tree creation is as follows :

notes : The code strictly follows that the weight of the left son is greater than that of the right son , Manual operation may produce different results , But the length of the root node is the same , The length of the shortest weighted path is the same .
Don't talk much , Code up :
#include <stdio.h>
typedef struct node {
int weight;
int parent, lchild, rchild;
}HTNode;
void print(HTNode *ht, int n) {
printf(" Print the contents of the constructed Huffman array :\n");
printf("weiht parent lchild rchild\n");
for(int i=0;i<2*n-1;i++)
printf("%d\t%d\t%d\t%d\n",ht[i].weight,ht[i].parent,ht[i].lchild,ht[i].rchild);
}
void creatht(HTNode* ht, int n) {
int min1, min2;
int lnode, rnode;
for(int i=n;i<2*n-1;i++) {
min1 = min2 = 0x7fffffff;
lnode = rnode = -1;
for(int k=0;k<i;k++) { // Find the two nodes with the smallest weight among the nodes whose parents have not been assigned a value, that is, the corresponding subscript
if(ht[k].parent == -1) {
if(ht[k].weight < min1) {
min2 = min1, rnode = lnode;
min1 = ht[k].weight, lnode = k;
} else if(ht[k].weight < min2) {
min2 = ht[k].weight, rnode = k;
}
}
}
printf(" Minimum subscript :%d\t",lnode);
printf(" Minor subscript :%d\n",rnode);
ht[i].weight = ht[lnode].weight + ht[rnode].weight; // Create subscript as i The new node
ht[i].lchild = lnode, ht[i].rchild = rnode;
ht[lnode].parent = ht[rnode].parent = i; // The parents of the left and right sons are assigned a new node
}
print(ht, n);
}
int main() {
int n;
printf(" Please enter the number of nodes :"); // initialization
scanf("%d",&n);
printf(" Please enter the weight of the node :");
HTNode ht[2*n-1];
for(int i=0;i<n;i++)
scanf("%d",&ht[i].weight);
for(int i=0;i<2*n-1;i++)
ht[i].parent = ht[i].lchild = ht[i].rchild = -1;
creatht(ht, n);
return 0;
}边栏推荐
- 简述冒泡排序与快速排序
- Which real-time gold trading platform is reliable and safe?
- Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
- 越来越成熟的Rust,都应用了哪些场景呢?
- H5 test point (mind map)
- I'm also drunk. Eureka delayed registration and this pit!
- Is there any inconspicuous but profitable sideline?
- "Deprecated gradle features were used in this build, making it incompatible with gradle 6.0" problem solving
- tkinter GUI版通信录管理系统
- 简述聚簇索引、二级索引、索引下推
猜你喜欢

Highlights

十九岁的总结

大话DevOps监控,团队如何选择监控工具?

Redis source code and design analysis -- 15. RDB persistence mechanism

nodejs 简单例子程序之express

Automated test Po design model

Redis source code and design analysis -- 16. AOF persistence mechanism

Auditing相关注解

Talking about Devops monitoring, how does the team choose monitoring tools?

WPF 实现用户头像选择器
随机推荐
Creation of unity Bezier curve
Unity 贝塞尔曲线的创建
New and malloc
绘制pdf表格 (一) 通过itext实现在pdf中绘制excel表格样式并且实现下载(支持中文字体)
Auditing相关注解
Brief introduction of bubble sort and quick sort
Update 3dcat real time cloud rendering V2.1.2 release
PageHelper还能结合Lambda表达式实现简洁的分页封装
[MySQL]数据库中的索引为什么是用B+树来实现? 哈希表/红黑树/B树是否可行呢?
SVN客户端(TortoiseSVN)安装及使用说明
UFT (QTP) - summary points and automated test framework
十九岁的总结
"Deprecated gradle features were used in this build, making it incompatible with gradle 6.0" problem solving
Interviewer: talk about log The difference between fatal and panic
专访即构科技李凯:音视频的有趣、行业前沿一直吸引着我
Postman get started quickly
Landmark buildings around the world
绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码
为什么数字化未来取决于3D实时渲染
Introduction to cloud XR and development opportunities of cloud XR in 5g Era