#define max 50
struct code{int bits[max];int start;};typedef struct code code;
struct node{int freq;int father;int isleft;};typedef struct node node;
void insert(int,int);
int del(int);
main()
{
code cd,code1[max];
node node1[max*2-1];
int i,k,n,p,p1,p2,root,rootnodes;
char symb,alph[max];
for(i=0;i<max;i++)
alph[i]=' ';
rootnodes=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s""%d",&symb,&node1[i].freq);
insert(rootnodes,i);
alph[i]=symb;
}
for(p=n;p<2*n-1;n++)
{ p1=del(rootnodes);
p2=del(rootnodes);
node1[p1].father=p;
node1[p1].isleft=1;
node1[p2].father=p;
node1[p2].isleft=0;
node1[p].freq=node1[p1].freq+node1[p2].freq;
insert(rootnodes,p);
}
root=del(rootnodes);
for(i=0;i<n;i++)
{ cd.start=max;
p=i;
while(p!=root)
{ cd.start-=1;
if(node1[p].isleft)
cd.bits[cd.start];
else
cd.bits[cd.start]=1;
p=node1[p].father;
}
for(k=cd.start;k<max;k++)
{code1[i].bits[k]=cd.bits[k];
code1[i].start=cd.start;
}
for(i=0;i<n;i++)
{
printf("\n%c \t%d",alph[i],node1[i].freq);
for(k=code1[i].start;k<max;k++)
printf("%d",code1[i].bits[k]);
printf("\n");
}
}
}
This is an incomplete code for implementing a Huffman Code using priority queue.
I have been given this code and i have to write the insert and delete functions
using priority queue.
However ,I am not able to understand somethigs like what is rootnodes here,what are exactly p1,p,p2?
Should i make priority queue with linked list or arrays?
Please help.