Question: 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}")
 Write a program in C language to generate Huffman coding for

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!