#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
using namespace std;
#define max 100
//define the nodes of hufuman tree
struct HTNode
{
int m_parent;
int m_lchild;
int m_rchild;
int m_weight;
};
//define hufuman tree
struct HTree
{
HTNode* m_HTree;
int m_length;
};
//init function
void initHTree(HTree& HT, int num)
{
int total_Node = 2 * num - 1;
HT.m_HTree = new HTNode[total_Node];
if (!HT.m_HTree)
{
cout << "---init fail---" << endl;
}
HT.m_length = 0;
int i;
for (i = 0;i < num;i++)
{
HT.m_HTree[i].m_parent = -1;
HT.m_HTree[i].m_rchild = -1;
HT.m_HTree[i].m_lchild = -1;
cout << "---Please input the weight of--- " << i + 1 << endl;
int weight;
cin >> weight;
HT.m_HTree[i].m_weight = weight;
HT.m_length++;
}
for (;i < total_Node;i++)
{
HT.m_HTree[i].m_parent = -1;
HT.m_HTree[i].m_rchild = -1;
HT.m_HTree[i].m_lchild = -1;
HT.m_HTree[i].m_weight = 999;
HT.m_length++;
}
}
//find two min node Function
void find_twomin_node(HTree& HT, int pos, int& min1, int& min2)
{
int i = 0;
int m1, m2;
int minweight;
//find min1
while (HT.m_HTree[i].m_parent != -1)
{
i++;
}
minweight = HT.m_HTree[i].m_weight;
m1 = i;
for (;i < pos;i++)
{
if (HT.m_HTree[i].m_weight < minweight && HT.m_HTree[i].m_parent == -1)
{
minweight = HT.m_HTree[i].m_weight;
m1 = i;
}
}
HT.m_HTree[m1].m_parent = 1; // change the node state to not root
min1 = m1;
//find min2
int j = 0;
while (HT.m_HTree[j].m_parent != -1)
{
j++;
}
minweight = HT.m_HTree[j].m_weight;
m2 = j;
for (;j < pos;j++)
{
if (HT.m_HTree[j].m_weight < minweight && HT.m_HTree[j].m_parent == -1)
{
minweight = HT.m_HTree[j].m_weight;
m2 = j;
}
}
HT.m_HTree[m2].m_parent = 1; // change the node state to not root
min2 = m2;
}
//function of createTree
void creat_hufuman_tree(HTree& HT, int num)
{
if (!HT.m_HTree)
{
cout << "---The tree is not exist--- " << endl;
}
int i, min1, min2;
for (i = num;i < HT.m_length;i++)
{
find_twomin_node(HT, i, min1, min2);
HT.m_HTree[min1].m_parent = i;
HT.m_HTree[min2].m_parent = i;
HT.m_HTree[i].m_lchild = min1;
HT.m_HTree[i].m_rchild = min2;
HT.m_HTree[i].m_weight = HT.m_HTree[min1].m_weight + HT.m_HTree[min2].m_weight;
}
}
//function of huffman coding
void HuffmanCoding(HTree& HT,char** HC)
{
int numofCode = (HT.m_length + 1) / 2;//叶子结点个数
HC = new char* [numofCode];//char数组 的数组
if (!HC)
{
cout << "---huffmancode memory is fail---" << endl;
}
char* code = new char[numofCode];
if (!code)
{
cout << "---code memory is fail---" << endl;
}
int cur = HT.m_length - 1;//pos in array
int codelen = 0;
//visit stste : 0 :no visit; 1 :lchild visit; 2 : all visit
int i;
for (i = 0;i < HT.m_length;i++)
{
HT.m_HTree[i].m_weight = 0;
}
while (cur != -1)
{
if (HT.m_HTree[cur].m_weight == 0)
{
HT.m_HTree[cur].m_weight = 1;
if (HT.m_HTree[cur].m_lchild != -1)
{
code[codelen++] = '0';
cur = HT.m_HTree[cur].m_lchild;
}
else
{
code[codelen] = '\0';
HC[cur] = new char[numofCode + 1];
if (!code)
{
cout << "---code mem fail---" << endl;
}
strcpy(HC[cur], code);
}
}
else if (HT.m_HTree[cur].m_weight == 1)
{
HT.m_HTree[cur].m_weight = 2;
if (HT.m_HTree[cur].m_rchild != -1)
{
code[codelen++] = '1';
cur = HT.m_HTree[cur].m_rchild;
}
}
else
{
HT.m_HTree[cur].m_weight = 0;
cur = HT.m_HTree[cur].m_parent;
--codelen;
}
}
//show the code
for (int j = 0;j < numofCode;j++)
{
cout << "---the code is " << HC[j] << endl;
}
delete[] code;
}
//show code
void showHuffmanCode(char** &HC,int numofCode)
{
for (int j = 0;j < numofCode;j++)
{
cout << "---the code is " << HC[j] << endl;
}
}
//show function
void showTree(HTree& HT)
{
if (HT.m_HTree != NULL)
{
cout << "---Show the Node of hufumantree---" << endl;
for (int i = 0;i < HT.m_length;i++)
{
//cout<< HT.m_HTree[i].m_lchild<<endl;
//cout<< HT.m_HTree[i].m_parent<<endl;
//cout<< HT.m_HTree[i].m_rchild<<endl;
cout << HT.m_HTree[i].m_weight << endl;
}
}
}
//test
void test()
{
cout << "---Please input the number of leaf_Node--- " << endl;
int num;
cin >> num;
HTree mytree;
initHTree(mytree, num);
int pos, min1, min2;
pos = mytree.m_length;
find_twomin_node(mytree, pos, min1, min2);
cout << min1 << " 1 :: 2 " << min2 << endl;
showTree(mytree);
}
//test
void test1()
{
cout << "---Please input the number of leaf_Node--- " << endl;
int num;
cin >> num;
HTree mytree;
initHTree(mytree, num);
creat_hufuman_tree(mytree, num);
showTree(mytree);
cout << "------------------------" << endl;
char** hcode;
HuffmanCoding(mytree,hcode);
cout << "------------------------" << endl;
//showHuffmanCode(hcode,num);
}
int main()
{
test1();
system("pause");
return 0;
}
构建哈夫曼树并创建哈弗曼编码
最新推荐文章于 2023-06-04 10:35:51 发布