SlideShare a Scribd company logo
1 of 10
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

What's hot

Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]Muhammad Hammad Waseem
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures treemaamir farooq
 
Travelling Salesman
Travelling SalesmanTravelling Salesman
Travelling SalesmanShuvojit Kar
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structureVardhil Patel
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite AutomataRatnakar Mikkili
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked ListsAfaq Mansoor Khan
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
Huffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationHuffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationAkm Monir
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSAjunnubabu
 

What's hot (20)

Red black tree
Red black treeRed black tree
Red black tree
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
Lecture notes data structures tree
Lecture notes data structures   treeLecture notes data structures   tree
Lecture notes data structures tree
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Heaps
HeapsHeaps
Heaps
 
Travelling Salesman
Travelling SalesmanTravelling Salesman
Travelling Salesman
 
Sparse matrix and its representation data structure
Sparse matrix and its representation data structureSparse matrix and its representation data structure
Sparse matrix and its representation data structure
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
Linklist
LinklistLinklist
Linklist
 
Doubly & Circular Linked Lists
Doubly & Circular Linked ListsDoubly & Circular Linked Lists
Doubly & Circular Linked Lists
 
Heap tree
Heap treeHeap tree
Heap tree
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Huffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationHuffman Coding Algorithm Presentation
Huffman Coding Algorithm Presentation
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Linked List
Linked ListLinked List
Linked List
 

Similar to Huffman's algorithm in Data Structure

Ppt of discrete structure copy - copy - copy
Ppt of discrete structure   copy - copy - copyPpt of discrete structure   copy - copy - copy
Ppt of discrete structure copy - copy - copyMRA7860
 
j001adcpresentation-2112170415 23.pdf
j001adcpresentation-2112170415      23.pdfj001adcpresentation-2112170415      23.pdf
j001adcpresentation-2112170415 23.pdfHarshSharma71048
 
Huffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh AgarwalHuffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh AgarwalEkansh Agarwal
 
DSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdfDSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdfGaneshPawar819187
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdfMuhammadUmerIhtisham
 
INSTRUCTIONS For this assignment you will be generating all code on y.pdf
 INSTRUCTIONS For this assignment you will be generating all code on y.pdf INSTRUCTIONS For this assignment you will be generating all code on y.pdf
INSTRUCTIONS For this assignment you will be generating all code on y.pdfadayarboot
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiProfessor Lili Saghafi
 
Hufman coding basic
Hufman coding basicHufman coding basic
Hufman coding basicradthees
 
Data Communication & Computer network: Shanon fano coding
Data Communication & Computer network: Shanon fano codingData Communication & Computer network: Shanon fano coding
Data Communication & Computer network: Shanon fano codingDr Rajiv Srivastava
 
Huffman&Shannon-multimedia algorithms.ppt
Huffman&Shannon-multimedia algorithms.pptHuffman&Shannon-multimedia algorithms.ppt
Huffman&Shannon-multimedia algorithms.pptPrincessSaro
 
Greedy Algorithms Huffman Coding.ppt
Greedy Algorithms  Huffman Coding.pptGreedy Algorithms  Huffman Coding.ppt
Greedy Algorithms Huffman Coding.pptRuchika Sinha
 
Huffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysisHuffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysisRamakant Soni
 

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

Ppt of discrete structure copy - copy - copy
Ppt of discrete structure   copy - copy - copyPpt of discrete structure   copy - copy - copy
Ppt of discrete structure copy - copy - copy
 
j001adcpresentation-2112170415 23.pdf
j001adcpresentation-2112170415      23.pdfj001adcpresentation-2112170415      23.pdf
j001adcpresentation-2112170415 23.pdf
 
Huffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh AgarwalHuffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh Agarwal
 
DSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdfDSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdf
 
Adaptive huffman coding
Adaptive huffman codingAdaptive huffman coding
Adaptive huffman coding
 
Data structures' project
Data structures' projectData structures' project
Data structures' project
 
Daa chapter4
Daa chapter4Daa chapter4
Daa chapter4
 
LEC 7-DS ALGO(expression and huffman).pdf
LEC 7-DS  ALGO(expression and huffman).pdfLEC 7-DS  ALGO(expression and huffman).pdf
LEC 7-DS ALGO(expression and huffman).pdf
 
INSTRUCTIONS For this assignment you will be generating all code on y.pdf
 INSTRUCTIONS For this assignment you will be generating all code on y.pdf INSTRUCTIONS For this assignment you will be generating all code on y.pdf
INSTRUCTIONS For this assignment you will be generating all code on y.pdf
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
 
Hufman coding basic
Hufman coding basicHufman coding basic
Hufman coding basic
 
Huffman coding || Huffman Tree
Huffman coding || Huffman TreeHuffman coding || Huffman Tree
Huffman coding || Huffman Tree
 
Data Communication & Computer network: Shanon fano coding
Data Communication & Computer network: Shanon fano codingData Communication & Computer network: Shanon fano coding
Data Communication & Computer network: Shanon fano coding
 
Huffman ppt
Huffman ppt Huffman ppt
Huffman ppt
 
Huffman&Shannon-multimedia algorithms.ppt
Huffman&Shannon-multimedia algorithms.pptHuffman&Shannon-multimedia algorithms.ppt
Huffman&Shannon-multimedia algorithms.ppt
 
Huffman
HuffmanHuffman
Huffman
 
Huffman
HuffmanHuffman
Huffman
 
Greedy Algorithms Huffman Coding.ppt
Greedy Algorithms  Huffman Coding.pptGreedy Algorithms  Huffman Coding.ppt
Greedy Algorithms Huffman Coding.ppt
 
Lec 6-bp
Lec 6-bpLec 6-bp
Lec 6-bp
 
Huffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysisHuffman and Arithmetic coding - Performance analysis
Huffman and Arithmetic coding - Performance analysis
 

Recently uploaded

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 

Recently uploaded (20)

VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 

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