Monday, March 14, 2011

Huffman tree using C++

#include <iostream>
using namespace std;
#include<string.h>
#include<stdlib.h>


class node
{
 public:
            float freq;                      //number of using character
            char ch[10];                   
            node * right;
            node * left;
   node(float x, node *l=NULL, node *r=NULL)
   {
         freq=x;
         right=r;
         left =l;
   }
};
class tree{
      public:
             node *root;
              
      tree()
      {
            root=NULL;
      }
void sort(node *[], float);
node* create(char[], float);
void right(node *[], int);
void assign_code(node*, int [], int);
void del_tree(node *);
};
node* tree::create(char a[], float x)

{
            node* ptr;
            ptr = new node(x);               //allocate a huffmann node
            ptr->freq = x;              
            strcpy( ptr->ch , a);           //save the character inserted to keep a track 
            ptr->right = NULL;
            ptr->left = NULL;
            return(ptr);                   //return the new huffman node 

}
void tree:: sort(node* a[], float n)

{
    int i, j;
    node* temp;
    for (i = 0; i < n - 1; i++)
       for (j = i; j < n; j++)
          if (a[i]->freq > a[j]->freq)           //sort the huffmann nodes according to increasing frequencies
             {
                temp = a[i];
                a[i] = a[j];
                a[j] = temp;
          }
}
void tree::right(node* a[], int n)
{
     int i;
      for (i = 1; i < n - 1; i++)
         a[i] = a[i + 1];                          //shift the array to the right by 1
}
void tree:: assign_code(node* tree1, int c[], int n)
{
       int i;
     if ((tree1->left == NULL) && (tree1->right == NULL))
            {
                  cout<<tree1->ch;                       //output the character
              for (i = 0; i < n; i++)
                      cout<< c[i];                    //output the codes
                   cout<<"\n";
            }
     else
        {
                       
            c[n] = 1;                           //assign 1 to the  left node
            n++;
            assign_code(tree1->left, c, n);
            c[n - 1] = 0;                        //assign o to the right node
            assign_code(tree1->right, c, n);
        }
}
void tree::del_tree(node * root)
{
 if(root!=NULL)                              //delete tree
   {
       del_tree(root->left);
       del_tree(root->right);
            delete root;
  }
}
int main()

{
    tree t;
    node* ptr, * head;
              node* a[12];                 //storing the  created  huffmann nodes
           int i, n, total = 0, u, c[15];   //c[15] to store the code 
            char str[10];             
            float f;
          cout<<"HUFFMANN TREE\n";
          cout<<"\nEnter the no. of letter to be coded:";
          cin>>n;
           for (i = 0; i < n; i++)
             {
                  cout<<"Enter the letter & frequency:";
                    cin>>str>>f;
                    cout<<str<<f;
                  a[i] =t.create(str, f);                 //store the created node to the array
                }

            while (n > 1)
             {
                    t.sort(a, n);
                    u = a[0]->freq + a[1]->freq;                //add 1st two lowest frequencies 
                        strcpy(str,a[0]->ch);
                        strcat(str,a[1]->ch);
                        ptr = t.create(str, u);           //create a huffmann node with this addup freq
                        ptr->right = a[1];             //assign the prev nodes to the right of newly created node
                        ptr->left = a[0];              //assign the prev node to the left of newly created node
                        a[0] = ptr;                   //store in the array at 1st index
                        t.right(a, n);               //shift all other nodes to the right
                        n--;
            }

            t.assign_code(a[0], c, 0);
            t.del_tree(a[0]);
            return 0;

}