0% found this document useful (0 votes)
12 views1 page

Steps of Huffman Encoding:: Calculate The Frequency of Each Character Build A Priority Queue Build A Binary Tree

Uploaded by

mohanbarhate700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Steps of Huffman Encoding:: Calculate The Frequency of Each Character Build A Priority Queue Build A Binary Tree

Uploaded by

mohanbarhate700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

10/17/24, 11:25 AM ChatGPT

ChatGPT

Explain Huffman Encoding with suitable example

Huffman Encoding is a widely used algorithm for data compression, which reduces the size of the data without losing any information. It is based on the frequency of characters in the data and uses variable-length codes for encoding the characters. The characters that
occur more frequently are assigned shorter codes, while less frequent characters are assigned longer codes.

Steps of Huffman Encoding:


1. Calculate the frequency of each character in the given input data (e.g., a text string).

2. Build a priority queue (or a min-heap) where each node represents a character and its frequency. The node with the smallest frequency has the highest priority.

3. Build a binary tree using the priority queue:

Extract the two nodes with the smallest frequencies from the queue.

Create a new node with these two nodes as children and a frequency equal to the sum of their frequencies.

Insert the new node back into the priority queue.

Repeat this process until only one node remains in the queue (this node becomes the root of the Huffman Tree).

4. Generate the Huffman Codes by traversing the tree:

Assign 0 to the left edge and 1 to the right edge of each node.

The code for each character is the sequence of 0 s and 1 s formed by the path from the root to the character's node.

5. Encode the data using the generated Huffman codes.

6. Decode the data by using the Huffman Tree to map the encoded binary string back to the original characters.

Example:
Let's consider an example with the following characters and their frequencies:

Character Frequency

A 5

B 9

C 12

D 13

E 16

F 45

Step 1: Build the Huffman Tree

1. Start by creating leaf nodes for each character and add them to a priority queue (min-heap) based on frequency.

2. The two smallest frequencies are A (5) and B (9) . Create a new internal node with a frequency of 5 + 9 = 14 , and make A and B its children.

3. Insert the new node back into the priority queue. Now, the smallest frequencies are C (12) and D (13) . Create a new internal node with a frequency of 12 + 13 = 25 , and make C and D its children.

4. Continue this process:

Combine E (16) and the internal node (A+B) (14) to form a new node with frequency 30 .

Combine F (45) and the internal node (C+D) (25) to form a new node with frequency 70 .

Finally, combine the two remaining nodes (E+(A+B)) (30) and (F+(C+D)) (70) to form the root node with frequency 100 .

The resulting Huffman Tree looks like this:

scss Copy code

(100)
/ \
(30) (70)
/ \ / \
(14) (16) (25) (45)
/ \ / \
A B C D

Step 2: Assign Huffman Codes

Traverse the tree to assign the binary codes:

A = 000

B = 001

C = 010

D = 011

E = 10

F = 11

Step 3: Encoding

Suppose the input data is ABCD . Using the generated Huffman codes:

A = 000

B = 001

C = 010

D = 011

The encoded data is: 000001010011 .

Step 4: Decoding

To decode 000001010011 , you use the Huffman Tree:

000 → A

001 → B

010 → C

011 → D

Decoded string: ABCD .

Advantages of Huffman Encoding:


It provides an optimal way of compressing data when the frequency of characters is known.

N t h t h th d i bi d di (thi i k fi d )
Message ChatGPT

ChatGPT can make mistakes. Check important info.

https://chatgpt.com/c/6710a6ab-8298-8005-b6fe-6436cf256770 1/1

You might also like