1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#include<iostream.h>
#include<stdlib.h>
class bnode{ public:
bnode *header;
bnode *lchild;
bnode *rchild;
int freq;
int type;
char ch;
int depth;
char leaf;
};
bnode combine(bnode node_i, bnode node_j)
{
bnode *temp, *root,*newnode;
newnode = new bnode;
(newnode->lchild)=(node_i.header);
cout<<"Address of left child of newnode: "<<newnode->lchild<<endl;
(newnode->rchild)=(node_j.header);
cout<<"Address of right child of newnode: "<<newnode->rchild<<endl;
cout<<"Address of root node: "<<newnode->header<<endl;
return *newnode;
}
void main()
{
const int LEFT_CHILD=0, RIGHT_CHILD=1, ROOT=-1;
int chars,i,j,k,temp1,temp2,min1,min2;
int frequency;
cout<<"how many characters do you have?"<<endl;
cin>>chars;
bnode node[2*chars-1],tempnode, *temp, *root, *newnode;
for(i=0;i<chars;i++)
{
node[i].lchild=NULL;
node[i].rchild=NULL;
}
for(i=0;i<chars;i++)
{
cout<<"Enter symbol:"<<endl;
cin>>node[i].ch;
cout<<"Enter the frequency of the symbol"<<endl;
cin>>node[i].freq;
}
for(i=0;i<chars;i++)
{
for(j=i;j<chars;j++)
{
if(node[i].freq>node[j].freq)
{
tempnode=node[i];
node[i]=node[j];
node[j]=tempnode;
}
}
}
for(i=chars-1;i>=0;i--)
{
cout<<"Symbol: "<<node[i].ch<<" and Frequency: "<<node[i].freq<<endl;
}
for(i=0;i<chars;i++)
{
temp1=node[i].freq;
temp2=node[i].freq;
for(k=i;k<chars+i;k++)
{
{
if(temp1>node[k].freq)
{
temp1=node[k].freq;
min1=k;
}
}
{
if( (temp2>=node[k].freq) && (temp2>temp1) )
{
temp2=node[k].freq;
min2=k;
}
}
}
node[chars+i]=combine(node[min1], node[min2]);
}
}
|