BC 190408018

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

BC190408018

STUDENT NAME: RAHMEEN SHAHBAZ


SUBJECT: CS502

Solution No. 1:
Count the frequency of each character in the given string/sentence "Leaders in Education Technology".

Create a priority queue (min heap) of all characters, where the priority is determined by the frequency
of the character.

Extract the two nodes with the lowest frequency from the priority queue and create a new internal node
with these two nodes as children. The frequency of this new node is the sum of the frequencies of the
children.

Insert the new node back into the priority queue. Repeat steps 3 and 4 until there is only one node left
in the queue. This node is the root of the Huffman binary tree.

Traverse the Huffman binary tree and assign 0 to the left child and 1 to the right child of each internal
node. The leaf nodes represent the characters in the original string/sentence and their corresponding
code is the path from the root to the leaf node.

The variable-length binary codes for each character in the given string/sentence are:

'L': 00

'e': 01

'a': 100

'd': 1010

'r': 1011

's': 1100

'i': 1101

'n': 1110

'E': 1111

'd': 10000

'u': 10001

'c': 10010

'a': 10011

't': 10100
'o': 10101

'T': 10110

'e': 10111

'c': 11000

'h': 11001

'n': 11010

'o': 11011

'l': 11100

'g': 11101

'y': 11110

'(space)': 11111

It should be noted that the specific Huffman tree and codes may vary depending on the specific
frequencies of each character in the given string/sentence.

Using these variable-length codes, the original string/sentence can be encoded to a binary string that
takes up less memory space than using a fixed-length code like ASCII.

Note: The above Huffman codes are just one possible solution, it can be different depending on the
specific implementation of Huffman algorithm.

You might also like