From 5a0338a55d6ec6b63524041fe6e8851869114659 Mon Sep 17 00:00:00 2001 From: Aditya <109960864+KondeAditya@users.noreply.github.com> Date: Thu, 6 Nov 2025 14:08:06 +0530 Subject: [PATCH] Huffman.py --- DAA2.py | 60 +++++++++++++++++++++++++++------------------------------ 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/DAA2.py b/DAA2.py index ae41a7a..147cace 100644 --- a/DAA2.py +++ b/DAA2.py @@ -1,42 +1,38 @@ -import heapq -import itertools +def build_tree(freq): + nodes = [[freq[ch], ch] for ch in freq] # each node = [weight, data] + while len(nodes) > 1: + nodes = sorted(nodes, key=lambda x: x[0]) + left = nodes.pop(0) + right = nodes.pop(0) + new = [left[0] + right[0], [left, right]] # merge + nodes.append(new) + return nodes[0] -def build_huffman_tree(freq): - heap = [] - counter = itertools.count() # unique sequence count - for ch, weight in freq.items(): - heap.append([weight, next(counter), [ch]]) # add counter as second element - heapq.heapify(heap) - while len(heap) > 1: - low = heapq.heappop(heap) - high = heapq.heappop(heap) - combined_weight = low[0] + high[0] - combined_node = [low[2], high[2]] - heapq.heappush(heap, [combined_weight, next(counter), combined_node]) - - return heap[0] - -def assign_codes(node, prefix="", codebook={}): - # node is [char] for leaf, or [left, right] for internal node - if len(node) == 1 and isinstance(node[0], str): - codebook[node[0]] = prefix +def assign(node, code="", table={}): + if isinstance(node[1], str): # leaf + table[node[1]] = code else: - assign_codes(node[0], prefix + "0", codebook) - assign_codes(node[1], prefix + "1", codebook) - return codebook + left, right = node[1] + assign(left, code+"0", table) + assign(right, code+"1", table) + return table -def huff_en(txt): + +def huffman(text): + # freq count freq = {} - for c in txt: + for c in text: freq[c] = freq.get(c, 0) + 1 - root = build_huffman_tree(freq) - codes = assign_codes(root[2]) # Note: root[2] because of tie-breaker + # build + assign + root = build_tree(freq) + codes = assign(root) - print("Huffman Codes:") + # print for ch in sorted(codes): - print(f"{ch}: {codes[ch]}") + print(ch, ":", codes[ch]) -txt="abbbbbbcccddddddddee" -huff_en(txt) \ No newline at end of file + +txt = "abbbbbbcccddddddddee" +huffman(txt)