0% found this document useful (0 votes)
10 views

huffman tree

A Huffman tree is a binary tree used for Huffman coding, which is a lossless data compression algorithm that encodes characters based on their frequencies. The algorithm involves initializing one-node trees, combining nodes with the smallest frequencies, and traversing the tree to generate binary codes for each character. An example is provided with a five-symbol alphabet and their frequencies, demonstrating how to construct a Huffman code and encode/decode messages.

Uploaded by

Ms. Pavithra D
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

huffman tree

A Huffman tree is a binary tree used for Huffman coding, which is a lossless data compression algorithm that encodes characters based on their frequencies. The algorithm involves initializing one-node trees, combining nodes with the smallest frequencies, and traversing the tree to generate binary codes for each character. An example is provided with a five-symbol alphabet and their frequencies, demonstrating how to construct a Huffman code and encode/decode messages.

Uploaded by

Ms. Pavithra D
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

HUFFMAN TREE

A Huffman tree is a binary tree used in Huffman coding. It is constructed based


on the frequency of characters in the input data.
Huffman coding is a lossless data compression algorithm used to reduce the
size of data by encoding characters with variable-length binary codes based on their
frequencies.
Key Characteristics:
•Each leaf node represents a character and its frequency.
•Each internal node contains the sum of the frequencies of its child nodes.
•The path from the root to a character forms its binary code
ALGORITHM:
Step 1 : Initialize n one-node trees and label them with the symbols of the
alphabet given
Step 2 : Record the frequency of each symbol in its tree’s root
to indicate the tree’s weight.
Step 3 : Select two nodes with the smallest frequencies.Combine them into a new internal
node with a frequency equal to the sum of the two.
Step 4 : Repeat Step 3 until only one node remains.
Generate Codes:
Traverse the tree to assign 0 for left and 1for right to the branches, creating the Huffman
codes.
Consider the five-symbol alphabet {A, B, C, D, E} with the following occurrence
frequencies in a text made up of these symbols:
Symbol Frequency
A 0.35
B 0.1
C 0.2
D 0.2
E 0.15
A B C D E
0.35 0.1 0.2 0.2 0.15

B E C D A
0.1 0.15 0.2 0.2 0.35

C D A
0.25 0.2 0.2 0.35

B E
0.1 0.15
C D A
0.25
0.2 0.2 0.35

B E
0.1 0.15

A
0.4 0.25
0.35

C D B E
0.2 0.2 0.1 0.15

A 0.4
0.25
0.35

B E C D
0.1 0.15 0.2 0.2
0.6
0.4

A C D
0.25 0.2 0.2
0.35

B E
0.1 0.15
1.0
0 1

0.6
0.4
0 1
0 1
A
0.25
C D 0.35
0 1
0.2 0.2
B E
0.1 0.15
symbol A B C D E
frequency 0.35 0.1 0.2 0.2 0.15
codeword 11 100 00 01 101

Hence,DAD is encoded as 011101 and 1001101 is decoded as BAD


With the occurrence frequencies given and the codeword lengths
obtained,the average number of bits per symbol in this code is
*
- binary digits
- frequency
2 *0.35 + 3 *0.1 + 2 *0.2 + 2 *0.2 + 3 *0.15 = 2.25.
1. a. Construct a Huffman code for the following data:
symbol A B C D _
frequency 0.4 0.1 0.2 0.15 0.15

b. Encode ABACABAD using the code of question (a).


c. Decode 100010111001010 using the code of question (a).

You might also like