SlideShare a Scribd company logo
Data Structures
Lecture No. 16
Huffman’s Algorithm
Engr. Rashid Farid Chishti
http://youtube.com/rfchishti
http://sites.google.com/site/chishti
International Islamic University H-10, Islamabad, Pakistan
Video Lecture
 Huffman code is method for the compression for standard text documents.
 It makes use of a binary tree to develop codes of varying lengths for the letters
used in the original message.
 Huffman code is also part of the JPEG image compression scheme.
 The algorithm was introduced by David Huffman in 1952 as part of a course
assignment at MIT.
 It’s a variable-length code because each character may be encoded with
different numbers of bits
 It’s a statistical coding scheme
 Normally Characters don’t occur with the same frequency
 It’s a lossless data compression algorithm.
Huffman Code
 Scan text and count the occurrences of all characters
 Sort characters based on the number of occurrences in text
 Make each character a leaf node, with its frequency as its weight
 Pair the two least frequently occurring characters
 Create a subtree from the pair and the weight is sum of the two
 Repeat until all subtrees have been paired into a single tree
 Encode each character as the path from the root, with a left
represented as 0, and a right as 1
Algorithm
Example
Characters Frequency
A 10
E 15
I 12
S 3
T 4
SP 13
X 1
Characters Frequency
E 15
SP 13
I 12
A 10
T 4
S 3
X 1
Sort
Minimum Priority Queue
X, 1 S, 3 E,15
SP,13
I,12
A,10
T, 4
push()
pop()
top()
removes an
element with
min priority
gets an element
with min priority
X, 1
E,15
SP,13
I,12
A,10
T, 4 $, 4
S,3
Priority Queue
E,15
SP,13
I,12
A,10
$, 8
T, 4
S,3
X, 1
$, 4
Priority Queue
E,15
SP,13
I,12
$, 8
T, 4
$,18
A,10
X, 1
$, 4
S,3
Priority Queue
E,15
$, 8
T, 4
$,18
A,10
X, 1
$, 4
S,3
I,12
$,25
SP,13
Priority Queue
I,12
$,25
SP,13
$, 8
T, 4
$,18
E,15
$,33
A,10
S,3
X, 1
$, 4
Priority Queue
Priority Queue
SP,13
I,12
$, 8
T, 4
$,25
$,18
E,15
$,33
$,58
A,10
S,3
X, 1
$, 4
root
SP,13
I,12
$, 8
T, 4
$,25
$,18
E,15
$,33
$,58
1
1
1
1
0
0
0
0
A,10
1
0
0
S,3
1
X, 1
$, 4
Characters Codes
I 00
SP 01
E 10
A 111
T 1100
S 11011
X 11010
Char. Freq.
A 10
E 15
I 12
S 3
T 4
SP 13
X 1
#include <string>
#include "Priority_Queue.h"
using namespace std;
struct New_Node {
char data;
size_t freq;
New_Node* left;
New_Node* right;
New_Node(char data, size_t freq) {
this->data = data;
this->freq = freq;
left = NULL ;
right = NULL;
}
~New_Node(){
delete left; delete right;
}
};
12
Example 1: Implementation of Huffman Coding Algorithm
1
class Huffman_Codes {
New_Node* root;
void Print_Code(New_Node* currentNode, string str) {
if(currentNode == NULL)
return;
if(currentNode->data == '$') {
Print_Code(currentNode->left, str + "0");
Print_Code(currentNode->right, str + "1");
}
if(currentNode->data != '$') {
cout << currentNode->data <<" : " << str << "n";
Print_Code(currentNode->left, str + "0");
Print_Code(currentNode->right, str + "1");
}
}
public:
Huffman_Codes () {};
~Huffman_Codes() {
delete root;
}
13
Example 1: Implementation of Huffman Coding Algorithm
2
void Generate_Huffman_Tree(char data[], size_t freq[], size_t size) {
New_Node* left;
New_Node* right;
Priority_Queue <New_Node*>PQ;
for(size_t i = 0; i < size; ++i) {
PQ.Push(new New_Node(data[i], freq[i]));
}
PQ.Show();
while(PQ.size() != 1) {
left = PQ.Top(); PQ.Pop();
right = PQ.Top(); PQ.Pop();
root = new New_Node('$', left->freq + right->freq);
root->left = left; root->right = right;
PQ.Push(root); PQ.Show();
}
Print_Code(PQ.Top(), "");
}
};
14
Example 1: Implementation of Huffman Coding Algorithm
3
int main() {
Huffman_Codes Huff;
char data [] = { 'A', 'E', 'I', 'S', 'T', ' ', 'X'};
size_t freq [] = { 10 , 15, 12, 3, 4, 13, 1};
size_t size = sizeof(data);
cout <<"Character Frequency" << endl;
for (size_t i=0 ; i<size ; i++)
cout <<" "<< data[i] << " " << freq[i] << endl;
cout <<"---------------------" << endl;
Huff.Generate_Huffman_Tree(data, freq, size);
system("PAUSE"); return 0;
}
15
Example 1: Implementation of Huffman Coding Algorithm
4

More Related Content

PDF
j001adcpresentation-2112170415 23.pdf
PPTX
Huffman Algorithm and its Application by Ekansh Agarwal
PPTX
Huffman coding || Huffman Tree
PPTX
Huffman Algorithm for File Compression.pptx
PPTX
Huffman coding || Huffman Tree
PPT
Greedy Algorithms Huffman Coding.ppt
PPTX
t.pptx hd gsd unduf jdsnfijnfi bndsfuu ubfuh
PPTX
DAA PPT.pptx
j001adcpresentation-2112170415 23.pdf
Huffman Algorithm and its Application by Ekansh Agarwal
Huffman coding || Huffman Tree
Huffman Algorithm for File Compression.pptx
Huffman coding || Huffman Tree
Greedy Algorithms Huffman Coding.ppt
t.pptx hd gsd unduf jdsnfijnfi bndsfuu ubfuh
DAA PPT.pptx

Similar to Data Structures and Agorithm: DS 16 Huffman Coding.pptx (20)

PDF
Huffman Encoding Algorithm - Concepts and Example
PPTX
Huffman Codes
PDF
Huffman
PDF
Huffman
PPTX
Huffman Coding
PDF
Adaptive huffman coding
PPTX
Huffman ppt
PPTX
Counting trees.pptx
PDF
DSA Presentetion Huffman tree.pdf
PDF
Data compression huffman coding algoritham
PPTX
Huffman's algorithm in Data Structure
PDF
Implementation of Lossless Compression Algorithms for Text Data
PPT
hufman coding for compression algorithm.ppt
PPT
compression & huffman coder problem .ppt
PPT
hufman code presentation and how to compress data using hufman code
PPT
23 priority queue
PPTX
Huffman's Alforithm
PPTX
Data structures' project
PPTX
5c. huffman coding using greedy technique.pptx
PPTX
Huffman.pptx
Huffman Encoding Algorithm - Concepts and Example
Huffman Codes
Huffman
Huffman
Huffman Coding
Adaptive huffman coding
Huffman ppt
Counting trees.pptx
DSA Presentetion Huffman tree.pdf
Data compression huffman coding algoritham
Huffman's algorithm in Data Structure
Implementation of Lossless Compression Algorithms for Text Data
hufman coding for compression algorithm.ppt
compression & huffman coder problem .ppt
hufman code presentation and how to compress data using hufman code
23 priority queue
Huffman's Alforithm
Data structures' project
5c. huffman coding using greedy technique.pptx
Huffman.pptx
Ad

More from RashidFaridChishti (20)

PPTX
DBMS: Week 15 - Database Security and Access Control
PPTX
DBMS: Week 14 - Backup and Recovery in MySQL
PPTX
DBMS: Week 13 - Transactions and Concurrency Control
PPTX
DBMS: Week 12 - Triggers in MySQL Database Server
PPTX
DBMS: Week 11 - Stored Procedures and Functions
PPTX
DBMS: Week 10 - Database Design and Normalization
PPTX
DBMS: Week 09 - SQL Constraints and Indexing
PPTX
DBMS: Week 08 - Joins and Views in MySQL
PPTX
DBMS: Week 07 - Advanced SQL Queries in MySQL
PPTX
DBMS: Week 06 - SQL - Data Manipulation Language (DML)
PPTX
DBMS: Week 05 - Introduction to SQL Query
PPTX
DBMS: Week 04 - Relational Model in a Database
PPTX
DBMS: Week 03 - Data Models and ER Model
PPTX
DBMS: Week 02 - Database System Architecture
PPTX
DBMS: Week 01 - Introduction to Databases
DOCX
Lab Manual Arduino UNO Microcontrollar.docx
DOCX
Object Oriented Programming OOP Lab Manual.docx
DOCX
Lab Manual Data Structure and Algorithm.docx
PPTX
Data Structures and Agorithm: DS 24 Hash Tables.pptx
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
DBMS: Week 15 - Database Security and Access Control
DBMS: Week 14 - Backup and Recovery in MySQL
DBMS: Week 13 - Transactions and Concurrency Control
DBMS: Week 12 - Triggers in MySQL Database Server
DBMS: Week 11 - Stored Procedures and Functions
DBMS: Week 10 - Database Design and Normalization
DBMS: Week 09 - SQL Constraints and Indexing
DBMS: Week 08 - Joins and Views in MySQL
DBMS: Week 07 - Advanced SQL Queries in MySQL
DBMS: Week 06 - SQL - Data Manipulation Language (DML)
DBMS: Week 05 - Introduction to SQL Query
DBMS: Week 04 - Relational Model in a Database
DBMS: Week 03 - Data Models and ER Model
DBMS: Week 02 - Database System Architecture
DBMS: Week 01 - Introduction to Databases
Lab Manual Arduino UNO Microcontrollar.docx
Object Oriented Programming OOP Lab Manual.docx
Lab Manual Data Structure and Algorithm.docx
Data Structures and Agorithm: DS 24 Hash Tables.pptx
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
Ad

Recently uploaded (20)

PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
86236642-Electric-Loco-Shed.pdf jfkduklg
PPTX
introduction to high performance computing
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
Nature of X-rays, X- Ray Equipment, Fluoroscopy
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Soil Improvement Techniques Note - Rabbi
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPT
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PDF
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
PPTX
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
PPT on Performance Review to get promotions
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Safety Seminar civil to be ensured for safe working.
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
86236642-Electric-Loco-Shed.pdf jfkduklg
introduction to high performance computing
III.4.1.2_The_Space_Environment.p pdffdf
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
Nature of X-rays, X- Ray Equipment, Fluoroscopy
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Soil Improvement Techniques Note - Rabbi
Automation-in-Manufacturing-Chapter-Introduction.pdf
A5_DistSysCh1.ppt_INTRODUCTION TO DISTRIBUTED SYSTEMS
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Exploratory_Data_Analysis_Fundamentals.pdf
SMART SIGNAL TIMING FOR URBAN INTERSECTIONS USING REAL-TIME VEHICLE DETECTI...
CURRICULAM DESIGN engineering FOR CSE 2025.pptx
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PPT on Performance Review to get promotions
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Safety Seminar civil to be ensured for safe working.

Data Structures and Agorithm: DS 16 Huffman Coding.pptx

  • 1. Data Structures Lecture No. 16 Huffman’s Algorithm Engr. Rashid Farid Chishti http://youtube.com/rfchishti http://sites.google.com/site/chishti International Islamic University H-10, Islamabad, Pakistan Video Lecture
  • 2.  Huffman code is method for the compression for standard text documents.  It makes use of a binary tree to develop codes of varying lengths for the letters used in the original message.  Huffman code is also part of the JPEG image compression scheme.  The algorithm was introduced by David Huffman in 1952 as part of a course assignment at MIT.  It’s a variable-length code because each character may be encoded with different numbers of bits  It’s a statistical coding scheme  Normally Characters don’t occur with the same frequency  It’s a lossless data compression algorithm. Huffman Code
  • 3.  Scan text and count the occurrences of all characters  Sort characters based on the number of occurrences in text  Make each character a leaf node, with its frequency as its weight  Pair the two least frequently occurring characters  Create a subtree from the pair and the weight is sum of the two  Repeat until all subtrees have been paired into a single tree  Encode each character as the path from the root, with a left represented as 0, and a right as 1 Algorithm
  • 4. Example Characters Frequency A 10 E 15 I 12 S 3 T 4 SP 13 X 1 Characters Frequency E 15 SP 13 I 12 A 10 T 4 S 3 X 1 Sort Minimum Priority Queue X, 1 S, 3 E,15 SP,13 I,12 A,10 T, 4 push() pop() top() removes an element with min priority gets an element with min priority
  • 5. X, 1 E,15 SP,13 I,12 A,10 T, 4 $, 4 S,3 Priority Queue
  • 7. E,15 SP,13 I,12 $, 8 T, 4 $,18 A,10 X, 1 $, 4 S,3 Priority Queue
  • 8. E,15 $, 8 T, 4 $,18 A,10 X, 1 $, 4 S,3 I,12 $,25 SP,13 Priority Queue
  • 10. Priority Queue SP,13 I,12 $, 8 T, 4 $,25 $,18 E,15 $,33 $,58 A,10 S,3 X, 1 $, 4
  • 11. root SP,13 I,12 $, 8 T, 4 $,25 $,18 E,15 $,33 $,58 1 1 1 1 0 0 0 0 A,10 1 0 0 S,3 1 X, 1 $, 4 Characters Codes I 00 SP 01 E 10 A 111 T 1100 S 11011 X 11010 Char. Freq. A 10 E 15 I 12 S 3 T 4 SP 13 X 1
  • 12. #include <string> #include "Priority_Queue.h" using namespace std; struct New_Node { char data; size_t freq; New_Node* left; New_Node* right; New_Node(char data, size_t freq) { this->data = data; this->freq = freq; left = NULL ; right = NULL; } ~New_Node(){ delete left; delete right; } }; 12 Example 1: Implementation of Huffman Coding Algorithm 1
  • 13. class Huffman_Codes { New_Node* root; void Print_Code(New_Node* currentNode, string str) { if(currentNode == NULL) return; if(currentNode->data == '$') { Print_Code(currentNode->left, str + "0"); Print_Code(currentNode->right, str + "1"); } if(currentNode->data != '$') { cout << currentNode->data <<" : " << str << "n"; Print_Code(currentNode->left, str + "0"); Print_Code(currentNode->right, str + "1"); } } public: Huffman_Codes () {}; ~Huffman_Codes() { delete root; } 13 Example 1: Implementation of Huffman Coding Algorithm 2
  • 14. void Generate_Huffman_Tree(char data[], size_t freq[], size_t size) { New_Node* left; New_Node* right; Priority_Queue <New_Node*>PQ; for(size_t i = 0; i < size; ++i) { PQ.Push(new New_Node(data[i], freq[i])); } PQ.Show(); while(PQ.size() != 1) { left = PQ.Top(); PQ.Pop(); right = PQ.Top(); PQ.Pop(); root = new New_Node('$', left->freq + right->freq); root->left = left; root->right = right; PQ.Push(root); PQ.Show(); } Print_Code(PQ.Top(), ""); } }; 14 Example 1: Implementation of Huffman Coding Algorithm 3
  • 15. int main() { Huffman_Codes Huff; char data [] = { 'A', 'E', 'I', 'S', 'T', ' ', 'X'}; size_t freq [] = { 10 , 15, 12, 3, 4, 13, 1}; size_t size = sizeof(data); cout <<"Character Frequency" << endl; for (size_t i=0 ; i<size ; i++) cout <<" "<< data[i] << " " << freq[i] << endl; cout <<"---------------------" << endl; Huff.Generate_Huffman_Tree(data, freq, size); system("PAUSE"); return 0; } 15 Example 1: Implementation of Huffman Coding Algorithm 4