Advertisement
brandblox

ImgLab-1-(06-11-24)

Nov 5th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1.  
  2. # 1. Node class: each node in the huffman tree is represented by a Node class . A node can be eather a leaf(containg a symbol)
  3. # or an internal node with left and right children
  4.  
  5. # 2. Building a huffman tree: Here we use a min heap to build a huffman tree, we repeatedly combine the two nodes with the smallest
  6. # smallest frequency unitil only one node remains, which is the root of the huffman tree.
  7.  
  8. # 3. Generating codes: Starting form the root, we assign "0" for left branches and "1" for right branches, building a binary code for each symbol.
  9. # these codes are stored in a dictionary called codelook.
  10.  
  11. # 4. Encoding and decoding: we encode the data by replacing each symbol with its binary code from the codebook.
  12. # decoding involves traversing the huffman tree according to each bit in the encoded data to tetrive the original symbols.
  13.  
  14.  
  15. from collections import Counter
  16. import heapq
  17.  
  18.  
  19.  
  20. class Node:
  21.     def __init__(self, symbol=None, freq=0, left=None, right=None):
  22.         self.freq = freq
  23.         self.symbol = symbol
  24.         self.left = left
  25.         self.right = right
  26.  
  27.     def __lt__(self, other):
  28.         return self.freq < other.freq
  29.  
  30.  
  31. def build_huffman_tree(data):
  32.     # Count the frequency of each symbol in the data
  33.     frequency = Counter(data)
  34.  
  35.     # Create a priority queue (min-heap) for the nodes
  36.     heap = [Node(symbol, freq) for symbol, freq in frequency.items()]
  37.     heapq.heapify(heap)
  38.  
  39.     # Build the Huffman tree
  40.     while len(heap) > 1:
  41.         left = heapq.heappop(heap)
  42.         right = heapq.heappop(heap)
  43.         merged = Node(freq=left.freq + right.freq, left=left, right=right)
  44.         heapq.heappush(heap, merged)
  45.  
  46.     # The remaining node is the root of the tree
  47.     return heap[0]
  48.  
  49.  
  50. def generate_huffman_codes(node, prefix="", codelook={}):
  51.     if node.symbol is not None:
  52.         codelook[node.symbol] = prefix
  53.     else:
  54.         # Traverse left (append '0') and right (append '1')
  55.         generate_huffman_codes(node.left, prefix + '0', codelook)
  56.         generate_huffman_codes(node.right, prefix + '1', codelook)
  57.     return codelook
  58.  
  59.  
  60. def huffman_encoding(data, codelook):
  61.     # Generate the encoded string based on the Huffman codes
  62.     return ''.join(codelook[symbol] for symbol in data)
  63.  
  64.  
  65. def huffman_decoding(encoded_data, root):
  66.     # Decode the encoded data back to the original string
  67.     decoded_data = []
  68.     node = root
  69.     for bit in encoded_data:
  70.         node = node.left if bit == '0' else node.right
  71.         if node.symbol is not None:
  72.             decoded_data.append(node.symbol)
  73.             node = root
  74.     return ''.join(decoded_data)
  75.  
  76.  
  77. # Test with sample data
  78. data = "We are the student of amity"
  79. root = build_huffman_tree(data)
  80. codelook = generate_huffman_codes(root)
  81. encoded_data = huffman_encoding(data, codelook)
  82. decoded_data = huffman_decoding(encoded_data, root)
  83.  
  84. print("Encoded Data:", encoded_data)
  85. print("Decoded Data:", decoded_data)
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement