You are on page 1of 2

Documentation

The program gives u a MINIMUM VARIANCE Huffman Coding. The Source code is
pretty well documented and doesnt require much more elaboration. However, I will just
clarify a coupla things -

I. I/O Format:
a. First you must enter the no. of lines that ur input will roughly occupy
b. Then enter the string taking care that only the following character-sets are allowed
i. 'a--z'
ii. 'A--Z'
iii. '0-9'
iv. ' ' (SPACE)
v. '\n' (NEWLINE)
c. Sit back while your computer encodes and presents u the requisite code :P

II. ALGORITHM:
The basic concept/Algorithm is :
1. Let ' frequency[ ] ' be a 1D integer Array. "frequency[ i ]" represents the
Frequency of the ith symbol.
Let 'code[ ][ ]' be a 2D Character Array, where : ' code[ i ] ' represents the
Code for the ith symbol.

2. Assume that we have an ASCENDING ordered Singly Linked List (ordered as per Cumulative
Frequency AND Alphabetical Precedence)

3. This Singly Linked List, contains :


a. A pointer ' *btroot ' , that points to the Root of a Binary Tree
b. A Self-Referential pointer ' *next '

4. The SLL is ordered on the basis of Relative Frequencies of Alphabets of the Root of the Binary
Tree pointed by ' *btroot ' & as per the Alphabetical order of Root Nodes of the Binary Tree.

5. The Binary Tree consists of :


a. "DATA" field that inturn consists of :
SymbolNo., the Symbol - Character/String, the Symbol's Frequency
b. 3 Self-Referential Pointers : *left, *right, *father
c. An integer 'son_type', that indicates whether the Given node is a
LEFT/RIGHT Son

6. Let ' position[ ] ' be a 1D pointer array, where " position[ i ] " contains a
Pointer to the Binary Tree Node of the ith sybol.

7. Initially, ALL the Symbols are created as DISTINCT Binary Trees, each with
only a single 'root' element. As they are created, ALL these DISTINCT Binary
Trees are pushed into the Ascendingly Ordered SLL, in such a way that the
Ascending Property is maintained. Also, set : position [ i ] = p, where 'p' is
the Distinct Binary Tree Root just created & inserted.
8. Then,
While ( SLL contains more than 1 element)
{
(a) Remove from the SLL, 2 Min Binary Tree Nodes, say p1 & p2
(Min as per Relative Freq. & Alphabetical Order)

(b) Conjoin the 2 Nodes to form a NEW Node, say p


When this Node is being formed,
Set : p->father=NULL, p->son_type=NOT_A_SON

(c) Set p1 as the LEFT son of p & p2 as the RIGHT son of p

(d) Update the *father pointers of p1 & p2 as : p1->father=p2->father=p


Also, update their son_types as :
p1->son_type=LEFT_SON , p2->son_type=RIGHT_SON

(e) Insert Node 'p' into the SLL


}

7. Remove the Single Node present in SLL & assign it to 'root'.


Here, 'root' represents the Root Node of the Singular Binary Tree, that exists
after a series of Removals, Conjoins & Insertions of Step 6

8. for ( i = ALL the Symbols present in the Text)


{
p = position[ i ];
code [ i ] = NULL BIT STRING;

while ( p NOT EQUAL TO root)


{
if ( p is a LEFT Son, i.e. if p->son_type == LEFT_SON)
code[ i ] = 0 followed by previous code[ i ];
else
code[ i ] = 1 followed by previous code[ i ];

/* Travel Upwards towards Root through ' father ' pointer */


p = p->father;
}
}

9. Display ALL the Frequencies, Sybols & their respective codes

10. STOP

You might also like