Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program in C language to generate Huffman coding for the following Text given: ADAM IS IN MADAMS KITCHEN ( make sure the output

Write a program in C language to generate Huffman coding for the following Text given:
ADAM IS IN MADAMS KITCHEN
(make sure the output is in the exact same order as shown in the testcase in results) Here i have given the eqivalent program in : import heapq
import collections
def huffman_coding(text):
# Frequency of each character
frequency = collections.Counter(text)
# Priority queue to hold the characters and their frequencies
priority_queue =[[weight,[symbol,""]] for symbol, weight in frequency.items()]
heapq.heapify(priority_queue)
# Tree construction
while len(priority_queue)>1:
low = heapq.heappop(priority_queue)
high = heapq.heappop(priority_queue)
for pair in low[1:]:
pair[1]='0'+ pair[1]
for pair in high[1:]:
pair[1]='1'+ pair[1]
heapq.heappush(priority_queue, [low[0]+ high[0]]+ low[1:]+ high[1:])
# Generating Huffman codes
huffman_codes ={}
for pair in heapq.heappop(priority_queue)[1:]:
huffman_codes[pair[0]]= pair[1]
return frequency, huffman_codes
text = "ADAM IS IN MADAMS KITCHEN"
frequency, huffman_codes = huffman_coding(text.replace("",""))
print("FREQUENCY OF ALPHABETS")
specific_order =['A','M','I','D','S','N','K','T','C','H','E']
for char in specific_order:
if char in frequency:
print(f"{char}-{frequency[char]}")
print("HUFFMAN CODE IS")
for char, code in sorted(huffman_codes.items(), key=lambda x: (-len(x[1]), x[1])):
print(f"{char}-{code}")
image text in transcribed

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_2

Step: 3

blur-text-image_3

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

Big Data And Hadoop Fundamentals Tools And Techniques For Data Driven Success

Authors: Mayank Bhushan

2nd Edition

9355516665, 978-9355516664

More Books

Students also viewed these Databases questions