当前位置:网站首页>Interesting Huffman tree
Interesting Huffman tree
2022-07-28 00:44:00 【*Black heart radish three bars*】
When we study data structures , It is inevitable to encounter problems related to Huffman tree , Xiaobian will take you to appreciate the charm of Huffman tree today .
One 、 Explanation of Huffman tree
Given N The weight of N A leaf node , Construct a binary tree , If the weighted path length of the tree reaches the minimum , Call such a binary tree an optimal binary tree , Also known as Huffman tree (Huffman Tree). Huffman tree is the tree with the shortest weight path length , Nodes with larger weights are closer to the root .
Two 、 The construction of Huffman tree
1、 Arrange all nodes in order from small to large .
2、 Take out the two smaller nodes as the left and right nodes of the new binary tree , Be careful , Use the smaller node as the left child , The larger node acts as the right child .
3、 Reorder the newly generated binary tree nodes together with other nodes .
4、 Repeat step 1、2、3 Until there is only one node left , The only node is the Huffman tree we need to construct .
while(list->count>1){
simpleselectsort(list);
p = list->head->next;
q = p->next;
tempbtnode0 = p->btnode;
tempbtnode1 = q->btnode;
newbtnode = (struct btnode *) malloc(sizeof(struct btnode ));
newbtnode->data = tempbtnode0->data + tempbtnode1->data;
newbtnode->info = '*';
newbtnode->leftchild = tempbtnode0;
newbtnode->rightchild = tempbtnode1;
p->btnode = newbtnode;
p->next = q->next;
free(q);
list->count--;
}3、 ... and 、 Get Huffman code
We use stack to recursively traverse binary tree , When the leaf node is reached , We output all the elements stored in the stack , You can get the reverse Huffman code of the leaf node . Here are the relevant steps and their codes :
1、 When the left child of this node is not empty , Then we will first 0 Push to stack , Then visit the left child of the node , After the visit, we need to get the top element out of the stack ; When the left child of this node is empty , Just execute the following procedure .
2、 When the node is a leaf node , We will output the data stored in the stack ; When this node is not a leaf node , Just execute the following procedures in sequence ;
3、 When the right child of this node is not empty , Then we will first 1 Push to stack , Then visit the right child of the node , After the visit, we need to get the top element out of the stack ; When the right child of this node is empty , End this recursion .
void inordervisit(struct btnode *p,struct int_slink_stack *stack){
if(p->leftchild != NULL){
int_slink_stack_push(stack,0);
inordervisit(p->leftchild,stack);
int_slink_stack_pop(stack);
}
if(p->leftchild == NULL && p->rightchild == NULL){
if((int_slink_stack_isempty(stack) == 0)){
printf("data:%d->",p->data);
struct int_slink_stack_node *q = stack->top;
while(q != NULL){
printf("%d",q->data);
q = q->next;
}
printf("\n");
}
}
if(p->rightchild != NULL){
int_slink_stack_push(stack,1);
inordervisit(p->rightchild,stack);
int_slink_stack_pop(stack);
}
}Four 、 summary
In fact, the establishment of Huffman tree is to select two smallest nodes each time and merge them into a new binary tree , Huffman coding is mainly about the problem of entering and leaving the stack when traversing the Huffman tree and the problem of outputting Huffman coding , As long as we solve these problems, we can get the Huffman code of leaf nodes .
Catalog
One 、 Explanation of Huffman tree
Two 、 The construction of Huffman tree
边栏推荐
- Prepare for the interview and stick to the third sentence of the question - Branch sentences!
- Threejs personal notes
- 英特尔发布开源AI参考套件
- Strong collaboration and common development! Intel and Taiyi IOT held a seminar on AI computing box aggregation services
- 几行代码轻松实现对于PaddleOCR的实时推理,快来get!
- 大众中国豪掷80亿,成国轩高科第一大股东
- Buildforge materials
- ASML launched the first generation HMI multi beam detector: the speed is increased by 600%, which is suitable for 5nm and more advanced processes
- C event related exercise code.
- Leetcode 452. minimum number of arrows to burst balloons (medium)
猜你喜欢

Fastjson历史漏洞复现

What foundation does Yolo need? How to learn Yolo?
![[21 day learning challenge] classmate K invites you to participate in the in-depth learning seminar](/img/88/b8d5e2a8609fbef57a1291b7c4225e.png)
[21 day learning challenge] classmate K invites you to participate in the in-depth learning seminar

In July, a software testing engineer came to the company. He looked like a hairy boy. He didn't expect to be the new generation of roll King
![[BRE]软件构建发布自动化](/img/c6/daead474a64a9a3c86dd140c097be0.jpg)
[BRE]软件构建发布自动化

Prepare for the interview and stick to the third sentence of the question - Branch sentences!

数据分析:拆解方法(详情整理)

code review 工具
![[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development](/img/29/f31fa3af18198754d2433f47c0c556.jpg)
[meetup preview] openmldb + ONEFLOW: link feature engineering to model training to accelerate machine learning model development

Rational and perceptual activities and required skills in programmers' work
随机推荐
基本初等函数
See how well-known enterprises use Web3 to reshape their industries
Leetcode 415. string addition and 43. string multiplication
ASML推出第一代HMI多光束检测机:速度提升600%,适用于5nm及更先进工艺
JVM memory model
JS event propagation capture stage bubbling stage onclick addeventlistener
The second uncle cured my spiritual internal friction and made me angry out of station B
CSDN21天学习挑战赛
单片机之led、数码管与按键
大众中国豪掷80亿,成国轩高科第一大股东
Yangchuanhui, CTO of oceanbase: some HTAP databases are not real htaps
英特尔携手汉朔、微软,释放“AI + 零售”大招!
MATLAB | 那些你不得不知道的MATLAB小技巧(二)
数据分析:拆解方法(详情整理)
HarmonyOS 3纯净模式可限制华为应用市场检出的风险应用获取个人数据
[leetcode] 547. Number of provinces (medium)
MATLAB | 那些你不得不知道的MATLAB小技巧(一)
永州水质检测实验室建设:家具说明
leetcode 452. Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球(中等)
҈直҈播҈预҈告҈ |҈ 炎热盛夏,与Nono一起跨越高温“烤”验吧!