#include<iostream.h>
#define MAX 20
struct huffinit
{
char data;
int weight;
};
struct huffnode
{
int weight;
int lchild,rchild,parent;
};
struct huffcode
{
char data;
char code[MAX+1];
};
class HuffTree
{
public:
HuffTree(huffinit w[],int n);
void Selete(int &m1,int &m2,int m);
void Output(int n);
void Encode(huffinit w[],int n);
void Decode(char code[]);
private:
huffnode tree[2*MAX-1];
huffcode cd[MAX];
};
HuffTree::HuffTree(huffinit w[],int n)
{
int i1,i2;
for(int i=0;i<2*n-1;i++)
{
tree[i].parent=-1;
tree[i].lchild=-1;
tree[i].rchild=-1;
}
for(i=0;i<n;i++)
tree[i].weight=w[i].weight;
for(int k=n;k<2*n-1;k++)
{
Selete(i1,i2,k);
tree[i1].parent=k;
tree[i2].parent=k;
tree[k].weight=tree[i1].weight+tree[i2].weight;
tree[k].lchild=i1;
tree[k].rchild=i2;
}
}
void HuffTree::Selete(int &m1,int &m2,int m)
{
int i,min1,min2;
int max=tree[0].weight;
for(i=0;i<m;i++)
{
if(max<tree[i].weight)
max=tree[i].weight;
}
min1=max;