Last active
March 25, 2023 18:34
-
-
Save SamirPaulb/74f1788f5b4fe8a532d160b27c72ce08 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment