Huffman Tree Coding __c

Source: Internet
Author: User
 

Statistical probability of the entered English capitals and then build Huffman tree, output is sorted according to the probability of descending sort output Huffman encoding
Input
Number of uppercase letters N first letter The second letter the third letter ... Nth Letter

Output
Letter 1 occurrence Times Huffman encoding
Letter 2 occurrence Times Huffman encoding
Letter 3 occurrence Times Huffman encoding
...
Letter N Occurrence Times Huffman encoding

Sample in
10
I i u u i u N u u

Sample out U 6 1
I 3 01
N 1 00

To solve this problem, we must first understand the construction principle of huffmantree

First of all, let's look at a simple example: Convert a class of classmates hundred to 5 points, the rules are as follows,
90~100 5 80~90 4 70~80 3 60~70 2
<60 1
It seems easy to implement that if (score<60) grade=1;      else if (score<70) grade=2;      else if (score<80) grade=3;      else if (score<90) grade=4; else grade=5; But if most of the students in this class have achieved a good score of more than 90 points, the previous 4-step judgment is not necessary and time-consuming. Obviously, this algorithm is more unreasonable in the face of large amount of data. So how to optimize the algorithm. Assuming we already know the distribution of this class's results.

Results

<60

60~70

70~80

80~90

90~100

Proportion

0.05

0.15

0.33

0.27

0.20


If you use just the method, then the efficiency of the algorithm is how much, with the proportion of the number of judgments 0.05*1+0.15*2+0.33*3+0.27*4+0.20*4=3.22 we slightly modify the algorithm, the first judge the proportion of the largest, that is, if (score<80)
{
if (score<70)
if (score<60)
grade=1;
Else
grade=2;
Else
grade=3;
}
else if (score<90)
grade=4;
Else
grade=5; The improved algorithm efficiency is 0.05*3+0.15*3+0.33*2+0.27*2+0.2*2=2.2 obviously, and the improved algorithm efficiency is increased a lot. The tree definition allows you to abstract just the program into the "tree" shown in the following illustration:

So how to construct a more efficient or the best search tree, this is the Huffman tree to solve the problem. Constructs the Huffmantree thought: the Weight value (frequency) from small to the big order, the weight value smallest two binary trees merge such as the existing weight value is the 1,2,3,4,5 node (already arranged well). 1. Select the smallest two, that is 1 and 2, combined, the sum of the weights is 3, 2. Select the 3 smallest from the merged good two and the remaining 3,4,5, that is, 3 and 3, combined, the sum of weights is 6 3. Select the two smallest from the 6,4,5, that is 4 and 5, merging, the sum of weights is 9 4. Merge 6 and 9, The sum of the weights is 15 below the Huffman tree formed:
For the above question, one of our ideas should be like this 1. Counts the number of occurrences of the same character and records 2. According to the results of a good statistical structure Huffman Tree 3. Get Huffman Code 4 >
#include <cstring>
#include <cstdio>
using namespace Std;


Template<class t>
struct stafrequency
{
T data;
int times;
Stafrequency ();
Stafrequency (T data,int times) {this->data=data;this->times=times;}
};
Template<class t>
struct Trinode
{
T data;
int parent,left,right;
};
Template<class t>
Class Huffmantree
{
Private
int leafnum;
Trinode<int> *huftree;
Char **hufcodes;
void Createhuffmantree (T weight[],int N);
void Gethuffmancode ();
Public
Huffmantree (T weight[],int N);
~huffmantree () {delete []huftree;delete []hufcodes;};
void print (int i);
};
const int max_weight=9999;
Template <class t>
Huffmantree<t>::huffmantree (T weight[],int N)
{
Createhuffmantree (Weight,n);
Gethuffmancode ();
}
Structural Huffman Tree
Template <class t>
void Huffmantree<t>::createhuffmantree (T weight[],int N)
{
Leafnum=n;
Huftree=new trinode<int>[2*n-1];
int i;
for (i=0;i<n;i++)
{
Huftree[i].data=weight[i];
Huftree[i].parent=huftree[i].left=huftree[i].right=-1;
}
for (i=0;i<n-1;i++)
{
int min1,min2,x1,x2;
Min1=min2=max_weight;
X1=x2=-1;
for (int j=0;j<n+i;j++)
{
if (huftree[j].data<min1&&huftree[j].parent==-1)
{
Min2=min1;
x2=x1;
Min1=huftree[j].data;
X1=j;
}
else if (huftree[j].data<min2&&huftree[j].parent==-1)
{
Min2=huftree[j].data;
X2=j;
}
}
Huftree[x1].parent=n+i;
Huftree[x2].parent=n+i;
Huftree[n+i].data=huftree[x1].data+huftree[x2].data;
Huftree[n+i].parent=-1;
huftree[n+i].left=x1;
huftree[n+i].right=x2;
}
}
Get Huffman Code
Template <class t>
void Huffmantree<t>::gethuffmancode ()
{
int n=leafnum;
Hufcodes=new Char *[n];
for (int i=0;i<n;i++)
{
char * code=new char[n];
Code[n-1]= ' ";
int start=n-1;
int child=i;
int parent=huftree[child].parent;
while (Parent!=-1)
{
start--;
if (huftree[parent].left==child)
code[start]= ' 0 ';
Else
code[start]= ' 1 ';
Child=parent;
Parent=huftree[child].parent;
}
Hufcodes[i]=code+start;
}
}
Print Huffman Code
Template <class t>
void Huffmantree<t>::p rint (int i)
{
cout<}
int main ()
{
int m;
cin>>m;
Char Weight[m];
for (int i=0;i<m;i++)
cin>>weight[i];
int i=0,n=0,sum=0,num=0,x=0;
int p[n];
Char W[m];
BOOL Flag;
for (i=0;i<m;i++)
{
Flag=true;
sum=0;
for (int j=i-1;j>=0;j--)//Check if there are any letters that have been counted
{
if (Weight[j]==weight[i])
{
Flag=false;
Break
}
}
for (n=i;n<m&&flag;n++)//If the previous step does not, then count the same number
{
if (Weight[i]==weight[n])
{
sum++;
}
}
if (flag)//storage of the same characters and number
{
S[num++]=sum;
W[x++]=weight[i];
}
}
Just write the simplest bubble sort.
for (int k=0;k<num;k++)
{
for (int j=0; j<num-1-k; j + +)
{
if (s[j]<s[j+1])
{
Swap (s[j],s[j+1]);
Swap (w[j],w[j+1]);
}


}
}
Huffmantree<int> Htree (S,num);
for (int i=0;i<num;i++)
{
cout<<w[i]<< "" <<s[i]<< "";
Htree.print (i);
}
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.