0% found this document useful (1 vote)
2K views10 pages

Huffman Coding in C++

This document contains C++ code to implement Huffman coding for text file compression. It defines data structures like a node and linked list to build a Huffman tree from character frequencies and assign codes to each character. The code counts character frequencies, builds the tree by repeatedly combining lowest frequency nodes, assigns codes by traversing the tree, and outputs the codes to a file. It also contains functions for encoding a file using the generated codes and decoding a file.

Uploaded by

Ahmad Farooq
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (1 vote)
2K views10 pages

Huffman Coding in C++

This document contains C++ code to implement Huffman coding for text file compression. It defines data structures like a node and linked list to build a Huffman tree from character frequencies and assign codes to each character. The code counts character frequencies, builds the tree by repeatedly combining lowest frequency nodes, assigns codes by traversing the tree, and outputs the codes to a file. It also contains functions for encoding a file using the generated codes and decoding a file.

Uploaded by

Ahmad Farooq
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 10

/*--------------------------HUFFMAN CODE FOR TEXT FILE COMPRESSION---------------------*/

#include<iostream.h>
#include<fstream.h>
/*-------------------------------GLOBAL VARIABLES DECLARATION--------------------------*/
#define max 20
int array[max];
int i=0;
ofstream myfile("CODES.txt");
/*-----------------------------------NODE STRUCTURE------------------------------------*/
struct node
{
char data;
int frequency;
int code[5];
node * next;
node * left;
node * right;
};
struct codenode
{
int code[10];
char alphabet;
codenode * next;
};
/*--------------------------------LINKED LIST STRUCTURE--------------------------------*/
struct list
{
node * first;
codenode * codefirst;
void initialize()
{
first=NULL;
codefirst=NULL;
}
/*-----------------------------------SORTED INSERTION------------------------------------*/
void insert(int val, char alphabet)
{
node * p;
p= new node;
p->frequency= val;
p->data=alphabet;
p->next= NULL;
p->left=NULL;

p->right=NULL;
if(first==NULL)
{
first=p;
}
else
{
node * curr;
node * prev;
curr=first;
if(curr->next==NULL)
{
if(curr->frequency>=val)
{
p->next=curr;
first=p;
}
if(curr->frequency<val)
{
curr->next=p;
}
}
else if(first->next!=NULL)
{
curr=first;
if(p->frequency<=curr->frequency)
{
p->next=curr;
first=p;
}
if(p->frequency>curr->frequency)
{
curr=first->next;
prev=first;
while(curr->frequency<p->frequency && curr->next!=NULL)
{
curr=curr->next;
prev=prev->next;
}
if(p->frequency<=curr->frequency)
{
p->next=prev->next;
prev->next=p;
}
else if(curr->next==NULL)
{
curr->next=p;}
}
}

}
}
/*-----------------------------------CODE INSERTION------------------------------------*/
void codeinsertion(char letter, int encode[])
{
codenode * p;
p=new codenode;
p->next=NULL;
p->alphabet=letter;
for(int i=0; i<10; i++)
{
if(encode[i]==0 || encode[i]==1)
p->code[i]=encode[i];
else
break;
}
if(codefirst==NULL)
{
codefirst=p;
}
else
{
codenode * search;
search=codefirst;
while(search->next!=NULL)
{
search=search->next;
}
search->next=p;
}
}
void codetraverse()
{
codenode * print;
print=codefirst;
while(print!=NULL)
{
cout<<"Alphabet="<<print->alphabet<<endl;
for(int k=0; k<10; k++)
{
if(print->code[k]==0 || print->code[k]==1)
{
cout<<print->code[k];
}
else
break;
}
print=print->next;
cout<<endl;
}

}
/*-----------------------------------DEL FIRST CASE ------------------------------------*/
bool isemptydelfirst(int &frequency, char &character)
{
node * del1;
if(first==NULL)
{
cout<<"empty list"<<endl;
return true;
}
else
{
del1=first;
frequency=del1->frequency;
character=del1->data;
first=first->next;
delete del1;
return false;
}
}
/*--------------------------INSERTION FROM LINKED LIST TO Q-------------------------------*/
void givetoQ()
{
node * print;
print=first;
while(print->next != NULL)
{
cout<<print->data<<"=="<<print->frequency<<endl;
print = print->next;
}
cout<<print->data<<"=="<<print->frequency<<endl;
}

};
/*-----------------------------------------QUEUE-------------------------------------------*/
struct Queue
{
node * front;
node * rear;

void initial()
{
rear=NULL;
front=NULL;
}
bool isempty()
{
if(rear==NULL && front==NULL)
return true;
else
return false;
}
/*-----------------------------------QUEUE INSERTION------------------------------------*/
void insert (char x)
{
node * p;
p=new node;
p->data=x;
p->frequency=1;
p->next=NULL;
p->left=NULL;
p->right=NULL;
if(rear==NULL)
{
rear=p;
front=p;
}
else
{
if(duality(x)==false)
{
rear->next=p;
rear=p;
}
}
}
/*-----------------------------------QUEUE TRAVERSE------------------------------------*/
void traverse()
{
node * search;
search=front;
if(rear==NULL && front==NULL)
{
cout<<"Empty"<<endl;
}
while(search!=rear)
{

cout<<search->data;
cout<<"
"<<search->frequency<<endl;
search=search->next;
}
cout<<search->data;
cout<<"
"<<search->frequency<<endl;
}

/*-----------------------------------DUALITY CHECK FOR Q------------------------------------*/


bool duality(char input)
{
node * search;
search=front;
int count=0;
while(search!=rear)
{
if(search->data==input)
{
count++;
search->frequency++;
}
search=search->next;
}
if(search->data==input)
{
count++;
search->frequency++;
}
if(count==0)
return false;
else
return true;
}
/*----------------------------------------REMOVE--------------------------------------------*/
void remove(char &x,int &f)
{
node * temp;
temp=front;
x=temp->data;
f=temp->frequency;
if(temp->next!=NULL)
{
front=temp->next;
delete temp;
}
if(temp->next==NULL)
{

delete temp;
front=NULL;
rear=NULL;
}
}
/*-----------------------------------SECOND INSERTION------------------------------------*/
void simpleinsert(int f, char x)
{
node * p;
p=new node;
p->data=x;
p->frequency=f;
p->next=NULL;
p->left=NULL;
p->right=NULL;
if(rear==NULL)
{
rear=p;
front=p;
}
rear->next=p;
rear=p;
}
/*-----------------------------------MAKE TREE FROM QUEUE------------------------------------*/
void maketree()
{
node * start;
start=front;
node * leftnode;
node * rightnode;
node * last;
while(start->next->next!=NULL)
{
node * nyt;
nyt=new node;
nyt->data='*';
leftnode=start;
rightnode=start->next;
nyt->left=leftnode;
nyt->right=rightnode;
nyt->frequency=leftnode->frequency+rightnode->frequency;
cout<<"NYT"<<nyt->frequency<<" contains: "<<leftnode->fr
equency<<rightnode->frequency<<endl;
start=start->next->next;
cout<<"Start:"<<start->data<<endl;
leftnode->next=NULL;

rightnode->next=NULL;
cout<<"Start:"<<start->data<<start->frequency<<endl;
front=start;
if(start->next!=NULL)
{
cout<<start->next->data<<start->next->frequency<
<endl;
cout<<"Start->next!=NULL"<<endl;
while(nyt->frequency >= start->next->frequency)
{
{
cout<<"Loop Worked"<<endl;
cout<<"Start:"<<start->data<<start->fre
quency<<endl;
start=start->next;
cout<<"Start Updated"<<start->data<<sta
rt->frequency<<endl;
}
if(start->next==NULL)
break;
}
}
cout<<"Now Start:"<<start->data<<endl;
nyt->next=start->next;
start->next=nyt;
start=front;
cout<<"AT THE END OF THIS SHOW: "<<endl;
cout<<"START:"<<start->data<<start->frequency<<endl;
cout<<"NYT:"<<nyt->frequency<<" SIBLINGS:"<<nyt->left->d
ata<<" "<<nyt->right->data<<endl;
last=nyt;
}
node * nyt;
nyt=new node;
nyt->data='*';
if(last->frequency>start->frequency)
{
nyt->right=last;
nyt->left=start;
}
else
{
nyt->right=start;
nyt->left=last;
}
nyt->frequency=last->frequency+start->frequency;
front=rear=nyt;
}
/*-----------------------------------GENERATING CODES------------------------------------*/

void treetraverse(node *root)


{
cout<<"Started: The Traversal"<<endl;
if(root!=NULL)
{
cout<<"ROOT!=NULL"<<endl;
array[i]=0;
i++;
treetraverse(root->left);
i--;
codenode * p;
p=new codenode;
for(int j=0;j<i;j++)
{
cout<<"weight "<<array[j]<<" ";
myfile<<array[j];
cout<<"CODE BEING SAVED TO THE LIST : IS : : "<<
endl;
p->code[j]=array[j];
cout<<"p->code["<<j<<"]="<<p->code[j]<<endl;
}
cout<<endl;
myfile<<"\t"<<root->data<<endl;
p->alphabet=root->data;
cout<<root->data<<endl;
array[i]=1;
i++;
treetraverse(root->right);
i--;
}
}
/*-----------------------------------CREATE ENCODED FILE------------------------------------*/
void encodefile()
{
}
/*-----------------------------------DECODE TREE CREATION------------------------------------*/
void decodetree()
{
}
};

void main()
{
char intake,m;
char *buff;

int f;
Queue k;
list l;
l.initialize();
k.initial();
ifstream myfile("input.txt");
if(!myfile)
{
cout<<"can't load"<<endl;
}
else
{
while(myfile>>intake)
//while (!myfile.eof())
{
//myfile.read(buff, 1);
//k.insert((char)buff);
k.insert(intake);
}
}
while(k.isempty()!=true)
{
k.remove(m,f);
cout<<"Char="<<m<<"\tF="<<f<<endl;
l.insert(f,m);
}
node * point;
point=l.first;
while(point!=NULL)
{
k.simpleinsert(point->frequency,point->data);
point=point->next;
}
node * tree;
tree=k.front;
k.traverse();
k.maketree();
cout<<"TREE CREATED"<<endl;
k.traverse();
cout<<"TRAVERSE"<<endl;
k.treetraverse(k.front);
cout<<"Code traverse"<<endl;
l.codetraverse();
}

You might also like