0% found this document useful (0 votes)
53 views6 pages

DAA Lab Exp (3) 9920004660

This document describes a lab experiment to implement Huffman encoding. It includes the aim to write a program for Huffman encoding using sample character frequency data. It then explains the Huffman encoding algorithm which involves building a Huffman tree from the characters by creating nodes and combining the lowest frequency nodes until a single root node remains. The document includes pseudocode for the algorithm and a Python program to traverse the tree and assign codes to characters. The output of running the program on the sample data is displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views6 pages

DAA Lab Exp (3) 9920004660

This document describes a lab experiment to implement Huffman encoding. It includes the aim to write a program for Huffman encoding using sample character frequency data. It then explains the Huffman encoding algorithm which involves building a Huffman tree from the characters by creating nodes and combining the lowest frequency nodes until a single root node remains. The document includes pseudocode for the algorithm and a Python program to traverse the tree and assign codes to characters. The output of running the program on the sample data is displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

“Design and Analysis of Algorithms”

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.

B  Now, Traverse the Huffman Tree and assign codes to


characters.

Program:-
# A Huffman Tree Node
class node:
def __init__(self, freq, symbol, left=None, right=None):
# frequency of symbol
self.freq = freq

# symbol name (character)


self.symbol = symbol

# node left of current node


self.left = left
# node right of current node
self.right = right

# tree direction (0/1)


self.huff = ''

# utility function to print huffman


# codes for all symbols in the newly
# created Huffman tree

def printNodes(node, val=''):


# huffman code for current node
newVal = val + str(node.huff)

# if node is not an edge node


# then traverse inside it
if(node.left):
printNodes(node.left, newVal)
if(node.right):
printNodes(node.right, newVal)

# if node is edge node then


# display its huffman code
if(not node.left and not node.right):
print(f"{node.symbol} -> {newVal}")

# characters for huffman tree


chars = ['a', 'b', 'c', 'd', 'e', 'f']

# frequency of characters
freq = [ 5, 9, 12, 13, 16, 45]

# list containing unused nodes


nodes = []

# converting characters and frequencies


# into huffman tree nodes
for x in range(len(chars)):
nodes.append(node(freq[x], chars[x]))

while len(nodes) > 1:


# sort all the nodes in ascending order
# based on theri frequency
nodes = sorted(nodes, key=lambda x: x.freq)

# pick 2 smallest nodes


left = nodes[0]
right = nodes[1]
# assign directional value to these nodes
left.huff = 0
right.huff = 1

# combine the 2 smallest nodes to create


# new node as their parent
newNode = node(left.freq+right.freq, left.symbol+right.symbol,
left, right)

# remove the 2 nodes and add their


# parent as new node among others
nodes.remove(left)
nodes.remove(right)
nodes.append(newNode)

# Huffman Tree is ready!


printNodes(nodes[0])

Output screenshot:-
Result:-
Hence, to write a program for Huffman encoding technique using the
above data is done.

*****The End*****

You might also like