0% found this document useful (0 votes)
124 views5 pages

Huff Man Code

The document contains code for generating Huffman codes from a set of characters and their frequencies. It includes functions to: 1) Set the character array and frequencies from user input. 2) Build a min heap from the character array. 3) Extract the minimum frequency character from the heap. 4) Combine the two lowest frequency characters and insert them into the heap as an internal node to build the Huffman tree. 5) Assign 0/1 Huffman codes by traversing the built tree from leaves to root.

Uploaded by

mundeepihimanshu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
124 views5 pages

Huff Man Code

The document contains code for generating Huffman codes from a set of characters and their frequencies. It includes functions to: 1) Set the character array and frequencies from user input. 2) Build a min heap from the character array. 3) Extract the minimum frequency character from the heap. 4) Combine the two lowest frequency characters and insert them into the heap as an internal node to build the Huffman tree. 5) Assign 0/1 Huffman codes by traversing the built tree from leaves to root.

Uploaded by

mundeepihimanshu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

//Program to generate huffman codes

#include<iostream>
#include<conio.h>
#include<stdio.h>

int freq;
char letter;
node *left,*right,*parent;
node()
{
freq=0;
letter='-';
left=NULL;
right=NULL;
parent=NULL;
node *arr[max];
int arr1[10],arr2[10],arr3[10];
int heapsize,arr_len,count;
huffman()
{
heapsize=0;
arr_len=0;
count=0;
}
void set_array();
void get_array();
void get_count();
void min_heapify(int i);
void buildheap();
node* extractmin();
int parent(int i);
int left(int i);
int right(int i);
void min_heap_insert(node*);
void minheapdecreasekey(int,node*);
void maketree();

//to display number of comparisons


void get_count()
{
cout<<"\nThe number of comparisons is "<<count;
}
//to input array from console
void set_array()
{
cout<<"\nEnter the size of the array";
cin>>arr_len;
for(int i=1;i<=arr_len;i++)
{ arr[i]=new node;
cout<<"\nEnter the letter";
cin>>arr[i]->letter;
cout<<"\nEnter the frequency";
cin>>arr[i]->freq;
}
buildheap();
}
//to display array on console
void get_array()
{ cout<<"\nThe min heap is ";
for(int i=1;i<=arr_len;i++)
{
cout<<"\n"<<arr[i]->letter<<" "<<arr[i]->freq;
}
}
//to find parent of any node
int parent(int i)
{
return i/2;
}
//to find left child of any node
int left(int i)
{
return 2*i;
}
//to find right child of a node
int right(int i)
{
return 2*i+1;
}
//to ensure that heap propert yis satisfied for every node
void min_heapify(int i)
{
int smallest,l,r;
l=left(i);
r=right(i);
if(l<=heapsize && arr[l]->freq<arr[i]->freq)
{ count++;
smallest=l;
}
else smallest= i;
if(r<=heapsize && arr[r]->freq<arr[smallest]->freq)
{
count++;
smallest=r;
}
if(smallest!=i)
{
node *temp;
temp=new node;
temp=arr[smallest];
arr[smallest]=arr[i];
arr[i]=temp;
min_heapify(smallest);
}
}
//to create a min heap of an unsorted array
void buildheap()
{
heapsize=arr_len;
for(int i=heapsize/2; i>=1; i--)
min_heapify(i);
}
node* extractmin()
{
if(heapsize<1)
{
cout<<"heap underflow";
return 0;
}
else
{
node *max1=0;
max1=new node;
max1=arr[1];
arr[1]=arr[heapsize];
heapsize-=1;
min_heapify(1);
return max1;
}
}
//to decrease key value of any node
void minheapdecreasekey(int i, node *z)
{
if(z->freq>arr[i]->freq)
{
count++;
cout<<"\nNEW key greater than current key";
}
else
{
arr[i]->freq=z->freq;
while((i>1)&&(arr[parent(i)]->freq>=arr[i]->freq))
{
count++;
node *temp;
temp=new node;
temp=arr[i];
arr[i]=arr[parent(i)];
arr[parent(i)]=temp;
i=parent(i);
}
}
}
//to insert a particular node
void min_heap_insert(node *z)
{
heapsize=heapsize+1;
arr[heapsize]=new node;
arr[heapsize]->freq=30000;
minheapdecreasekey(heapsize,z);
}
//function to display huffman codes
void maketree()
{ static int j=1;
node *leaf_1=new node;
node *leaf_2=new node;
node *child=new node;
leaf_1=extractmin();
//cout<<"\n"<<leaf_1->freq<<" "<<leaf_1->letter;
leaf_2=extractmin();
// cout<<"\n"<<leaf_2->freq<<" "<<leaf_2->letter;
child->freq=leaf_1->freq+leaf_2->freq;
arr1[j]=child->freq;
child->letter='-';
child->left=leaf_1;
child->right=leaf_2;

leaf_1->parent=child;
leaf_2->parent=child;
cout<<"\n\n"<<child->left->freq<<" : "<<child->left->letter;
cout<<"\n\n"<<child->right->freq<<" : "<<child->right->letter;
arr2[j]=child->left->freq;
arr3[j]=child->right->freq;
j++;
cout<<"\n\n"<<child->freq<<" : "<<child->letter;
min_heap_insert(child);
cout<<"\nArray after min heap insert";
for(int i=1;i<=heapsize;i++)
cout<<"\n"<<arr[i]->freq<<" "<<arr[i]->letter;
if(heapsize==1)
{
for(int k=(arr_len-1);k>=1;k--)
{ if(k==(arr_len-1))
cout<<"\n"<<arr1[k];
cout<<" "<<"\n0 : "<<arr2[k]<<"\n1 : "<<arr3[k];
}
}
}
void main()
{
clrscr();
int select;
do
{
cout<<"\n\nMENU";
cout<<"\n1.ENTER CHARACTERS WITH FREQUENCIES";
cout<<"\n2.DISPLAY";
cout<<"\n3.MAKE HUFFMAN TREE";
cout<<"\n4.EXIT";
cout<<"\nENTER YOUR CHOICE : ";
cin>>select;
switch(select)
{
case 1:obj.set_array();
break;
case 2:obj.get_array();
break;
case 3:while(obj.heapsize!=1)
obj.maketree();
obj.get_count();
break;
case 4:break;
}
}while(select!=4);
getch();
}

You might also like