Huffman Coding Software
Transcript of Huffman Coding Software
-
8/9/2019 Huffman Coding Software
1/9
/* HUFFMAN CODING (Mini Project)*/
/*Implemented By :Asif Iqbal,Aquib Rahid Pandit,Kaleem Dar & Sahil Sholla
*/
/*header files:*/
#include#include#include#include#include#include#include/*Global declarations*/int n;char b[94][2];
/* Structure Specifications */
/* Binary Tree Node Specification *//* 'tree' is a Binary Tree, that holds symbol,itsfrequency and pointers to left and right children */
struct tree
{
char a[94];
int s;
struct tree *left,*right;
}*root=NULL,*tt[47]={NULL},*temp,*temp2,*t2,*ri,*le;
/* Priority Queue Node Specification *//* 'pqu'is a priority queue node that holds symbol,itsfrequency and pointer to next node in the queue*/
struct pqu
{
int info;char a[94];struct pqu *ptr;
}*front=NULL,*t,*par,*t1,*p1,*p2;
/* Function Declarations */void welcome();void input();
/* Binary Tree Related Routines */
void insert();
-
8/9/2019 Huffman Coding Software
2/9
void encode(char c[2]);
/* Priority Queue related Routines */void enqu(char a[94],int );struct pqu* fp(int);struct pqu* dequ();
/* Main Logical Routine for Huffman's Encoding Algorithm */void encode(char c[2]);
/* Temporary De-Bugging Functionsvoid disp(struct tree *rt);*//*Name of Routine : fpFunction Call : fp(p) in enquParameters Passed : intReturn Type : VoidPurpose : finds the position for a node in priority queue
*/
struct pqu* fp(int info){
struct pqu *p=NULL;//front points to the first node of priority queuefor(t1=front;t1->infoptr){p=t1;}
return (p);
}
/*Name of Routine : enquFunction Call : enqu(m,g) from inputParameters Passed : char array and intReturn Type : VoidPurpose : inserts a node in priority queue
*/
void enqu(char a[94],int p){
t=(struct pqu*)malloc(sizeof(struct pqu));strcpy(t->a,a);t->info=p;t->ptr=NULL;
//front points to the first node of priority queueif(front==NULL){front=t;}else{
par=fp(p);if(par==NULL){
-
8/9/2019 Huffman Coding Software
3/9
t->ptr=front;front=t;}else{t->ptr=par->ptr;par->ptr=t;
}}
}
/*Name of Routine : dequ()Function Call : dequ() from insertParameters Passed : noneReturn Type : struct pquPurpose : returns the first node in the prioriy queue
*/
struct pqu* dequ(){
t1=front;//front points to the first node of priority queuefront=front->ptr;return t1;
}
/*Name of Routine : encode
Function Call : encode(b[i]) from mainParameters Passed : char arrayReturn Type : voidPurpose : displays the huffman code for a symbol
*/
void encode(char c[2]){
int m=0,i;temp2=root;
while(strcmp(c,temp2->a)!=0){t2=temp2->left;
for(i=0;ia);i++){
if(t2->a[i]==c[0])//symbol found in left child{temp2=temp2->left;m=1;cout
-
8/9/2019 Huffman Coding Software
4/9
coutinfo;p2=dequ();
if(p2==NULL)break;
strcpy(b1,p2->a);strcpy(v1,a1);temp=(struct tree*)malloc(sizeof(struct tree));
strcpy(temp->a,strcat(v1,b1));temp->s=l+p2->info;temp->left=NULL;temp->right=NULL;temp2=temp;root=temp;
//adjusting tiny trees to generate huffman treefor(i=0;ia,a1)==0){temp->left=tt[i];
for(l=i;la,b1)==0){temp->right=tt[i];
for(l=i;l
-
8/9/2019 Huffman Coding Software
5/9
i=0;continue;}
i++;}
if(temp->left==NULL)
{le=(struct tree*)malloc(sizeof(struct tree));strcpy(le->a,a1);le->left=NULL;le->right=NULL;temp2->left=le;}
if(temp->right==NULL){ri=(struct tree*)malloc(sizeof(struct tree));strcpy(ri->a,b1);
ri->left=NULL;ri->right=NULL;temp2->right=ri;}
if(front!=NULL)enqu(temp2->a,temp2->s);//insert composite node in priority queue
tt[z++]=temp2;}
}
/*Name of Routine : dispFunction Call : disp(root) from mainParameters Passed : struct treeReturn Type : voidPurpose : inorder traversal of huffman tree
void disp(struct tree *rt){
if(rt!=NULL){disp(rt->left);coutright);}
}*/
/*Name of Routine : welcomeFunction Call : welcome() from mainParameters Passed : noneReturn Type : void
Purpose : displays the welome message and developer team*/
-
8/9/2019 Huffman Coding Software
6/9
void welcome(){
int driver,mode;driver=DETECT;initgraph(&driver,&mode,"C:\\tc\\bgi");settextstyle(GOTHIC_FONT,0,8);
setcolor(1);outtextxy(60,80,"HUFFMAN");outtextxy(250,220,"CODING");setcolor(15);settextstyle(SMALL_FONT,0,6);outtextxy(120,390,"Developed by:");setcolor(3);settextstyle(SMALL_FONT,0,6);outtextxy(260,390,"Asif,Aquib,Kaleem & Sahil");getch();closegraph();textmode(MONO);
}/*Name of Routine : inputFunction Call : input() from mainParameters Passed : noneReturn Type : voidPurpose : takes the symbol and frequency after validation
*/
void input(){
int i,j,g,fr,z=1,rp=0;char m[2],re,tn[5],tf[5];for(i=0;i
-
8/9/2019 Huffman Coding Software
7/9
{
cout
-
8/9/2019 Huffman Coding Software
8/9
{cout
-
8/9/2019 Huffman Coding Software
9/9
}//main program
void main()
{int i;welcome();input();insert();//disp(root);clrscr();
cout