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

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
 

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
 
Ad

Recently uploaded (20)

PDF
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
PPTX
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PPTX
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
PDF
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 
How to Hire AI Developers_ Step-by-Step Guide in 2025.pdf
DianApps Technologies
 
Coefficient of Variance in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
Customise Your Correlation Table in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Homogeneity of Variance Test Options IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
OpenChain @ OSS NA - In From the Cold: Open Source as Part of Mainstream Soft...
Shane Coughlan
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
SAP Firmaya İade ABAB Kodları - ABAB ile yazılmıl hazır kod örneği
Salih Küçük
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
AEM User Group: India Chapter Kickoff Meeting
jennaf3
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Empowering Asian Contributions: The Rise of Regional User Groups in Open Sour...
Shane Coughlan
 
TheFutureIsDynamic-BoxLang witch Luis Majano.pdf
Ortus Solutions, Corp
 

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