#include "iostream.h" struct element { int weight; int lchild,rchild,parent; }; int i1, i2; void Select(element huffTree[]){ int small,ssmall; small=ssmall=huffTree[0].weight; for(int i=1;huffTree[i].weight!=-1&&huffTree[i].parent!=-1;i++){ if(huffTree[i].weight<small)> small=huffTree[i].weight; if(huffTree[i].weight<small&&hufftree[i].weight>small) ssmall=huffTree[i].weight; } i1=small; i2=ssmall; } void HuffmanTree(element huffTree[],int w[],int n){ for(int i=0;i<2*n-1;i++){ huffTree[i].lchild=-1; huffTree[i].parent=-1; huffTree[i].rchild=-1; } for(int k=0;k<n;k++)> huffTree[i].weight=w[i]; for(int m=n;m<2*n-1;m++){ Select(huffTree); huffTree[i1].parent=k; huffTree[i2].parent=k; huffTree[k].weight=huffTree[i2].weight+huffTree[i1].weight; huffTree[k].lchild=i1; huffTree[k].rchild=i2; } } void main(){ int w[5]={1,2,3,4,5}; element a[9]; HuffmanTree(a,w,5); for(int i=0;i<9;i++) cout<<a[2].weight<<endl; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)