Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

# kindly note that I want a pseudocode of this code with detailed comments on each step. import heapq from heapq import heappop, heappush #check

# kindly note that I want a pseudocode of this code with detailed comments on each step.

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions