SlideShare a Scribd company logo
Al-amin Hossain
123 1173 642
North South University
An application of Binary Tree and priority Queue.
Huffman coding is a technique used to compress files
for transmission.
Developed by Dr. David A. Huffman in 1952
“A Method for the Construction of Minimum
Redundancy Codes.”
Application:
Common application is data transmission in
Fax Machines
Encoding-Decoding text.
Leaf nodes are character.
Left Branches are labeled with 0.
Right Branches are labeled with 1.
Following a path from root to leaf
You can get the encoding of the
Character in the leaf.
Like: 101= ‘ I ’
Scan text to be compressed and tally occurrence of all
characters.
Sort or prioritize characters based on number of
occurrences in text.
Build Huffman code tree based on prioritized list.
Perform a traversal of tree to determine all code words.
Scan text again and create new file using the Huffman
codes.
Lets consider this text,
“Eerie eyes seen near lake.”
Count up the occurrences of all characters.
What characters are present ?
E e r i space
y s n a r l k .
Now the Frequency of each character in the text.
Char Freq. Char Freq. Char Freq.
E 1 y 1 k 1
e 8 s 2 . 1
r 2 n 2
i 1 a 2
space 4 l 1
Create Binary tree nodes with character and frequency of
each character.
Place nodes in a priority queue
The lower the occurrence (frequency), the higher the
priority in the queue.
• After inserting all nodes in the queue
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
While priority queue contains two or more nodes
Create new node
Dequeue node and make it left subtree
Dequeue next node and make it right subtree
Frequency of new node equals sum of frequency of left and right
children
Enqueue new node back into queue
• Dequeue 2 lowest frequency nodes
• Lowest will be left child
• Other will be right child
• Sum of the two frequencies will be
the parent.
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
• Enqeue the new created node to the queue
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
• Following the same procedure
two lowest Dequeue creating
new Tree and Enqueue
E
1
i
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
• After Enqueue
E
1
i
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
• Again following the same procedure
Two lowest Dequeue -> New Tree-> Enqueue
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
And Enqueue
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
Two lowest Dequeue -> New Tree-> Enqueue
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
Enqueue
E
1
i
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
Two lowest Dequeue -> New Tree-> Enqueue
E
1
i
1
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
Enqueue
E
1
i
1
n
2
a
2
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
Two lowest Dequeue -> New Tree -> Enqueue
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
E
1
Enqueue
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
Two lowest Dequeue ->
New Tree -> Enqueue
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
Enqueue
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4 4
Two lowest Dequeue ->
New Tree -> Enqueue
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4 4
6
Enqueue
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4 4 6
Two lowest Dequeue ->
New Tree -> Enqueue
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4 6
8
Enqueue
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6 8
Two lowest Dequeue ->
New Tree -> Enqueue
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
Enqueue
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2r
2
s
2
4
n
2
a
2
4 4
6
8 10
Two lowest Dequeue ->New Tree -> Enqueue
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
16
Enqueue 
E
1
i
1
sp
4
e
82
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10 16
Two lowest Dequeue ->New Tree -> Enqueue
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
16
26
After Enqueing this node there is only one node left in priority
queue.
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6
8
10
16
26
And the last Dequeue
We got a Tree 
This tree contains the new code
words for each character.
Freq of root = Num of char in text
Root=26
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6 8
10
16
26
Eerie eyes seen near lake. 26 characters
Encoding the File
• By traversing tree we will get
new code words for each
character
• Going left is a 0
• Going Right is a 1
• Code word is only completed
when a leaf node is reached
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6 8
10
16
26
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6 8
10
16
26
Char Code
E 0000
i 0001
y 0010
l 0011
k 0100
. 0101
Space 011
e 10
r 1100
s 1101
n 1110
a 1111
Char Code
E 0000
i 0001
y 0010
l 0011
k 0100
. 0101
Space 011
e 10
r 1100
s 1101
n 1110
a 1111
Rescan text and encode file
using new code words.
Eerie eyes seen near
lake.
0000101100000110011
1000101011011010011
1110101111110001100
1111110100100101
Now the question is, have we made things any better?
Answer is YES! 
If we use ASCII, it would take 8*26 = 208 bits.
But now after encoding its just 73 bits.
Decoding the File
Read the Frequency table and rebuild the tree.
Read one bit at a time
when you read a bit 1, go to the Right child
when you read a bit 0, go to the Left child
Return to the root and continue reading bits
The tree allows us to easily overcome the challenge of determining the
character boundaries!
• If got 0000
0
0
0
0
Char Code
E 0000
i 0001
y 0010
l 0011
k 0100
. 0101
Space 011
e 10
r 1100
s 1101
n 1110
a 1111
E
1
0000101100000110011
1000101011011010011
1110101111110001100
1111110100100101
Final Tree is looks like that.
E
1
i
1
sp
4
e
8
2
y
1
l
1
2
k
1
.
1
2
r
2
s
2
4
n
2
a
2
4
4
6 8
10
16
26
Thank You
For
Listening

Huffman tree

More Related Content

PPTX
Huffman coding
PPTX
Huffman's Alforithm
PPTX
Huffman's algorithm in Data Structure
PDF
Searching and Sorting Techniques in Data Structure
PDF
Array data structure
ODP
Introduction to Shell script
PPT
PPTX
Huffman Algorithm and its Application by Ekansh Agarwal
Huffman coding
Huffman's Alforithm
Huffman's algorithm in Data Structure
Searching and Sorting Techniques in Data Structure
Array data structure
Introduction to Shell script
Huffman Algorithm and its Application by Ekansh Agarwal

What's hot (20)

PPT
Hashing PPT
PDF
Parse Tree
PPTX
Regular expressions
PDF
Daa notes 1
PPTX
2 phase locking protocol DBMS
PPSX
PPTX
sorting and its types
PPTX
Priority queue in DSA
PPTX
Pumping lemma
PPTX
Input-Buffering
PPTX
Unit I-Data Structures_Intoduction.pptx
PPTX
Command line arguments
PPT
Searching in c language
PPT
Monoalphabetic Substitution Cipher
PDF
Algorithms Lecture 7: Graph Algorithms
PDF
Python Flow Control
PPTX
Restoring & Non-Restoring Division Algorithm By Sania Nisar
PPT
Huffman > Data Structures & Algorithums
PDF
Strings in python
PPTX
Huffman Coding Algorithm Presentation
Hashing PPT
Parse Tree
Regular expressions
Daa notes 1
2 phase locking protocol DBMS
sorting and its types
Priority queue in DSA
Pumping lemma
Input-Buffering
Unit I-Data Structures_Intoduction.pptx
Command line arguments
Searching in c language
Monoalphabetic Substitution Cipher
Algorithms Lecture 7: Graph Algorithms
Python Flow Control
Restoring & Non-Restoring Division Algorithm By Sania Nisar
Huffman > Data Structures & Algorithums
Strings in python
Huffman Coding Algorithm Presentation
Ad

Viewers also liked (20)

PDF
Huffman Encoding Pr
PDF
17 Disjoint Set Representation
PPT
Huffman Tree And Its Application
PPTX
PPTX
Advanced Algorithms #1 - Union/Find on Disjoint-set Data Structures.
PDF
07. disjoint set
PPT
Disjoint sets
PDF
18 Basic Graph Algorithms
PDF
Turing Machine
PPT
Sets and disjoint sets union123
PPTX
Computer architecture
PPT
0 introduction to computer architecture
PPT
Turing machines
PPTX
Set data structure
PPTX
Memory Organization
PPT
Computer architecture
PDF
Graph theory
PDF
Graph theory
PPTX
BASIC COMPUTER ARCHITECTURE
PDF
Memory organization
Huffman Encoding Pr
17 Disjoint Set Representation
Huffman Tree And Its Application
Advanced Algorithms #1 - Union/Find on Disjoint-set Data Structures.
07. disjoint set
Disjoint sets
18 Basic Graph Algorithms
Turing Machine
Sets and disjoint sets union123
Computer architecture
0 introduction to computer architecture
Turing machines
Set data structure
Memory Organization
Computer architecture
Graph theory
Graph theory
BASIC COMPUTER ARCHITECTURE
Memory organization
Ad

Similar to Huffman tree (20)

PPT
Data Structure and Algorithms Huffman Coding Algorithm
PPT
huffman algoritm upload for understand.ppt
PPT
PPT
huffman codes and algorithm
PPT
huffman_nyu.ppt ghgghtttjghh hhhhhhhhhhh
PPT
PPT
Huffman 2
PPT
PPTX
Huffman Codes
PDF
CS-102 Data Structures huffman coding.pdf
PDF
CS-102 Data Structures huffman coding.pdf
PPT
Greedy Algorithms Huffman Coding.ppt
PPTX
Huffman.pptx
PPT
Huffman coding presentation Sukkur iba.ppt
PPT
PPTX
Data structures' project
PPTX
Huffman.Encodingpptx Variable length coding
PPTX
Data compession
PPT
Komdat-Kompresi Data
PPT
4888009.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
Data Structure and Algorithms Huffman Coding Algorithm
huffman algoritm upload for understand.ppt
huffman codes and algorithm
huffman_nyu.ppt ghgghtttjghh hhhhhhhhhhh
Huffman 2
Huffman Codes
CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdf
Greedy Algorithms Huffman Coding.ppt
Huffman.pptx
Huffman coding presentation Sukkur iba.ppt
Data structures' project
Huffman.Encodingpptx Variable length coding
Data compession
Komdat-Kompresi Data
4888009.pptnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn

Recently uploaded (20)

PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Sports Quiz easy sports quiz sports quiz
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Lesson notes of climatology university.
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
GDM (1) (1).pptx small presentation for students
102 student loan defaulters named and shamed – Is someone you know on the list?
Supply Chain Operations Speaking Notes -ICLT Program
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Sports Quiz easy sports quiz sports quiz
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Module 4: Burden of Disease Tutorial Slides S2 2025
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Complications of Minimal Access Surgery at WLH
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Lesson notes of climatology university.
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
human mycosis Human fungal infections are called human mycosis..pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
GDM (1) (1).pptx small presentation for students

Huffman tree