Sie sind auf Seite 1von 9

/* HUFFMAN CODING (Mini Project)*/

/*
Implemented By :
Asif Iqbal,Aquib Rahid Pandit,Kaleem Dar & Sahil Sholla
*/
/*header files:*/
#include<iostream.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#include<ctype.h>
#include<graphics.h>
/*Global declarations*/
int n;
char b[94][2];
/* Structure Specifications */
/* Binary Tree Node Specification */
/* 'tree' is a Binary Tree, that holds symbol,its
frequency 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,its
frequency 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();
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 Functions
void disp(struct tree *rt);
*/
/*
Name of Routine : fp
Function Call : fp(p) in enqu
Parameters Passed : int
Return Type : Void
Purpose : 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 queue
for(t1=front;t1->info<info&&t1!=NULL;t1=t1->ptr)
{
p=t1;
}
return (p);
}

/*
Name of Routine : enqu
Function Call : enqu(m,g) from input
Parameters Passed : char array and int
Return Type : Void
Purpose : 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 queue
if(front==NULL)
{
front=t;
}
else
{
par=fp(p);
if(par==NULL)
{
t->ptr=front;
front=t;
}
else
{
t->ptr=par->ptr;
par->ptr=t;
}
}
}

/*
Name of Routine : dequ()
Function Call : dequ() from insert
Parameters Passed : none
Return Type : struct pqu
Purpose : returns the first node in the prioriy queue
*/
struct pqu* dequ()
{
t1=front;
//front points to the first node of priority queue
front=front->ptr;
return t1;
}
/*
Name of Routine : encode
Function Call : encode(b[i]) from main
Parameters Passed : char array
Return Type : void
Purpose : 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;i<strlen(t2->a);i++)
{
if(t2->a[i]==c[0])//symbol found in left child
{
temp2=temp2->left;
m=1;
cout<<"0";
break;
}
}
if(m!=1)//symbol present in right child
{
temp2=temp2->right;
cout<<1;
}
m=0;
}
}

/*
Name of Routine : insert
Function Call : insert() from main
Parameters Passed : none
Return Type : void
Purpose : inserts a node in the huffman tree
*/
void insert()
{
char a1[94],b1[94],v1[94];
int i,j,z=0,l;
while(front!=NULL)
{
p1=dequ();
strcpy(a1,p1->a);
l=p1->info;
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 tree
for(i=0;i<z;)
{
if(strcmp(tt[i]->a,a1)==0)
{
temp->left=tt[i];
for(l=i;l<z;l++)
{
tt[l]=tt[l+1];
}
i=0;
continue;
}

else if(strcmp(tt[i]->a,b1)==0)
{
temp->right=tt[i];
for(l=i;l<z;l++)
{
tt[l]=tt[l+1];
}
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 : disp
Function Call : disp(root) from main
Parameters Passed : struct tree
Return Type : void
Purpose : inorder traversal of huffman tree

void disp(struct tree *rt)


{
if(rt!=NULL)
{
disp(rt->left);
cout<<rt->a;
disp(rt->right);
}
}
*/
/*
Name of Routine : welcome
Function Call : welcome() from main
Parameters Passed : none
Return Type : void
Purpose : displays the welome message and developer team
*/
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 : input
Function Call : input() from main
Parameters Passed : none
Return Type : void
Purpose : 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<=94;i++)
for(j=0;j<=2;j++)
b[i][j]='\0'; //initializing that array
while(z) //validating total no. of symbols
{
clrscr();
cout<<"\n\nEnter the total no of characters : ";
cin>>tn;
if (isdigit(tn[0]))
n = atoi (tn);
else
{
cout<<"invalid no,please enter postive integers only";
getch();
continue;
}
if (n==0)
{
cout<<"invalid no,please enter a non-zero value";
getch();
continue;
}

if (n>94)
{
cout<<"invalid no, please enter a value less than 94";
getch();
continue;
}
z=0;
} //while loop closed, no. symbols validated

if(n==1)
{
cout<<"\n\nEnter the character : ";
cin>>m;
cout<<"The corresponding code is:\n\n"<< m<<"==> 1\n";
getch();
exit(0);//bypassing huffman coding algorithm
}
else
{
for(i=0;i<n;i++)
{
cout<<"Enter the character "<<i+1<<": ";
cin>>m;
if( strlen(m)>1)
{
cout<<"please enter one symbol only\n";
i--;
getch();
continue;// get character again
}
else
{
for(j=0;j<n;j++)
{
if (strcmp(m,b[j])==0)
{
cout<<"already been entered\n";
rp=1;
getch();
break;//symbol found exit from this loop
}
}
if(rp==1)//symbol already present
{
i--;
rp=0;
continue;//get symbol again
}
}

strcpy(b[i],m);

fr=1;
while(fr) //validating frequency of symbol
{
cout<<"Enter frequency for "<<m<<" : ";
cin>>tf;
if (isdigit(tf[0]))
{
g= atoi (tf);
fr=0;
}
else
{
cout<<"invalid no,please enter postive integers
only\n";
fr=1;
}

if (g==0)
{
cout<<"please enter a non-zero value";
fr=1;
}
}//validation 4 freq. closed

enqu(m,g);

}//for loop closed


}//closing else
}
//main program

void main()
{
int i;
welcome();
input();
insert();
//disp(root);
clrscr();
cout<<"\n\nThe corresponding codes are...\n\n";
for(i=0;i<n;i++)
{
cout<<b[i] << "==>";
encode(b[i]);
cout<<"\t";
getch();
}

Das könnte Ihnen auch gefallen