Skip to content

Commit 85e3fee

Browse files
authored
Merge pull request #279 from ASHMITA-DE/master
Create Huffman_Coding.py
2 parents 7ea7b3f + 868f790 commit 85e3fee

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
string = 'BCAADDDCCACACAC'
2+
3+
4+
# Creating tree nodes
5+
class NodeTree(object):
6+
7+
def __init__(self, left=None, right=None):
8+
self.left = left
9+
self.right = right
10+
11+
def children(self):
12+
return (self.left, self.right)
13+
14+
def nodes(self):
15+
return (self.left, self.right)
16+
17+
def __str__(self):
18+
return '%s_%s' % (self.left, self.right)
19+
20+
21+
# Main function implementing huffman coding
22+
def huffman_code_tree(node, left=True, binString=''):
23+
if type(node) is str:
24+
return {node: binString}
25+
(l, r) = node.children()
26+
d = dict()
27+
d.update(huffman_code_tree(l, True, binString + '0'))
28+
d.update(huffman_code_tree(r, False, binString + '1'))
29+
return d
30+
31+
32+
# Calculating frequency
33+
freq = {}
34+
for c in string:
35+
if c in freq:
36+
freq[c] += 1
37+
else:
38+
freq[c] = 1
39+
40+
freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
41+
42+
nodes = freq
43+
44+
while len(nodes) > 1:
45+
(key1, c1) = nodes[-1]
46+
(key2, c2) = nodes[-2]
47+
nodes = nodes[:-2]
48+
node = NodeTree(key1, key2)
49+
nodes.append((node, c1 + c2))
50+
51+
nodes = sorted(nodes, key=lambda x: x[1], reverse=True)
52+
53+
huffmanCode = huffman_code_tree(nodes[0][0])
54+
55+
print(' Char | Huffman code ')
56+
print('----------------------')
57+
for (char, frequency) in freq:
58+
print(' %-4r |%12s' % (char, huffmanCode[char]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Python Implementation of Huffman Coding using Greedy Approach.
2+
Huffman Coding is a technique of compressing data to reduce its size without loss of data. It is generally useful to compress the data in which there are frequently occurring characters.
3+
It follows a Greedy approach since it deals with generating minimum length prefix-free binary codes.
4+
It uses variable-length encoding scheme for assigning binary codes to characters depending on how frequently they occur in the given text.
5+
Priority Queue is used for building the Huffman tree such that the character that occurs most frequently is assigned the smallest code and the one that occurs least frequently gets the largest code.
6+
It follows this procedure: -
7+
8+
Create a leaf node for each character and build a min heap using all the nodes (The frequency value is used to compare two nodes in min heap)
9+
10+
Repeat Steps 3 to 5 while heap has more than one node
11+
12+
Extract two nodes, say x and y, with minimum frequency from the heap
13+
14+
Create a new internal node z with x as its left child and y as its right child. Also frequency(z)= frequency(x)+frequency(y)
15+
16+
Add z to min heap
17+
18+
Last node in the heap is the root of Huffman tree

0 commit comments

Comments
 (0)