For example: "aabc"
Build a Huffman tree to know the encoding of the characters in the above string
a:1
b:00
c:01
Therefore, the Huffman code for "aabc" is "110001", which is six bytes in total.
code show as below:
#include <iostream>
using namespace std;
char str[30]; //输入的字符
struct Node
{
Node *lchild;
Node *rchild;
char c; //字符
int value; //字符出现的频率
bool operator < (Node &A)
{
return value < A.value;
}
}TreeNode[50];
int loc;
Node *creat() //初始化
{
TreeNode[loc].lchild = NULL;
TreeNode[loc].rchild = NULL;
TreeNode[loc].c = NULL;
TreeNode[loc].value = 0;
return &TreeNode[loc++];
}
Node *deleteNode()
{
TreeNode[loc].lchild = NULL;
TreeNode[loc].rchild = NULL;
TreeNode[loc].c = NULL;
TreeNode[loc].value = 0;
return &TreeNode[--loc];
}
void structOriginal() //Record the frequency of each character in the input string, initialize the node
{
int len = strlen(str);
for (int i = 0; i < len; i++)
{
Node *T = NULL;
if (T==NULL)
{
T = creat();
T->c = str[i];
T->value += 1;
}
for (int j = 0; j < len; j++)
{
if (j<i && T->c==str[j])
{
T = deleteNode();
break;
}
else if (j>i && T->c==str[j] )
{
T->value += 1;
}
}
}
}
int rankTreeNode() // Sort the structure array by weight; loc records the current number of nodes
{
int tree_n = loc;
for (int i = 0; i < loc-1; i++)
{
bool flag = false;
for (int j = loc-1; j > i; j--)
{
if (TreeNode[j] < TreeNode[j - 1])
{
swap(TreeNode[j].value, TreeNode[j - 1].value);
swap(TreeNode[j].c, TreeNode[j - 1].c);
flag = true;
}
}
if (flag==false)
{
return tree_n;
}
}
return tree_n;
}
/ /Construct a Huffman tree according to the node weight
Node *buildHuffmanTree(Node *T,int n) //n represents the position of the current maximum weight node
{
int count = 0;
for (int index = 0; index < n; index++)
{
count += TreeNode[index].value;
}
if (T==NULL)
{
T = creat();
T->value = count;
if (n == 1)
{
return T;
}
else
{
T->rchild = &TreeNode[--n];
T->lchild = buildHuffmanTree(T->lchild, n);
}
}
return T; //return the root node pointer
}
int main()
{
int str_n; //Number of nodes
while (cin>>str_n)
{
loc = 0;
Node *T = NULL; //The root node of Huffman tree is empty
cin >> str;
structOriginal();
int tree_count = rankTreeNode(); //tree_count is to remove duplicate nodes, the actual number of nodes involved in sorting
T = buildHuffmanTree(T, tree_count); //the root node of Huffman tree
int tree_length = 0; //tree height
int ret = 0; //result
while (T->rchild!=NULL)
{
tree_length++;
ret += tree_length*(T->rchild)->value;
T = T->lchild;
}
ret += tree_length*(T->value);
cout << ret << endl;
}
return 0;
}
Rough code a bit, not perfect, please correct me.