当前位置:网站首页>07.02 Huffman code
07.02 Huffman code
2022-07-22 02:04:00 【zzyzxb】
One : Huffman code
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXBIT 100
#define MAXVALUE 10000
#define MAXLEAF 30
#define MAXNODE MAXLEAF*2 -1
typedef struct
{
double weight;
int parent;
int lchild;
int rchild;
char value;
} HNodeType; /* Node structure */
typedef struct
{
int bit[MAXBIT];
int start;
} HCodeType; /* Coding structure */
HNodeType HuffNode[MAXNODE]; /* Define a node structure array */
HCodeType HuffCode[MAXLEAF];/* Define an array of encoded structures */
/* Construct the Huffman tree */
void HuffmanTree(HNodeType HuffNode[MAXNODE], int n) {
/* i、j: Loop variable ,m1、m2: The weights of two minimum weight nodes in different processes of constructing Huffman tree ,
x1、x2: The sequence numbers of two minimum weight nodes in the array in different processes of constructing Huffman tree .*/
int i, j, x1, x2;
double m1, m2;
/* Initialize the Huffman tree array HuffNode[] Node in */
for (i = 0; i < 2 * n - 1; i++)
{
HuffNode[i].weight = 0;// A weight
HuffNode[i].parent = -1;
HuffNode[i].lchild = -1;
HuffNode[i].rchild = -1;
}
/* Input n Weight of leaf nodes */
for (i = 0; i < n; i++)
{
cout << "Please input value and weight of leaf node " << i + 1 << endl;
cin >> HuffNode[i].value >> HuffNode[i].weight;
}
/* structure Huffman Trees */
for (i = 0; i < n - 1; i++)
{// perform n-1 The merger
m1 = m2 = MAXVALUE;
/* m1、m2 Two nodes without parent nodes and with the smallest node weight are stored in */
x1 = x2 = 0;
/* Find out the smallest weight among all nodes 、 Two nodes without parent nodes , And merge them into a binary tree */
for (j = 0; j < n + i; j++)
{
if (HuffNode[j].weight < m1 && HuffNode[j].parent == -1)
{
m2 = m1;
x2 = x1;
m1 = HuffNode[j].weight;
x1 = j;
}
else if (HuffNode[j].weight < m2 && HuffNode[j].parent == -1)
{
m2 = HuffNode[j].weight;
x2 = j;
}
}
/* Set the two child nodes found x1、x2 Parent node information of */
HuffNode[x1].parent = n + i;
HuffNode[x2].parent = n + i;
HuffNode[n + i].weight = m1 + m2;
HuffNode[n + i].lchild = x1;
HuffNode[n + i].rchild = x2;
cout << "x1.weight and x2.weight in round " << i + 1 << "\t" << HuffNode[x1].weight << "\t" << HuffNode[x2].weight << endl; /* Used for testing */
}
}
/* Huffman tree coding */
void HuffmanCode(HCodeType HuffCode[MAXLEAF], int n) {
HCodeType cd; /* Define a temporary variable to store the information when solving the code */
int i, j, c, p;
for (i = 0; i < n; i++)
{
cd.start = n - 1;
c = i;
p = HuffNode[c].parent;
while (p != -1)
{
if (HuffNode[p].lchild == c)
cd.bit[cd.start] = 0;
else
cd.bit[cd.start] = 1;
cd.start--; /* Move forward one bit */
c = p;
p = HuffNode[c].parent; /* Set the next cycle condition */
}
/* The coding information of leaf nodes is changed from temporary coding cd It's copied out of , Put in the array of encoded structures */
for (j = cd.start + 1; j < n; j++)
HuffCode[i].bit[j] = cd.bit[j];
HuffCode[i].start = cd.start;
}
}
int main()
{
int i, j, n;
cout << "Please input n:" << endl;
cin >> n;
HuffmanTree(HuffNode, n); // Construct the Huffman tree
HuffmanCode(HuffCode, n); // Huffman tree coding
// Output all saved Huffman codes with existing codes
for (i = 0; i < n; i++)
{
cout << HuffNode[i].value << ": Huffman code is: ";
for (j = HuffCode[i].start + 1; j < n; j++)
cout << HuffCode[i].bit[j];
cout << endl;
}
return 0;
}
边栏推荐
- Database generates HTML document
- 大佬们我在diea本地运行没问题,放在服务器上运行,cdc连接不上咋回事,数据库ip可以ping通
- Huirong technology and jiangbolong work together to improve the competitiveness of mobile phone storage
- 【FreeRTOS】10 事件标志组
- 模板与泛型编程之值萃取
- 奇葩!一公司面试题竟问如厕习惯、吃饭时长、入睡时间等
- have a look
- OSPF knowledge summary
- Number game: n people count off, those who report a multiple of 3 leave, and the rest continue
- RedisGraph图形数据库多活设计方案
猜你喜欢
两数相加leecode2
编程语言之父们退休太无聊,纷纷选择重返职场
Judgment of empty string in Oracle
clip:learning transferable visual models from natural language supervision
3564. 日期类
Database transaction isolation level
huawei设置使用账号密码登录
The sum of the last three numbers
Oracle中對空字符串的判斷
Understand the domestic open source Magnolia license series agreement in simple terms
随机推荐
[deep learning notes] attention mechanism
LeetCode 560和为 K 的子数组(有负数、一次遍历前缀和)、LeetCode 438找到字符串中所有字母异位词(优化滑动窗口)、LeetCode 141环形链表I(快慢指针)、142II
Aruba learning notes 03 Web UI -- Introduction to monitoring panel
Multiple knapsack problem code template
sql中substr与substring函数用法
3537. 树查找
AtCoder Beginner Contest 260 - C, D, E
COVID-19-20——基于VNet3D分割网络的基础方法
午休专列&问题思考:由时:分:秒构成字符串转换为秒的问题思考
一文让你掌握22个神经网络训练技巧
Local storage software system of local area network for data acquisition of electric meters in Enterprises
TodoList案例
2022-7-17 FTP client project implementation - Summary
SparkSql中的窗口函数
m基于中继协助的认知无线电频谱切换机制的matlab仿真分析
The sum of the last three numbers
数字游戏:n人报数,报3的倍数的人离开,其余继续
模板与泛型编程之值萃取
RedisGraph图形数据库多活设计方案
Jugement des chaînes vides dans Oracle