SlideShare a Scribd company logo
2
Most read
3
Most read
5
Most read
Data Structure & Files
Unit 4: Tables
4.2 Huffman’s Algorithm
Ms. Vrushali Dhanokar
Assistant Professor
IT Department
What is Huffman’s Algorithm?
• Lossless data compression algorithm.
• Variable-length code is assigned to input different characters.
• The code length is related to how frequently characters are used.
• Most frequent characters have the smallest codes and longer codes for least frequent characters.
• Complexity for assigning the code for each character according to their frequency is
O(n log n).
• There are mainly two parts:
1. To create a Huffman tree,
2. To traverse the tree to find Huffman codes.
Steps to build Huffman Tree:
Input is an array of unique characters along with their frequency of occurrences
and output is Huffman Tree.
1. Create a leaf node for each unique character and build a min heap of all leaf
nodes (Min Heap is used as a priority queue. The value of frequency field is used
to compare two nodes in min heap. Initially, the least frequent character is at
root).
2. Extract two nodes with the minimum frequency from the min heap.
3. Create a new internal node with a frequency equal to the sum of the two
nodes frequencies. Make the first extracted node as its left child and the other
extracted node as its right child. Add this node to the min heap.
4. Repeat steps#2 and #3 until the heap contains only one node. The remaining
node is the root node and the tree is complete.
Steps to Building Huffman’s Tree:
Let’s understand the algorithm with example:
Example 1:
Character Frequency
a 5
b 9 1. 5+9=14(Internal Node)
c 12 2. 12+13=25(Internal Node)
d 13 3. 14+16=30(Internal Node)
e 16 4. 25+30=55.(Internal Node)
f 45 5. 55+45=100.(Root Node)
Continue..
Step 1: Build Min Heap DS, Which contains 6 Nodes(Given example having 6 Char.) Each node represent as
root.
Step 2: Extract two minimum frequency nodes from min heap. Add a new internal node with frequency
5 + 9 = 14. Now 14 becomes internal root node.
Step 3: Again extract two minimum frequency nodes from heap.
Add a new internal node with frequency
12 + 13 = 25. Now 25 becomes internal root node.
Step 4: Again extract two minimum frequency nodes. Add a new internal node with frequency 14 + 16 = 30.
Now 30 becomes internal root node.
Now min heap contains 3 nodes.
Continue..
Step 5: Extract two minimum frequency nodes.
Add a new internal node with frequency 25 + 30 = 55.
Now min heap contains 2 nodes.
Step 6: Extract two minimum frequency nodes. Add a new internal node with frequency 45 + 55 = 100.
Step 7: Now min heap contains only one node.
Since the heap contains only one node, the algorithm stops here.
Steps to print codes from Huffman Tree:
1. Traverse the tree formed starting from the root.
2. Maintain an auxiliary array.
3. While moving to the left child, write 0 to the array.
4. While moving to the right child, write 1 to the array.
5. Print the array when a leaf node is encountered.
• The codes are as follows:
Characters Code-word
f 0
c 100
d 101
a 1100
b 1101
e 111
Example 2:
Apply Huffman Coding for the word ‘MALAYALAM’. Give Huffman Code for each
symbol.
Important Questions
• By SPPU Exam Pattern
1. Explain Huffman algorithm with example. 4M
1. Construct Huffman Tree and Code with following data
Character A B C D E
Frequency20 10 10 30 30 5M
3. Apply Huffman Coding for the word ‘ENGINEERING’.
Give Huffman Code for each symbol. 6M
Thank You..!

More Related Content

PPTX
Huffman Coding Algorithm Presentation
Akm Monir
 
PPTX
AVL Tree in Data Structure
Vrushali Dhanokar
 
PPTX
Data Flow Diagrams
Reetesh Gupta
 
PPTX
Data structure - Graph
Madhu Bala
 
PPTX
Tree - Data Structure
Ashim Lamichhane
 
PPTX
System and its types
nidhipandey79
 
PPTX
Matrix chain multiplication
Respa Peter
 
PPTX
DFS and BFS
satya parsana
 
Huffman Coding Algorithm Presentation
Akm Monir
 
AVL Tree in Data Structure
Vrushali Dhanokar
 
Data Flow Diagrams
Reetesh Gupta
 
Data structure - Graph
Madhu Bala
 
Tree - Data Structure
Ashim Lamichhane
 
System and its types
nidhipandey79
 
Matrix chain multiplication
Respa Peter
 
DFS and BFS
satya parsana
 

What's hot (20)

PPTX
trees in data structure
shameen khan
 
PPTX
Pipelining and vector processing
Kamal Acharya
 
PPTX
daa-unit-3-greedy method
hodcsencet
 
PPTX
Breadth First Search & Depth First Search
Kevin Jadiya
 
PPT
Data structures using c
Prof. Dr. K. Adisesha
 
PPTX
Divide and conquer - Quick sort
Madhu Bala
 
PPT
Spanning trees
Shareb Ismaeel
 
PPTX
Dag representation of basic blocks
Jothi Lakshmi
 
PPTX
Input-Buffering
Dattatray Gandhmal
 
PPT
Binary Search
kunj desai
 
PDF
Code generation in Compiler Design
Kuppusamy P
 
PPTX
Timing and control
chauhankapil
 
PPTX
Data Structures : hashing (1)
Home
 
PPT
Heaps
Hafiz Atif Amin
 
PPTX
Automata theory - NFA ε to DFA Conversion
Akila Krishnamoorthy
 
PPTX
Semophores and it's types
Nishant Joshi
 
PPTX
Circular link list.ppt
Tirthika Bandi
 
PPTX
Linked List
Ashim Lamichhane
 
PPTX
Predictive parser
Jothi Lakshmi
 
PPTX
Exception Handling in object oriented programming using C++
Janki Shah
 
trees in data structure
shameen khan
 
Pipelining and vector processing
Kamal Acharya
 
daa-unit-3-greedy method
hodcsencet
 
Breadth First Search & Depth First Search
Kevin Jadiya
 
Data structures using c
Prof. Dr. K. Adisesha
 
Divide and conquer - Quick sort
Madhu Bala
 
Spanning trees
Shareb Ismaeel
 
Dag representation of basic blocks
Jothi Lakshmi
 
Input-Buffering
Dattatray Gandhmal
 
Binary Search
kunj desai
 
Code generation in Compiler Design
Kuppusamy P
 
Timing and control
chauhankapil
 
Data Structures : hashing (1)
Home
 
Automata theory - NFA ε to DFA Conversion
Akila Krishnamoorthy
 
Semophores and it's types
Nishant Joshi
 
Circular link list.ppt
Tirthika Bandi
 
Linked List
Ashim Lamichhane
 
Predictive parser
Jothi Lakshmi
 
Exception Handling in object oriented programming using C++
Janki Shah
 
Ad

Similar to Huffman's algorithm in Data Structure (20)

PPTX
Ppt of discrete structure copy - copy - copy
MRA7860
 
PDF
j001adcpresentation-2112170415 23.pdf
HarshSharma71048
 
PPTX
Huffman Algorithm and its Application by Ekansh Agarwal
Ekansh Agarwal
 
PPTX
Huffman's Alforithm
Roohaali
 
PPTX
Huffman.Encodingpptx Variable length coding
zeeshanmubeen1
 
PDF
Huffman Encoding Algorithm - Concepts and Example
MaryJacob24
 
PDF
DSA Presentetion Huffman tree.pdf
GaneshPawar819187
 
PDF
Adaptive huffman coding
Burqaa Hundeessaa
 
PPTX
Data structures' project
Behappy Seehappy
 
PDF
Daa chapter4
B.Kirron Reddi
 
PDF
LEC 7-DS ALGO(expression and huffman).pdf
MuhammadUmerIhtisham
 
PDF
INSTRUCTIONS For this assignment you will be generating all code on y.pdf
adayarboot
 
PDF
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
PPT
Hufman coding basic
radthees
 
PDF
LEC 5 [CS 101] Introduction to computer science.pdf
p87783936
 
PPTX
Huffman coding || Huffman Tree
SatishKumarInumarthi
 
DOCX
Huffman Coding is a technique of compressing data
Kumari99
 
PDF
Data Communication & Computer network: Shanon fano coding
Dr Rajiv Srivastava
 
PPTX
Huffman ppt
ALexHunter69
 
PPT
Huffman&Shannon-multimedia algorithms.ppt
PrincessSaro
 
Ppt of discrete structure copy - copy - copy
MRA7860
 
j001adcpresentation-2112170415 23.pdf
HarshSharma71048
 
Huffman Algorithm and its Application by Ekansh Agarwal
Ekansh Agarwal
 
Huffman's Alforithm
Roohaali
 
Huffman.Encodingpptx Variable length coding
zeeshanmubeen1
 
Huffman Encoding Algorithm - Concepts and Example
MaryJacob24
 
DSA Presentetion Huffman tree.pdf
GaneshPawar819187
 
Adaptive huffman coding
Burqaa Hundeessaa
 
Data structures' project
Behappy Seehappy
 
Daa chapter4
B.Kirron Reddi
 
LEC 7-DS ALGO(expression and huffman).pdf
MuhammadUmerIhtisham
 
INSTRUCTIONS For this assignment you will be generating all code on y.pdf
adayarboot
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Professor Lili Saghafi
 
Hufman coding basic
radthees
 
LEC 5 [CS 101] Introduction to computer science.pdf
p87783936
 
Huffman coding || Huffman Tree
SatishKumarInumarthi
 
Huffman Coding is a technique of compressing data
Kumari99
 
Data Communication & Computer network: Shanon fano coding
Dr Rajiv Srivastava
 
Huffman ppt
ALexHunter69
 
Huffman&Shannon-multimedia algorithms.ppt
PrincessSaro
 
Ad

Recently uploaded (20)

PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PPTX
ternal cell structure: leadership, steering
hodeeesite4
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
PPT
Lecture in network security and mobile computing
AbdullahOmar704132
 
PPT
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
PPTX
Color Model in Textile ( RGB, CMYK).pptx
auladhossain191
 
PPTX
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PPTX
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
Information Retrieval and Extraction - Module 7
premSankar19
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
Software Testing Tools - names and explanation
shruti533256
 
ternal cell structure: leadership, steering
hodeeesite4
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
Lecture in network security and mobile computing
AbdullahOmar704132
 
1. SYSTEMS, ROLES, AND DEVELOPMENT METHODOLOGIES.ppt
zilow058
 
Color Model in Textile ( RGB, CMYK).pptx
auladhossain191
 
22PCOAM21 Session 2 Understanding Data Source.pptx
Guru Nanak Technical Institutions
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Introduction to Data Science: data science process
ShivarkarSandip
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 

Huffman's algorithm in Data Structure

  • 1. Data Structure & Files Unit 4: Tables 4.2 Huffman’s Algorithm Ms. Vrushali Dhanokar Assistant Professor IT Department
  • 2. What is Huffman’s Algorithm? • Lossless data compression algorithm. • Variable-length code is assigned to input different characters. • The code length is related to how frequently characters are used. • Most frequent characters have the smallest codes and longer codes for least frequent characters. • Complexity for assigning the code for each character according to their frequency is O(n log n). • There are mainly two parts: 1. To create a Huffman tree, 2. To traverse the tree to find Huffman codes.
  • 3. Steps to build Huffman Tree: Input is an array of unique characters along with their frequency of occurrences and output is Huffman Tree. 1. Create a leaf node for each unique character and build a min heap of all leaf nodes (Min Heap is used as a priority queue. The value of frequency field is used to compare two nodes in min heap. Initially, the least frequent character is at root). 2. Extract two nodes with the minimum frequency from the min heap. 3. Create a new internal node with a frequency equal to the sum of the two nodes frequencies. Make the first extracted node as its left child and the other extracted node as its right child. Add this node to the min heap. 4. Repeat steps#2 and #3 until the heap contains only one node. The remaining node is the root node and the tree is complete.
  • 4. Steps to Building Huffman’s Tree: Let’s understand the algorithm with example: Example 1: Character Frequency a 5 b 9 1. 5+9=14(Internal Node) c 12 2. 12+13=25(Internal Node) d 13 3. 14+16=30(Internal Node) e 16 4. 25+30=55.(Internal Node) f 45 5. 55+45=100.(Root Node)
  • 5. Continue.. Step 1: Build Min Heap DS, Which contains 6 Nodes(Given example having 6 Char.) Each node represent as root. Step 2: Extract two minimum frequency nodes from min heap. Add a new internal node with frequency 5 + 9 = 14. Now 14 becomes internal root node. Step 3: Again extract two minimum frequency nodes from heap. Add a new internal node with frequency 12 + 13 = 25. Now 25 becomes internal root node. Step 4: Again extract two minimum frequency nodes. Add a new internal node with frequency 14 + 16 = 30. Now 30 becomes internal root node. Now min heap contains 3 nodes.
  • 6. Continue.. Step 5: Extract two minimum frequency nodes. Add a new internal node with frequency 25 + 30 = 55. Now min heap contains 2 nodes. Step 6: Extract two minimum frequency nodes. Add a new internal node with frequency 45 + 55 = 100. Step 7: Now min heap contains only one node. Since the heap contains only one node, the algorithm stops here.
  • 7. Steps to print codes from Huffman Tree: 1. Traverse the tree formed starting from the root. 2. Maintain an auxiliary array. 3. While moving to the left child, write 0 to the array. 4. While moving to the right child, write 1 to the array. 5. Print the array when a leaf node is encountered. • The codes are as follows: Characters Code-word f 0 c 100 d 101 a 1100 b 1101 e 111
  • 8. Example 2: Apply Huffman Coding for the word ‘MALAYALAM’. Give Huffman Code for each symbol.
  • 9. Important Questions • By SPPU Exam Pattern 1. Explain Huffman algorithm with example. 4M 1. Construct Huffman Tree and Code with following data Character A B C D E Frequency20 10 10 30 30 5M 3. Apply Huffman Coding for the word ‘ENGINEERING’. Give Huffman Code for each symbol. 6M