Question
Using Python. Using the pseudocode and basic skeleton code below, implement Huffman code with a priority queue using the helper function HuffmanCode class. No need
Using Python.
Using the pseudocode and basic skeleton code below, implement Huffman code with a priority queue using the helper function HuffmanCode class. No need to make the encoding and decoding functions, just need to construct Huffman code and use it to set T and C.
F is the inputed dictionary of the form produced by get frequencies. T is the root node of the Huffman code tree that would be used for decoding. C is the dictionary that would be used for encoding. The keys of C are all symbols occurring in an input string.
#####################################################################
import heapq
class Internal_Node: def __init__(self, count, children, left=None, right=None): self.count = count self.children = children self.left = left self.right = right def __lt__(self, other): return self.count < other.count
def __eq__(self, other): return self.count == other.count
class HuffmanCode: def __init__(self, F): self.T = None self.T = dict()
######CODE HERE########## # ####Construct the Huffman Code and set C and T#################
#####################################################################
HuffmanCode(F): - Initialize n nodes v_1,...,v_n with v_i.char=c_i and v_i.frequency = F[c_i]. - Insert nodes v_1,...,v_n into the min-priority queue Q. - while Q > 1: - Allocate a new node z. - z.left = x = Q.extract_min() - z.right = y = Q.extract_min() - z.frequency = x.frequency + y.frequency - Q.insert(z) - T = Q.extract_min() - Construct C by running DFS on T. - Return T and C.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started