To implement Huffman coding to compress the data using Python.
- Anaconda - Python 3.7
Get the input string.
Create the required tree nodes.
Function to implement the huffman coding.
Calculate frequency of occurence.
Print the characters and its huffman code.
# Get the input String
class NodeTree(object):
def __init__(self,left=None,right=None):
self.left = left
self.right = right
def children(self):
return (self.left,self.right)
# Create tree nodes
def huffman_code_tree(node,left=True,binString=''):
if type(node) is str:
return {node: binString}
(l, r) = node.children()
d = dict()
d.update(huffman_code_tree(l, True, binString + '0'))
d.update(huffman_code_tree(r, False, binString + '1'))
return d
# Main function to implement huffman coding
freq = {}
for c in string:
if c in freq:
freq[c] += 1
else:
freq[c] = 1
freq = sorted(freq.items(),key=lambda x: x[1], reverse=True)
nodes = freq
# Calculate frequency of occurrence
while len(nodes)>1:
(key1, c1) = nodes[-1]
(key2, c2) = nodes[-2]
nodes = nodes[:-2]
node = NodeTree(key1,key2)
nodes.append((node,c1+c2))
nodes = sorted(nodes, key=lambda x: x[1], reverse=True)
# Print the characters and its huffmancode
huffmanCode = huffman_code_tree(nodes[0][0])
print(' Char | Huffman code ')
print('---------------------')
for (char, frequency) in freq:
print(' %-4r | %12s' % (char, huffmanCode[char]))
![image](https://private-user-images.githubusercontent.com/93427376/242174743-d35a1207-891a-4b01-9e22-37b3aac69948.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTk5MzY1NzksIm5iZiI6MTcxOTkzNjI3OSwicGF0aCI6Ii85MzQyNzM3Ni8yNDIxNzQ3NDMtZDM1YTEyMDctODkxYS00YjAxLTllMjItMzdiM2FhYzY5OTQ4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MDIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzAyVDE2MDQzOVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWU4MzI2YjRiODExZWM2ZDViOGY0MzhkN2IzMzRjNTA0NmIyNjQ0YWZlYzVlYzA5OGU3ZWFiMGI1MzBiY2FmNzMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.nhdaxRDjYK6FXHvErEK9-yIvghTi8eejaDRKa-1bedQ)
Thus the huffman coding was implemented to compress the data using python programming.