当前位置:网站首页>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;
}边栏推荐
- Methods of de duplication and connection query in MySQL database
- How to choose digital twin visualization platform
- Oracle使用impdp导入报错:ORA-39001: 参数值无效 ORA-39000: 转储文件说明错误 ORA-39088: 文件名不能包含路径说明
- Automated test Po design model
- STM8S003F3 内部flash调试
- Auditing related notes
- tkinter GUI版通信录管理系统
- Mock服务moco系列(三)- 重定向、正则表达式、延迟、模板、事件、分模块设计
- 2022/7/23
- Update 3dcat real time cloud rendering V2.1.2 release
猜你喜欢

Interviewer: talk about log The difference between fatal and panic

Auditing相关注解

Why the future of digitalization depends on 3D real-time rendering

SLA 、SLO & SLI

图的相关操作

十九岁的总结

OSPF comprehensive experiment

Problems faced by cloud XR and main application scenarios of cloud XR

What is the relationship between cloud fluidization and cloud desktop

IDEA集成SVN代码管理常用功能
随机推荐
实时云渲染有哪些优势
Kendryte K210 在freertos上的lcd屏幕的使用
Redis source code and design analysis -- 18. Analysis of redis network connection Library
Take you to a preliminary understanding of multiparty secure computing (MPC)
UFT (QTP) - summary points and automated test framework
H5测试点(思维导图)
Go channel simple notes
PHP解决并发问题的几种实现
图的相关操作
Sorting also needs to know the information and linked list
关于flickr的数据集笔记
What are the advantages of real-time cloud rendering
UFT(QTP)-总结点与自动化测试框架
What is the relationship between cloud fluidization and cloud desktop
C语言 整数与字符串的相互转换
更新|3DCAT实时云渲染 v2.1.2版本全新发布
Ch582 ble 5.0 uses Le coded broadcast and connection
Introduction to cloud XR and development opportunities of cloud XR in 5g Era
go defer与recover简单笔记
C语言 cJSON库的使用