Professional Documents
Culture Documents
INTRODUCTION
A. Greedy Algorithm
III. IMPLEMENTATION
In the implementation, we try to figure out the Huffman
technique. There are four steps must be accomplished to make
the Huffman technique is fully operated. These phases below
explain the steps of its algorithm.
A. Phase One.
12
*
6
A
6
T
1
U
3
*
20
*
8
I
4
L
6
A
6
K
7
Draw the first two nodes, and release from the table. Make a
new node which will be their parent.
*
4
T
1
U
3
*
4
I
4
L
6
A
6
K
7
*
6
U
3
*
4
I
4
*
6
L
6
A
6
*
4
*
8
* *
12 13
12
*
13
*
20
*
12
*
33
*
13
*
33
*
20
3
3
3
3
3
3
3
12
12
13
12
13
20
12
13
20
33
3
*
8
K
7
K
7
L
6
*
13
B. Phase Two.
*
6
L
6
A
6
K
7
*
8
I
4
*
NewNode^.Freq := tFreq;
NewNode^.ASCII := tASCII;
Tail^.Next := NewNode;
NewNode^.Prev := Tail;
Tail:=NewNode;
Tail^.Next := NIL;
NewNode^.Left := Current;
NewNode^.Right := Current^.Next;
Current^.Parent := NewNode;
Current^.Next^.Parent := NewNode;
break;
end;
InsertN := InsertN^.Next;
end;
Current := Current^.Next;
Current := Current^.Next;
end;
end;
procedure Huffman_Tree;
var
tFreq : byte;
tASCII : char;
InsertN : NodeP;
NewNode : NodeP;
begin
tASCII := '*';
Current := Head;
while Current^.Next <> NIL do
begin
InsertN := Head;
tFreq := Current^.Freq + Current^.Next^.Freq;
Current^.Bit
:= 0;
Current^.Next^.Bit := 1;
while InsertN <> NIL do
begin
if tFreq <= InsertN^.Freq then
begin
new(NewNode);
NewNode^.Freq := tFreq;
NewNode^.ASCII := tASCII;
NewNode^.Next := InsertN;
NewNode^.Prev := InsertN^.Prev;
InsertN^.Prev^.Next := NewNode;
InsertN^.Prev := NewNode;
NewNode^.Left := Current;
NewNode^.Right := Current^.Next;
Current^.Parent := NewNode;
Current^.Next^.Parent := NewNode;
break;
end
else if tFreq > Tail^.Freq then
begin
new(NewNode);
ASCII
Bit
Code
Dec
Freq
Next, Prev,
Parent,
Left, Right
= ^Node;
= record
ASCII :
Bit
:
Code :
Dec
:
freq :
Prev,
Next :
Parent,
Left,
Right :
end;
char;
byte;
string;
byte;
byte;
NodeP;
NodeP;
:
:
:
:
:
The first two nodes must be combined. The first node will be
marked as 0 since it is on the left and the second node will be
marked as 1 since it is one the right. Moreover, both nodes
have the same parent, and the parent has two children, the
nodes. After the parent node is created, it must be inserted into
the linked list by combining the value of the node and the value
of the linked list. The node must be added to the left of the larger
or same value. However, if the parent node is greater than every
node, it has to be inserted after the last node.
C. Phase Three.
In this step, the tree is already structured. It is time to retrieve
the node sign by making a loop until the parent of the last node
is empty (null).
procedure Write_Huffman;
var
result : string;
bit
: string[1];
L
A
K
6
6
7
111
00
01
3
2
2
begin
Current := Head;
repeat
result := '';
Cursor := Current;
Current^.Dec := 0;
Biner := 1;
Bit Code :
11110101 00100111 11010111 00110111 10001101
10011110 00110111 01100000 01110111 10001110
01001111 00011100
begin
Current^.Dec := Current^.Dec +
(Cursor^.Bit * Biner);
Biner := Biner * 2;
str(Cursor^.Bit, Bit);
insert(Bit, result, 1);
end;
Cursor := Cursor^.Parent;
until Cursor^.Parent = NIL;
end;
Decimal Code :
254, 39, 215, 55, 141, 158,
55, 96, 119, 142, 79, 28
Current^.Code := result;
Current := Current^.Next;
until Current^.Next = NIL;
end;
D. Phase Four.
Each node has consisted of code. This phase, everything that
has been coded is converted to a Huffman table. Table 1 shows
the priority of characters. The character K has the most
appearance while the character T has the less one. Characters
with the highest emergence is having the shortest binary code.
Table 1. Huffman Table.
Char
T
U
I
Freq.
1
3
3
3
4
Code
1000
1001
1100
1101
101
18
12
14
96
Bit Len.
4
4
4
4
3
Code Len.
4
12
12
12
12
:
:
:
33
12
(33 12) / 33 * 100 %
63.63636 %