K-Fork Huffman Tree
THINK
What is Huffman tree.
Huffman Tree is a two-fork tree with a weighted path length of the smallest. The K-Fork Huffman Tree is the smallest K-fork tree with weighted path length. Huffman tree is also an optimal decision tree in the context of the need for conditional judgment. What is the use of Huffman tree. Optimal decision Tree Huffman coding How to construct K-Fork Huffman Tree
For a given real number, two minimum weights are added each time until an extended binary tree is obtained, so that the extended binary tree has the smallest weighted path length. And for K-fork Huffman Tree, need to do is to find the minimum weight of the K number to merge. Consider the idea of merging sorts, define two pointers, move through the array of leaf nodes and inner nodes, and select the smaller ones in P1 and P2 each time until the smallest number of k is obtained. CODE
@author Hunter Lee #include <iostream> #include <float.h> using namespace std;
#define N #define K 3 #define M (n-k)/(K-1) +1+n typedef struct node{string CHR;
Double weight;
int children[k];
int parent;
}HT;
typedef HT HUFFMANT[M+1];
Huffmant T;
int q[m+1];
int last = 0; void Sort (Huffmant T) {for (int i=1, i<n; i++) for (int j=n-1; j>=i; j--) if (T[j].weight <
t[j-1].weight) {Float temp = t[j-1].weight;
string s = T[J-1].CHR;
T[j-1].weight = T[j].weight;
T[J-1].CHR = T[J].CHR;
T[j].weight = temp;
T[J].CHR = s;
} return;
} float Selectmin (huffmant T, int i) {cout<< "Position:" <<i+1<<endl;
int num=0, p1=0, p2;
float sum=0;
for (; p1<n; p1++) if (t[p1].parent = =-1) break;
if (P1 = = N) P1 = M;
if (i = = N) P2 = M; else for (P2=n; p2<i;
p2++) if (t[p2].parent = =-1) break;
cout<< "P1:" <<p1<< "P2:" <<p2<<endl;
while (num<k && (p1!=m | | p2!=m)) if (t[p1].weight <= t[p2].weight) {sum + = T[p1].weight;
cout<<p1<< "(p1):" <<T[p1].weight<<endl;
T[p1].parent = i;
T[i].children[num] = p1;
num++;
for (; p1<n; p1++) if (t[p1].parent = =-1) break;
if (P1 = = N) P1 = M;
} else{sum + = t[p2].weight;
cout<<p2<< "(p2):" <<T[p2].weight<<endl;
T[p2].parent = i;
T[i].children[num] = p2;
num++;
for (; p2<i; p2++) if (t[p2].parent = =-1) break;
} cout<< "min:" <<sum<<endl;
return sum; } void Initht (Huffmant) {int i;
cout<< "Please input the character and the weight ..." <<endl;
For (i=0, i<n; i++) {cout<<endl<< "The character is:";
cin>>t[i].chr;
cout<< "The weight is:";
cin>>t[i].weight;
T[i].parent =-1;
for (int j=0; j<k; j + +) T[i].children[j] =-1;
} for (i=n; i<=m; i++) {t[i].parent =-1;
for (int j=0; j<k; j + +) T[i].children[j] =-1;
} t[m].weight = Flt_max;
for (int j=0; j<n; j + +) cout<<t[j].chr<<endl; cout<<endl<< "succeed!"
<<endl<<endl;
} void Createhuffmantree (Huffmant t) {initht (t);
Sort (T);
int m = m;
for (int j=0; j<n; j + +) cout<<t[j].chr<<endl;
if ((n-k)% (K-1)! = 0) m++;
for (int i=n; i<m; i++) {t[i].weight = Selectmin (T, i);
cout<<t[i].weight<<endl; } for (int j=0; j<m;
J + +) cout<<t[j].weight<<endl;
} void Dfs (int x) {int i=0;
while (t[x].children[i]! =-1 && i<k) {q[last++] = i;
DFS (T[x].children[i]);
i++;
} if (x<n) {cout<<endl<<t[x].chr<< ":";
for (int i=0; i<last; i++) cout<<q[i];
cout<<endl;
} last--;
} int main () {int m = m;
Createhuffmantree (T);
if ((n-k)% (K-1)! = 0) m++;
DFS (M-1);
return 0; }