Huffman Encoding Using C

Huffman encoding is a compression technique used to compress data for transmission.This program takes string as input from user and shows its encoded form as output with some intermediate outputs as well. The purpose of it is to reduce the number of bits used for each character for its transmission

Here is the code..

The Code

The code explains itself.To make it easier, i have documented the code well.

Code: C

/* HUFFMAN ENCODING Implementation in C *//* Implemented By : N SUDHAKAR IIIrd year BTECH *//*The program takes a string from user as input and encodes it for transmission*/

/*STRUCTURES*/struct tree //The structure is used to build the binary tree.{int data; //Variable 'data' is used to represent the frequency of each characterint trav_val; //trav_value takes 0 if the node is traversed to the right or takes 1 if it is traversed to the leftchar ckey; //This is the character assigned to the leaf node tree *rlink; //Right child tree *llink; //Left child}**root=NULL,*temp;

struct calc_sum //The structure is used to store the sum of two min values{int sum; //Sum is the total valueint right; //right is the biggest of the two min valuesint left; //left is the smallest of the two min values}*total;

structstring//The structure is used to assign the frequency of each character{char alph; //alph is the characterint freq; //freq is the frequency of the character}*ori_string,*pre_string; /*ori_string is the original string(containing freq and char of each character entered by user) and pre_string is the preserved string*/

struct encode //The structure is used to assign the Huffman's code to different characters{char letter; //This is the character assigned to the leaf nodeint *code; //It contains the Huffman's code for the character int length; //Contains the length of the Huffman's code}*enc;

/*GLOBAL VARIABLES*//*i,j,k,m are loop variables next_char is the no of different characters in the input string str_len is the length of the input string ch is used to drive the menu input is the input string*/int next_char,str_len;staticint *trav,m,root_check,i,j,k;char ch,*input;