SlideShare a Scribd company logo
Data Structure and Algorithms Huffman Coding Algorithm
Encoding and Compression of Data
Fax Machines
ASCII
Variations on ASCII
min number of bits needed
cost of savings
patterns
modifications
Purpose of Huffman Coding
Proposed by Dr. David A. Huffman in 1952
“A Method for the Construction of Minimum
Redundancy Codes”
Applicable to many forms of data transmission
Our example: text files
The Basic Algorithm
Huffman coding is a form of statistical coding
Not all characters occur with the same frequency!
Yet all characters are allocated the same amount of
space
1 char = 1 byte, be it e or x
Any savings in tailoring codes to frequency of character?
Code word lengths are no longer fixed like ASCII.
Code word lengths vary and will be shorter for the more
frequently used characters.
The (Real) Basic Algorithm
1. Scan text to be compressed and tally occurrence of
all characters.
2. Sort or prioritize characters based on number of
occurrences in text.
3. Build Huffman code tree based on prioritized list.
4. Perform a traversal of tree to determine all code
words.
5. Scan text again and create new file using the
Huffman codes.
Building a Tree
Scan the original text
Consider the following short text:
Eerie eyes seen near lake.
Count up the occurrences of all characters in the text
Building a Tree
Scan the original text
Eerie eyes seen near lake.
What characters are present?
E e r i space
y s n a r l k .
Building a Tree
Scan the original text
Eerie eyes seen near lake.
What is 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
Building a Tree
Prioritize characters
Create binary tree nodes with character and
frequency of each character
Place nodes in a priority queue
The lower the occurrence, the higher the priority in
the queue
•CS 102
Building a Tree
Prioritize characters
Uses binary tree nodes
public class HuffNode
{
public char myChar;
public int myFrequency;
public HuffNode myLeft, myRight;
}
priorityQueue myQueue;
Building a Tree
The queue after inserting all nodes
Null Pointers are not shown
•CS 102
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
Building a Tree
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
Building a Tree
E
1
i
1
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
E i
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
E i
y
1
l
1
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
E i
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
y l
E i
k
1
.
1
r
2
s
2
n
2
a
2
sp
4
e
8
2
y l
2
E i
r
2
s
2
n
2
a
2
sp
4
e
8
2
y l
2
k .
2
E i
r
2
s
2
n
2
a
2
sp
4
e
8
2
y l
2
k .
2
E i
n
2
a
2
sp
4
e
8
2
y l
2
k .
2
r s
4
E i
n
2
a
2
sp
4
e
8
2
y l
2
k .
2
r s
4
E i
sp
4
e
8
2
y l
2
k .
2
r s
4
n a
4
E i
sp
4
e
8
2
y l
2
k .
2
r s
4
n a
4
E i
sp
4
e
8
2
y l
2
k .
2
r s
4
n a
4
4
E i
sp
4
e
82
y l
2
k .
2
r s
4
n a
4 4
E i
sp
e
82
y l
2
k .
2
r s
4
n a
4 4
6
E i
sp
e
8
2
y l
2
k .
2
r s
4
n a
4 4 6
What is happening to the characters
with a low number of occurrences?
E i
sp
e
82
y l
2
k .
2
r s
4
n a
4
4
6
8
E i
sp
e
82
y l
2
k .
2
r s
4
n a
4
4
6 8
E i
sp
e
8
2
y l
2
k .
2
r s
4
n a
4
4
6
8
10
E i
sp
e
8
2
y l
2
k .
2r s
4
n a
4 4
6
8 10
E i
sp
e2
y l
2
k .
2
r s
4
n a
4
4
6
8
10
16
E i
sp
e
2
y l
2
k .
2
r s
4
n a
4
4
6
8
10 16
E i
sp
e
2
y l
2
k .
2
r s
4
n a
4
4
6
8
10
16
26
E i
sp
e
2
y l
2
k .
2
r s
4
n a
4
4
6
8
10
16
26
•After
enqueueing
this node
there is only
one node left
in priority
queue.
Dequeue the single node
left in the queue.
This tree contains the
new code words for each
character.
Frequency of root node
should equal number of
characters in text.
E i
sp
e
2
y l
2
k .
2
r s
4
n a
4
4
6 8
10
16
26
Eerie eyes seen near lake. 26 characters
Encoding the File
Traverse Tree for Codes
Perform a traversal of the
tree to obtain new code
words
Going left is a 0 going right
is a 1
code word is only
completed when a leaf
node is reached
E i
sp
e
2
y l
2
k .
2
r s
4
n a
4
4
6 8
10
16
26
Encoding the File
Traverse Tree for Codes
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 i
sp
e
2
y l
2
k .
2
r s
4
n a
4
4
6 8
10
16
26
Encoding the File
Rescan text and encode file
using new code words
Eerie eyes seen near lake.
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
0000101100000110011
1000101011011010011
1110101111110001100
1111110100100101
• Why is there no need
for a separator
character?
Encoding the File
Results
Have we made things any
better?
73 bits to encode the text
ASCII would take 8 * 26 =
208 bits
0000101100000110011
1000101011011010011
1110101111110001100
1111110100100101
If modified code used 4 bits per
character are needed. Total bits
4 * 26 = 104. Savings not as great.
Decoding the File
How does receiver know what the codes are?
Tree constructed for each text file.
Considers frequency for each file
Big hit on compression, especially for smaller files
Tree predetermined
based on statistical analysis of text files or file types
Data transmission is bit based versus byte based
Decoding the File
Once receiver has tree it
scans incoming bit stream
0 ⇒ go left
1 ⇒ go right
E i
sp
e
2
y l
2
k .
2
r s
4
n a
4
4
6 8
10
16
26
101000110111101111
01111110000110101
0000101100000110011
1000101011011010011
1110101111110001100
1111110100100101
Summary
Huffman coding is a technique used to compress
files for transmission
Uses statistical coding
more frequently used symbols have shorter code words
Works well for text and fax transmissions
An application that uses several data structures
Thank You

More Related Content

PPTX
Huffman Codes
Md. Shafiuzzaman Hira
 
PPTX
Huffman Coding
Muhammad Saqib Rehan
 
PPTX
Huffman coding || Huffman Tree
Gurunadh Guru
 
PPTX
Huffman coding
Nihal Gupta
 
PDF
Huffman and Arithmetic coding - Performance analysis
Ramakant Soni
 
PPTX
Huffman's Alforithm
Roohaali
 
PPTX
Huffman coding || Huffman Tree
SatishKumarInumarthi
 
PPTX
Huffman Algorithm and its Application by Ekansh Agarwal
Ekansh Agarwal
 
Huffman Codes
Md. Shafiuzzaman Hira
 
Huffman Coding
Muhammad Saqib Rehan
 
Huffman coding || Huffman Tree
Gurunadh Guru
 
Huffman coding
Nihal Gupta
 
Huffman and Arithmetic coding - Performance analysis
Ramakant Soni
 
Huffman's Alforithm
Roohaali
 
Huffman coding || Huffman Tree
SatishKumarInumarthi
 
Huffman Algorithm and its Application by Ekansh Agarwal
Ekansh Agarwal
 

What's hot (17)

PPTX
Huffman codes
Nargis Ehsan
 
PPT
Huffman Coding
anithabalaprabhu
 
PPTX
Huffman Coding
Ehtisham Ali
 
DOC
Huffman coding01
Nv Thejaswini
 
PDF
Data compression huffman coding algoritham
Rahul Khanwani
 
PPTX
Shannon Fano
anithabalaprabhu
 
PPT
Huffman Student
anithabalaprabhu
 
PPT
Komdat-Kompresi Data
mursalinfajri007
 
PDF
Huffman Code Decoding
Rex Yuan
 
PPTX
Text compression
Sammer Qader
 
PDF
Data Communication & Computer network: Shanon fano coding
Dr Rajiv Srivastava
 
PPT
Lossless
anithabalaprabhu
 
PPTX
Huffman coding
Nazmul Hyder
 
PDF
Huffman
keerthi vasan
 
PPT
Adaptive Huffman Coding
anithabalaprabhu
 
PPTX
Huffman Algorithm By Shuhin
suhin4000
 
Huffman codes
Nargis Ehsan
 
Huffman Coding
anithabalaprabhu
 
Huffman Coding
Ehtisham Ali
 
Huffman coding01
Nv Thejaswini
 
Data compression huffman coding algoritham
Rahul Khanwani
 
Shannon Fano
anithabalaprabhu
 
Huffman Student
anithabalaprabhu
 
Komdat-Kompresi Data
mursalinfajri007
 
Huffman Code Decoding
Rex Yuan
 
Text compression
Sammer Qader
 
Data Communication & Computer network: Shanon fano coding
Dr Rajiv Srivastava
 
Huffman coding
Nazmul Hyder
 
Huffman
keerthi vasan
 
Adaptive Huffman Coding
anithabalaprabhu
 
Huffman Algorithm By Shuhin
suhin4000
 
Ad

Similar to Data Structure and Algorithms Huffman Coding Algorithm (20)

PPT
Huffman > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
PPT
huffman codes and algorithm
Vinay379568
 
PPT
huffman_nyu.ppt ghgghtttjghh hhhhhhhhhhh
BARUNSINGH43
 
PPT
huffman algoritm upload for understand.ppt
SadiaSharmin40
 
PPT
Huffman
Priyanka Chauhan
 
PPT
Huffman Tree And Its Application
Papu Kumar
 
PDF
CS-102 Data Structures huffman coding.pdf
ssuser034ce1
 
PPTX
Huffman.pptx
HarisMasood20
 
PPTX
Huffman tree
Al-amin Hossain
 
PPT
Greedy Algorithms Huffman Coding.ppt
Ruchika Sinha
 
PPT
Huffman 2
grupo6tic
 
PPT
Huffman1
anithabalaprabhu
 
PPT
Huffman
anithabalaprabhu
 
PPTX
Data compession
arvind carpenter
 
PDF
Data communication & computer networking: Huffman algorithm
Dr Rajiv Srivastava
 
PPT
Huffman
Tanmay Baranwal
 
DOCX
The assigment is overdue now. I will up the price I am willing to pa.docx
rtodd17
 
PPT
Huffman Coding.ppt
MuktarHossain13
 
PPT
Hufman coding basic
radthees
 
PPT
computer notes - Data Structures - 24
ecomputernotes
 
Huffman > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
huffman codes and algorithm
Vinay379568
 
huffman_nyu.ppt ghgghtttjghh hhhhhhhhhhh
BARUNSINGH43
 
huffman algoritm upload for understand.ppt
SadiaSharmin40
 
Huffman Tree And Its Application
Papu Kumar
 
CS-102 Data Structures huffman coding.pdf
ssuser034ce1
 
Huffman.pptx
HarisMasood20
 
Huffman tree
Al-amin Hossain
 
Greedy Algorithms Huffman Coding.ppt
Ruchika Sinha
 
Huffman 2
grupo6tic
 
Data compession
arvind carpenter
 
Data communication & computer networking: Huffman algorithm
Dr Rajiv Srivastava
 
The assigment is overdue now. I will up the price I am willing to pa.docx
rtodd17
 
Huffman Coding.ppt
MuktarHossain13
 
Hufman coding basic
radthees
 
computer notes - Data Structures - 24
ecomputernotes
 
Ad

More from ManishPrajapati78 (15)

PPT
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
PPT
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
PPT
Data Structure and Algorithms Queues
ManishPrajapati78
 
PPTX
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
PPTX
Data Structure and Algorithms The Tower of Hanoi
ManishPrajapati78
 
PPT
Data Structure and Algorithms Stacks
ManishPrajapati78
 
PPT
Data Structure and Algorithms Linked List
ManishPrajapati78
 
PPT
Data Structure and Algorithms Sorting
ManishPrajapati78
 
PPT
Data Structure and Algorithms Arrays
ManishPrajapati78
 
PPT
Data Structure and Algorithms
ManishPrajapati78
 
PPT
Data Structure and Algorithms Hashing
ManishPrajapati78
 
PPTX
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
PPT
Data Structure and Algorithms Graphs
ManishPrajapati78
 
PPT
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
PPT
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 
Data Structure and Algorithms Binary Search Tree
ManishPrajapati78
 
Data Structure and Algorithms Binary Tree
ManishPrajapati78
 
Data Structure and Algorithms Queues
ManishPrajapati78
 
Data Structure and Algorithms Merge Sort
ManishPrajapati78
 
Data Structure and Algorithms The Tower of Hanoi
ManishPrajapati78
 
Data Structure and Algorithms Stacks
ManishPrajapati78
 
Data Structure and Algorithms Linked List
ManishPrajapati78
 
Data Structure and Algorithms Sorting
ManishPrajapati78
 
Data Structure and Algorithms Arrays
ManishPrajapati78
 
Data Structure and Algorithms
ManishPrajapati78
 
Data Structure and Algorithms Hashing
ManishPrajapati78
 
Data Structure and Algorithms Graph Traversal
ManishPrajapati78
 
Data Structure and Algorithms Graphs
ManishPrajapati78
 
Data Structure and Algorithms Heaps and Trees
ManishPrajapati78
 
Data Structure and Algorithms AVL Trees
ManishPrajapati78
 

Recently uploaded (20)

PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PDF
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PDF
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
PPTX
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
PDF
Exploring AI Agents in Process Industries
amoreira6
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
Micromaid: A simple Mermaid-like chart generator for Pharo
ESUG
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Community & News Update Q2 Meet Up 2025
VictoriaMetrics
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
IEEE-CS Tech Predictions, SWEBOK and Quantum Software: Towards Q-SWEBOK
Hironori Washizaki
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Odoo Integration Services by Candidroot Solutions
CandidRoot Solutions Private Limited
 
Services offered by Dynamic Solutions in Pakistan
DaniyaalAdeemShibli1
 
Exploring AI Agents in Process Industries
amoreira6
 
Activate_Methodology_Summary presentatio
annapureddyn
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Presentation about variables and constant.pptx
kr2589474
 
Wondershare Filmora 14.5.20.12999 Crack Full New Version 2025
gsgssg2211
 
Bandai Playdia The Book - David Glotz
BluePanther6
 

Data Structure and Algorithms Huffman Coding Algorithm

  • 2. Encoding and Compression of Data Fax Machines ASCII Variations on ASCII min number of bits needed cost of savings patterns modifications
  • 3. Purpose of Huffman Coding Proposed by Dr. David A. Huffman in 1952 “A Method for the Construction of Minimum Redundancy Codes” Applicable to many forms of data transmission Our example: text files
  • 4. The Basic Algorithm Huffman coding is a form of statistical coding Not all characters occur with the same frequency! Yet all characters are allocated the same amount of space 1 char = 1 byte, be it e or x Any savings in tailoring codes to frequency of character? Code word lengths are no longer fixed like ASCII. Code word lengths vary and will be shorter for the more frequently used characters.
  • 5. The (Real) Basic Algorithm 1. Scan text to be compressed and tally occurrence of all characters. 2. Sort or prioritize characters based on number of occurrences in text. 3. Build Huffman code tree based on prioritized list. 4. Perform a traversal of tree to determine all code words. 5. Scan text again and create new file using the Huffman codes.
  • 6. Building a Tree Scan the original text Consider the following short text: Eerie eyes seen near lake. Count up the occurrences of all characters in the text
  • 7. Building a Tree Scan the original text Eerie eyes seen near lake. What characters are present? E e r i space y s n a r l k .
  • 8. Building a Tree Scan the original text Eerie eyes seen near lake. What is 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
  • 9. Building a Tree Prioritize characters Create binary tree nodes with character and frequency of each character Place nodes in a priority queue The lower the occurrence, the higher the priority in the queue •CS 102
  • 10. Building a Tree Prioritize characters Uses binary tree nodes public class HuffNode { public char myChar; public int myFrequency; public HuffNode myLeft, myRight; } priorityQueue myQueue;
  • 11. Building a Tree The queue after inserting all nodes Null Pointers are not shown •CS 102 E 1 i 1 y 1 l 1 k 1 . 1 r 2 s 2 n 2 a 2 sp 4 e 8
  • 12. Building a Tree 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
  • 22. E i sp 4 e 8 2 y l 2 k . 2 r s 4 n a 4
  • 23. E i sp 4 e 8 2 y l 2 k . 2 r s 4 n a 4
  • 24. E i sp 4 e 8 2 y l 2 k . 2 r s 4 n a 4 4
  • 25. E i sp 4 e 82 y l 2 k . 2 r s 4 n a 4 4
  • 26. E i sp e 82 y l 2 k . 2 r s 4 n a 4 4 6
  • 27. E i sp e 8 2 y l 2 k . 2 r s 4 n a 4 4 6 What is happening to the characters with a low number of occurrences?
  • 28. E i sp e 82 y l 2 k . 2 r s 4 n a 4 4 6 8
  • 29. E i sp e 82 y l 2 k . 2 r s 4 n a 4 4 6 8
  • 30. E i sp e 8 2 y l 2 k . 2 r s 4 n a 4 4 6 8 10
  • 31. E i sp e 8 2 y l 2 k . 2r s 4 n a 4 4 6 8 10
  • 32. E i sp e2 y l 2 k . 2 r s 4 n a 4 4 6 8 10 16
  • 33. E i sp e 2 y l 2 k . 2 r s 4 n a 4 4 6 8 10 16
  • 34. E i sp e 2 y l 2 k . 2 r s 4 n a 4 4 6 8 10 16 26
  • 35. E i sp e 2 y l 2 k . 2 r s 4 n a 4 4 6 8 10 16 26 •After enqueueing this node there is only one node left in priority queue.
  • 36. Dequeue the single node left in the queue. This tree contains the new code words for each character. Frequency of root node should equal number of characters in text. E i sp e 2 y l 2 k . 2 r s 4 n a 4 4 6 8 10 16 26 Eerie eyes seen near lake. 26 characters
  • 37. Encoding the File Traverse Tree for Codes Perform a traversal of the tree to obtain new code words Going left is a 0 going right is a 1 code word is only completed when a leaf node is reached E i sp e 2 y l 2 k . 2 r s 4 n a 4 4 6 8 10 16 26
  • 38. Encoding the File Traverse Tree for Codes 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 i sp e 2 y l 2 k . 2 r s 4 n a 4 4 6 8 10 16 26
  • 39. Encoding the File Rescan text and encode file using new code words Eerie eyes seen near lake. 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 0000101100000110011 1000101011011010011 1110101111110001100 1111110100100101 • Why is there no need for a separator character?
  • 40. Encoding the File Results Have we made things any better? 73 bits to encode the text ASCII would take 8 * 26 = 208 bits 0000101100000110011 1000101011011010011 1110101111110001100 1111110100100101 If modified code used 4 bits per character are needed. Total bits 4 * 26 = 104. Savings not as great.
  • 41. Decoding the File How does receiver know what the codes are? Tree constructed for each text file. Considers frequency for each file Big hit on compression, especially for smaller files Tree predetermined based on statistical analysis of text files or file types Data transmission is bit based versus byte based
  • 42. Decoding the File Once receiver has tree it scans incoming bit stream 0 ⇒ go left 1 ⇒ go right E i sp e 2 y l 2 k . 2 r s 4 n a 4 4 6 8 10 16 26 101000110111101111 01111110000110101 0000101100000110011 1000101011011010011 1110101111110001100 1111110100100101
  • 43. Summary Huffman coding is a technique used to compress files for transmission Uses statistical coding more frequently used symbols have shorter code words Works well for text and fax transmissions An application that uses several data structures