当前位置:网站首页>哈夫曼树的构建
哈夫曼树的构建
2022-07-25 17:16:00 【进阶的小新】
【哈夫曼树】当用 n 个结点(都做叶子结点且都有各自的权值)试图构建一棵树时,如果构建的这棵树的带权路径长度最小,称这棵树为“最优二叉树”,有时也叫“赫夫曼树”或者“哈夫曼树”。
【构建原则】权值大的节点离根近,权值小的节点离根远。
【预备知识】哈夫曼树是二叉树,只有0度节点和2度节点。
对于有n0个叶子节点的哈夫曼树,共有2*n0-1个节点。
【算法步骤】
1.初始化,ht数组的前n个weight成员变量赋值为权重,所有的双亲和左右儿子赋值为-1。
2.在前n个节点点构成的森林中选取权值最小的两个节点构成一棵新的二叉树代替原来的两个节点。
3.重复2直到森林中只剩一棵二叉树。
【案例】
输入:
7
5 2 9 11 8 3 7输出:
请输入节点个数:7
请输入节点的权值:5 2 9 11 8 3 7
最小下标:1 次小下标:5
最小下标:0 次小下标:7
最小下标:6 次小下标:4
最小下标:2 次小下标:8
最小下标:3 次小下标:9
最小下标:10 次小下标:11
打印构建好的哈夫曼数组内容:
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 11手动建树如下:

注:代码严格按照左儿子的权值大于右儿子的权值执行,手动可能产生不同结果,但根节点的长度是一样的,最短带权路径长度也是一样的。
话不多说,上代码:
#include <stdio.h>
typedef struct node {
int weight;
int parent, lchild, rchild;
}HTNode;
void print(HTNode *ht, int n) {
printf("打印构建好的哈夫曼数组内容:\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++) { //在双亲还未被赋值的节点中找到权值最小的两个节点即对应下标
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("最小下标:%d\t",lnode);
printf("次小下标:%d\n",rnode);
ht[i].weight = ht[lnode].weight + ht[rnode].weight; //创建下标为i的新节点
ht[i].lchild = lnode, ht[i].rchild = rnode;
ht[lnode].parent = ht[rnode].parent = i; //左右儿子的双亲赋值为新节点
}
print(ht, n);
}
int main() {
int n;
printf("请输入节点个数:"); //初始化
scanf("%d",&n);
printf("请输入节点的权值:");
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;
}边栏推荐
- [Nanjing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
- [mathematical modeling and drawing series tutorial] II. Drawing and optimization of line chart
- What are the free low code development platforms?
- 「数字安全」警惕 NFT的七大骗局
- 04. Find the median of two positive arrays
- pgsql有没有好用的图形化管理工具?
- Random talk on generation diffusion model: DDPM = Bayesian + denoising
- 基于redis6.2.4的redis cluster部署
- ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity
- postgreSQL 密码区分大小写 ,有参数控制吗?
猜你喜欢

生成扩散模型漫谈:DDPM = 贝叶斯 + 去噪

Hcip notes 11 days

Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project

Starting from business needs, open the road of efficient IDC operation and maintenance
![[target detection] yolov5 Runtong visdrone data set](/img/a6/118e6bbeb254f9d1afd1406d1b0089.png)
[target detection] yolov5 Runtong visdrone data set

How to install govendor and open a project

HCIP笔记十一天

Automatic reply of wechat official account development message

Jenkins' file parameters can be used to upload files

用秩讨论线性方程组的解/三个平面的位置关系
随机推荐
How to prevent the unburned gas when the city gas safety is alarmed again?
聊聊如何用 Redis 实现分布式锁?
Is there a principal guaranteed product for financial management?
From digitalization to intelligent operation and maintenance: what are the values and challenges?
咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
Dynamic planning topic record
[OBS] frame loss and frame priority before transmission
HCIP笔记十一天
Outlook tutorial, how to search for calendar items in outlook?
搜狗批量推送软件-搜狗批量推送工具【2022最新】
jenkins的文件参数,可以用来上传文件
Technical difficulties and applications of large humanoid robots
Crawler framework crawler
EasyUI modification and DataGrid dialog form control use
第三章、数据类型和变量
一百个用户眼中,就有一百个QQ
博后招募 | 西湖大学机器智能实验室招聘博士后/助理研究员/科研助理
Wu Enda logistic regression 2
[target detection] yolov5 Runtong voc2007 dataset (repair version)
Chapter 4: operators