|
| 1 | +# Problem name: Huffman Coding |
| 2 | +# Approach: Greedy Method |
| 3 | + |
| 4 | +# ----------------------------------------------------------------------------------------------- |
| 5 | + |
| 6 | +# Problem Statement: Huffman coding is a lossless data compression algorithm. |
| 7 | +# The idea is to assign variable-length codes to input |
| 8 | +# characters, lengths of the assigned codes are based on the |
| 9 | +# frequencies of corresponding characters. The most frequent |
| 10 | +# character gets the smallest code and the least frequent |
| 11 | +# character gets the largest code. |
| 12 | + |
| 13 | +# ----------------------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +# Constraints: |
| 16 | +# chars[] -> set of characters/array of characters. |
| 17 | +# freq[] -> frequency of each of the characters in the given set. |
| 18 | + |
| 19 | +# ----------------------------------------------------------------------------------------------- |
| 20 | + |
| 21 | +# importing the library named as heapq for the implementation of the huffman tree. |
| 22 | +import heapq |
| 23 | + |
| 24 | +# class node defined as the back bone of the node class |
| 25 | +class node: |
| 26 | + def __init__(self, freq, symbol, left=None, right=None): |
| 27 | + # frequency of symbol |
| 28 | + self.freq = freq |
| 29 | + |
| 30 | + # symbol name (character) |
| 31 | + self.symbol = symbol |
| 32 | + |
| 33 | + # node left of current node |
| 34 | + self.left = left |
| 35 | + |
| 36 | + # node right of current node |
| 37 | + self.right = right |
| 38 | + |
| 39 | + # tree direction (0/1) |
| 40 | + self.huff = '' |
| 41 | + |
| 42 | + def __lt__(self, nxt): |
| 43 | + return self.freq < nxt.freq |
| 44 | + |
| 45 | + |
| 46 | +# utility function to print huffman |
| 47 | +# codes for all symbols in the newly |
| 48 | +# created Huffman tree |
| 49 | +def printNodes(node, val=''): |
| 50 | + |
| 51 | + # huffman code for current node |
| 52 | + newVal = val + str(node.huff) |
| 53 | + |
| 54 | + # if node is not an edge node |
| 55 | + # then traverse inside it |
| 56 | + if(node.left): |
| 57 | + printNodes(node.left, newVal) |
| 58 | + if(node.right): |
| 59 | + printNodes(node.right, newVal) |
| 60 | + |
| 61 | + # if node is edge node then |
| 62 | + # display its huffman code |
| 63 | + if(not node.left and not node.right): |
| 64 | + print(" {0} -> {1}".format(node.symbol, newVal)) |
| 65 | + |
| 66 | + |
| 67 | +# characters for huffman tree |
| 68 | +chars = ['a', 'b', 'c', 'd', 'e', 'f'] |
| 69 | + |
| 70 | +# frequency of characters |
| 71 | +freq = [ 5, 9, 12, 13, 16, 45] |
| 72 | + |
| 73 | +print ("-- Huffman Coding using Greedy Method --") |
| 74 | +print () |
| 75 | +print ("Provided input for implementing the Huffman Tree...") |
| 76 | +print ("Characters Frequency") |
| 77 | +print ("---------------------------") |
| 78 | +for k in range (0, len(chars)): |
| 79 | + print (" {0} -> {1}".format(chars[k],freq[k])) |
| 80 | +print () |
| 81 | + |
| 82 | + |
| 83 | +# list containing unused nodes |
| 84 | +nodes = [] |
| 85 | + |
| 86 | +# converting characters and frequencies |
| 87 | +# into huffman tree nodes |
| 88 | +for x in range(len(chars)): |
| 89 | + heapq.heappush(nodes, node(freq[x], chars[x])) |
| 90 | + |
| 91 | +while len(nodes) > 1: |
| 92 | + |
| 93 | + # sort all the nodes in ascending order |
| 94 | + # based on their frequency |
| 95 | + left = heapq.heappop(nodes) |
| 96 | + right = heapq.heappop(nodes) |
| 97 | + |
| 98 | + # assign directional value to these nodes |
| 99 | + left.huff = 0 |
| 100 | + right.huff = 1 |
| 101 | + |
| 102 | + # combine the 2 smallest nodes to create |
| 103 | + # new node as their parent |
| 104 | + newNode = node(left.freq+right.freq, left.symbol+right.symbol, left, right) |
| 105 | + |
| 106 | + heapq.heappush(nodes, newNode) |
| 107 | + |
| 108 | +# Huffman Tree is ready! |
| 109 | +print ("Creating Huffman Tree...\n") |
| 110 | +print ("Your Huffman Tree is ready! Here you go...") |
| 111 | +print () |
| 112 | +print ("Characters Huffman Code") |
| 113 | +print ("-----------------------------") |
| 114 | +printNodes(nodes[0]) |
| 115 | + |
| 116 | + |
| 117 | +# ----------------------------------------------------------------------------------------------- |
| 118 | + |
| 119 | +# Output: |
| 120 | +# -- Huffman Coding using Greedy Method -- |
| 121 | + |
| 122 | +# Provided input for implementing the Huffman Tree... |
| 123 | +# Characters Frequency |
| 124 | +# --------------------------- |
| 125 | +# a -> 5 |
| 126 | +# b -> 9 |
| 127 | +# c -> 12 |
| 128 | +# d -> 13 |
| 129 | +# e -> 16 |
| 130 | +# f -> 45 |
| 131 | + |
| 132 | +# Creating Huffman Tree... |
| 133 | + |
| 134 | +# Your Huffman Tree is ready! Here you go... |
| 135 | + |
| 136 | +# Characters Huffman Code |
| 137 | +# ----------------------------- |
| 138 | +# f -> 0 |
| 139 | +# c -> 100 |
| 140 | +# d -> 101 |
| 141 | +# a -> 1100 |
| 142 | +# b -> 1101 |
| 143 | +# e -> 111 |
| 144 | + |
| 145 | +# ----------------------------------------------------------------------------------------------- |
| 146 | + |
| 147 | +# Code contributed by, Abhishek Sharma, 2022 |
| 148 | + |
| 149 | +# ----------------------------------------------------------------------------------------------- |
0 commit comments