DAA Lab Exp (3) 9920004660
DAA Lab Exp (3) 9920004660
CSE18R173
Lab Experiment:4
Name:M.Chethan Kumar
Reg.no:9920004660
Section:A14-(N)
***Huffman encoding technique
Aim:-
To write program for Huffman encoding technique using the
following data.
Character Frequency
a 5
b 9
c 12
d 13
e 16
f 45
Algorithm:-
A Build a Huffman Tree from input characters .
Step 1: Create a leaf node for each unique character and build a min
heap of all leaf nodes (Min Heap is used as a priority queue. The
value of frequency field is used to compare two nodes in min heap.
Initially, the least frequent character is at root)
Step 2: Extract two nodes with the minimum frequency from the
min heap.
Step 3: Create a new internal node with a frequency equal to the sum
of the two nodes frequencies. Make the first extracted node as its left
child and the other extracted node as its right child. Add this node to
the min heap.
Step 4: Repeat steps#2 and #3 until the heap contains only one node.
The remaining node is the root node and the tree is complete.
Program:-
# A Huffman Tree Node
class node:
def __init__(self, freq, symbol, left=None, right=None):
# frequency of symbol
self.freq = freq
# frequency of characters
freq = [ 5, 9, 12, 13, 16, 45]
Output screenshot:-
Result:-
Hence, to write a program for Huffman encoding technique using the
above data is done.
*****The End*****