Displaying Source Code(s)
|
|
TEXT COMPRESSION USING HUFFMAN CODES.
--------------------------------------------------------------------------------
Description : The project provides the way of compressing the
repeated text
size by using concept of huffman encoding technique.
/* TEXT COMPRESSION - HUFFMAN CODING */
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define MAXSUM 50
#define MAXNODES 100
#define MAXLEN 15
enum direction{left,right};
enum boolean{false,true};
typedef class node*nodeptr;
typedef short int symbolindex;
typedef short int listptr;
nodeptr root;
class symbol
{
public:
char c;
int count;
nodeptr ptr;
operator int()
{
return(count);
}
};
class node
{
public:
int weight;
nodeptr lc,rc;
nodeptr parent;
direction dir;
boolean isleaf;
symbolindex index;
node()
{
lc=NULL;
rc=NULL;
parent=NULL;
weight=0;
isleaf=false;
index=0;
}
node(symbolindex i,symbol &s)
{
index=i;
weight=s.count;
lc=NULL;
rc=NULL;
parent=NULL;
isleaf=true;
s.ptr=this;
}
int getindex()
{
if(isleaf)
return(index);
else
return(0);
}
void insertright(nodeptr n)
{
n->parent=this;
n->dir=right;
rc=n;
isleaf=false;
}
void insertleft(nodeptr n)
{
n->parent=this;
n->dir=left;
lc=n;
isleaf=false;
}
};
class nodelist
{
private:
listptr start,avail,prev;
int cnt;
nodeptr info[MAXNODES];
listptr link[MAXNODES];
public:
nodelist();
void add(nodeptr n);
nodeptr least();
boolean isempty()
{
return(cnt==0)?true:false;
}
};
nodelist::nodelist()
{
int i;
start=1;
link[start]=0;
info[start]=NULL;
avail=2;
cnt=0;
for(i=2;i<MAXNODES;i++)
{
link[i]=i+1;
}
}
void nodelist::add(nodeptr n)
{
listptr ptr=link[start];
prev=start;
listptr temp=avail;
avail=link[avail];
info[temp]=n;
while((ptr!=0 && (info[ptr]->weight<info[temp]->weight)))
{
prev=ptr;
ptr=link[ptr];
}
link[prev]=temp;
link[temp]=ptr;
cnt++;
}
nodeptr nodelist::least()
{
listptr temp=link[start];
cnt--;
if(temp==0)
return(NULL);
link[start]=link[temp];
link[temp]=avail;
avail=temp;
return(info[temp]);
}
class symtab
{
private:
symbol sym[MAXSUM];
public:
int count;
symtab()
{
count=0;
}
int find(char s);
symbol & operator[](int index)
{
return(sym[index]);
}
void add(char c,int n);
char*getcode(int index);
};
int symtab::find(char s)
{
int i;
for(i=0;i<count;i++)
if(sym[i].c==s) return i;
return(-1);
}
void symtab::add(char c,int n)
{
symbol s;
s.count=n;
s.c=c;
sym[count++]=s;
}
char *symtab::getcode(int index)
{
int pos=MAXLEN;
static char buffer[MAXLEN+1];
buffer[pos]='
-------------------------------------------------------------------------------- |
|
|