Você está na página 1de 20

EX.NO : 5 DATE : 17.08.

11

SIDDARTH.R. 10I345

IMPLEMENTATION OF HUFFMAN ALGORITHM AIM


To create a simple implementation of Huffman algorithm program in C++.

ALGORITHM
Step 1: Start Step 2: Define a Class with following Data members: Create a structure named tree with data members ch, fre, code, struct pointer left, struct pointer right. Int index variable. double Pointer variable of type tree. Function members: void create(char*,int*,char*,int)-Used to create the tree. void display(tree*,int)-Used to display the tree. void code(tree*,char*,char*)-Used to display the code.

Step 3: main() function is coded in such a way to do the following operations 1. Enter the set of characters. 2. Enter the word.

SIDDARTH.R. 10I345
3. Code. 4. Exit Step 4: stop

PROGRAM #include<iostream.h> #include<process.h> #include<dos.h> #include<conio.h> #include<ctype.h> #include<string.h> #include<stdio.h> int i,j,m,k; float *fre; class huff { private: struct tree { float fre;

SIDDARTH.R. 10I345 int code; char ch; tree *left; tree *right; }; int index; public: tree *head,**stack[10]; huff() { index =0; head = NULL; } void create(char*,int*,char*,int); void display(tree*,int); void code(tree*,char*,char*); }; void huff::code(tree *y,char x[],char l[])

SIDDARTH.R. 10I345 { tree *tra; float fre_q; char ch_; int index=0; tra = y; for(i=0;i<strlen(x);i++) { index = 0; for(j=0;j<strlen(l);j++) if(x[i]==l[j]) { fre_q = fre[j]; ch_ = x[i]; break; } for(;index!=1;) {

SIDDARTH.R. 10I345 if(tra->fre == fre_q && tra->ch==ch_) { cout<<tra->code; index=1; } else { if(tra->fre==fre_q && tra->ch!=ch_) { cout<<1; index=1; } else if(tra->fre > fre_q && tra->left!=NULL && tra->right>ch!=ch_) { if(tra->left->ch=='.'){ tra = tra->left;

SIDDARTH.R. 10I345 cout<<tra->code;} else tra = tra->left; } else tra=tra->right; } } cout<<" "; } } void huff::display(tree *x,int level) { if(x) { display(x->right,level+1); for(i=0;i<level;i++) cout<<" ";

SIDDARTH.R. 10I345 cout<<x->ch; cout<<"\n"; display(x->left,level+1); } } void huff::create(char a[],int b[],char c[],int no1) { int max=0,m=0,n=0; char cha,*d; tree *new_; float val=0.5,tot=0.0; d = new char[strlen(c)-1]; fre = new float[strlen(c)-1]; for(i=0;i<no1;i++) { if(b[i]>=max) { max = b[i];

SIDDARTH.R. 10I345 cha = a[i]; } } for(i=0;i<strlen(c);i++) { if(c[i]==cha && c[i]!='0') { new_ = new tree; new_->ch = c[i]; fre[i] = val; n = i; new_->left = NULL; new_->right = NULL; new_->fre = val; tot+=val; c[i] = '0'; *stack[index++] = new_; }

SIDDARTH.R. 10I345 } for(i=0,j=0;i<strlen(c);i++) { if(c[i]!='0') { d[j++] = c[i]; } } for(j=0;j<strlen(d)-1;j++) { val/=(float)2; new_ = new tree; new_->ch = d[j]; new_->left = NULL; new_->right = NULL; if(m!=n) fre[m++] = val; else{

SIDDARTH.R. 10I345 m = m+1; fre[m++] = val; } new_->fre = val; tot+=val; *stack[index++] = new_; }

new_ = new tree; new_->ch = d[strlen(d)-1]; new_->left = NULL; new_->right = NULL; new_->fre = 1 - tot; if(m!=n) fre[m] = 1 - tot; else { m++;

SIDDARTH.R. 10I345 fre[m] = 1-tot; } *stack[index] = new_; //Creates a tree now. tree *sam,*sam1; for(;index!=0;) { sam = *stack[index--]; sam1 = *stack[index];

new_ = new tree; new_->fre = sam->fre + sam1->fre; new_->ch = '.'; new_->left = sam; new_->left->code = 0; new_->right = sam1; new_->right->code = 1; *stack[index] = new_;

SIDDARTH.R. 10I345 }

head = *stack[0]; cout<<'\n'; display(head,0); } void main() { char op='y'; char *let,*word,*org; int ch,*dup,no=0,coun; huff h; while(op=='y') { clrscr();

cout<<endl; cout<<"\t\t";

SIDDARTH.R. 10I345 cout<<"-----Huffman Tree-----"; cout<<endl<<endl; cout<<"1.Enter the set of characters"<<endl; cout<<"2.Enter the word"<<endl; cout<<"3.Code"<<endl; cout<<"4.Exit"<<endl; cout<<"Choice:"; cin>>ch; cout<<endl<<endl; switch(ch) { case 1:{ clrscr();

cout<<"\t\t"; cout<<endl; cout<<"-----Enter the character----"<<endl; cout<<"Enter the no.of characters:";

SIDDARTH.R. 10I345 cin>>no; cout<<endl<<endl; let = new char[no]; cout<<"Enter the characters<a-z>:"; cin>>let; break;} case 2:{ clrscr(); int no1=0; cout<<"\t\t"<<endl; cout<<"-----Enter the word-----"; cout<<endl; cout<<"Enter the no.of character in a word:"; cin>>no1;

word = new char[no1]; dup = new int[no1]; org = new char[no1];

SIDDARTH.R. 10I345

cout<<endl; cout<<"Enter the word:"; gets(word); strcpy(org,word);

//Checks for availability of all characters in a word. for(i=0;i<strlen(word);i++) { m=0; for(j=0;j<strlen(let);j++) { dup[i] = 0; if(word[i]!=let[j]) { m++; } }

SIDDARTH.R. 10I345 if(m==strlen(let)) { cout<<endl; cout<<"Oops! Condition violated!!"; delay(2000); exit(1); } }

//Counts the no.of occurences and deletes the dup value for(i=0;i<strlen(word)-1;i++) { coun = 0; for(j=i+1;j<strlen(word);j++) { if(word[i]!='0') { if(word[i]==word[j])

SIDDARTH.R. 10I345 { dup[i] = ++coun; word[j] = '0'; } } } }

h.create(word,dup,let,no1); break;} case 3:{ h.code(h.head,org,let); break; } case 4: exit(1); } cout<<endl<<endl;

SIDDARTH.R. 10I345 cout<<"Do you want to continue<y/n>:"; } } OUTPUT cin>>op;

SIDDARTH.R. 10I345

SIDDARTH.R. 10I345

RESULT
The implementation of Huffman tree program was executed successfully.

Você também pode gostar