Question: Q) Write only the pseudo code of the following python code. Note that don't add the python code as only pseudo code is required with
Q) Write only the pseudo code of the following python code. Note that don't add the python code as only pseudo code is required with comments.
import heapq from heapq import heappop, heappush #check if node is (root)the top of the tree if the left and right nodes are non existent then it is root def is_Leaf(root): return root.left_node is None and root.right_node is None class Node: def __init__(self, X, frequency, left_node=None, right_node=None): self.X = X self.frequency = frequency self.left_node = left_node self.right_node = right_node def __lt__(self, other): return self.frequency < other.frequency #if node has least frequency, then priority will be higher def string_to_huffman(root, string, Codes): if root is None: return if is_Leaf(root):#if children nodes are present Codes[root.X] = string if len(string) > 0 else '1' string_to_huffman(root.left_node, string + '0', Codes) string_to_huffman(root.right_node, string + '1', Codes) def huffman_to_string(root, index, string): if root is None: return index if is_Leaf(root):#if children nodes are present print(root.X, end='') return index index +=1 root = root.left_node if string[index] == '0' else root.right_node return huffman_to_string(root, index, string) def HUFFMANCODE(string): #checking if string is empty if len(string) == 0: return #making dictionery of frequency of each character from input string frequency={} for i in string: if i not in frequency: frequency[i]=1 #add the character in dictionery else: if i in frequency: frequency[i]+=1 #increase the frequency value of character priorityq = [Node(characters, count) for characters, count in frequency.items()] heapq.heapify(priorityq) #iterate till the last node is left(the root) count=0 while (len(priorityq) != 1): count+=1 print(count) left_node = heappop(priorityq) right_node = heappop(priorityq) total_frequency = left_node.frequency + right_node.frequency heappush(priorityq, Node(None, total_frequency, left_node, right_node)) root = priorityq[0] Codes = {} empty_string="" string_to_huffman(root, empty_string, Codes) print("HUFFMAN CODE LIST:", Codes) print(" ") print("ORIGINAL MESSAGE:", string) print(" ") encoded_string = "" for characters in string: encoded_string += Codes.get(characters) print("ENCODED MESSAGE:", encoded_string) print(" ") print("DECODING THE MESSAGE:", end=' ') print(" ") if is_Leaf(root): while root.frequency > 0: print(root.X, end='') root.frequency -=1 else: index =-1 while index < len(encoded_string) - 1: index = huffman_to_string(root, index, encoded_string) message=input("ENTER MESSAGE TO CONVERT TO HUFFMAN CODE: ") print(" ") HUFFMANCODE(message)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
