SlideShare a Scribd company logo
1 of 48
Huffman Coding:
An Application of Binary
Trees and Priority Queues
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
Example Huffman encoding
• A=0
B = 100
C = 1010
D = 1011
R = 11
• ABRACADABRA = 01001101010010110100110
• This is eleven letters in 23 bits
• A fixed-width encoding would require 3 bits
for five different letters, or 33 bits for 11
letters
• Notice that the encoded bit string can be
decoded!
Why it works
• In this example, A was the
most common letter
• In ABRACADABRA:
–5
–2
–2
–1
–1

As
Rs
Bs
C
D

code
code
code
code
code

for
for
for
for
for

A
R
B
C
D

is
is
is
is
is

1
2
3
4
4

bit long
bits long
bits long
bits long
bits long
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
The Basic Algorithm
• 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.
E
1
e
8
r
2
i
1
space 4

Char Freq.
y
1
s
2
n
2
a
2
l
1

Char Freq.
k
1
.
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
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

E

i

y

l

k

.

r

s

n

a

sp

e

1

1

1

1

1

1

2

2

2

2

4

8

• Null Pointers are not shown
Building a Tree
• While priority queue contains two
or more nodes
– Create new node
– Dequeue node and make
– Dequeue next node and
subtree
– Frequency of new node
frequency of left and
– Enqueue new node back

it left subtree
make it right
equals sum of
right children
into queue
Building a Tree

E

i

y

l

k

.

r

s

n

a

sp

e

1

1

1

1

1

1

2

2

2

2

4

8
Building a Tree

y

l

k

.

r

s

n

a

sp

e

1

1

1

1

2

2

2

2

4

8

2
E

i
Building a Tree

y

l

k

.

r

s

n

a

sp

e

1

1

1

1

2

2

2

2

4

8

E

i
Building a Tree

k

.

r

s

n

a

sp

e

1

1

2

2

2

2

4

8

E

y

i

l
Building a Tree

k

.

r

s

n

1

1

2

2

2

2

2

a
2

y
E

i

sp
l

e

4

8
Building a Tree

r

s

n

2

2

2

2

a
2

i

y

sp
l

2
k

.

e

4
E

2

8
Building a Tree

r

s

n

2

2

2

2

a
2
E

i

y

sp

l

k

.

e

4

2

2

8
Building a Tree

n

a

2

2

2

E

2
i

y

sp
4

2
l

k

4
r

s

.

e
8
Building a Tree

n

a

2

2

2

E

2
i

y

sp

2

e

4

8

4
l

k

.

r

s
Building a Tree

2

E

2
i

y

2
l

k

sp
4

.

4
n

a

e

4

8
r

s
Building a Tree

2

E

2
i

y

2
l

k

4

sp
.

4

e

4

8
r

s

n

a
Building a Tree

2
k

4

sp
.

4

e

4

8
r

s

n

a

4
2
E

i

2
y

l
Building a Tree

2
k

4

sp
.

4

r

4

4
s

n

e

2

a
E

i

2
y

8
l
Building a Tree

4
r

4

4
s

n

2

a
E

6
sp

2
k

.

e

i

2
y

8
l
Building a Tree

4

4
r

s

n

6

4
2

a

2

2
k

E

i

y

e
sp

.

l

What is happening to the characters
with a low number of occurrences?

8
Building a Tree

4

6

2
E

i

2
y

2
l

k

e
sp

8

.
8
4

4
r

s

n

a
Building a Tree

4

6

2
E

i

2
y

2
l

k

8

e
sp

8

4

4

.
r

s

n

a
Building a Tree

8

e
8

4

4

10
r

s

n

a

4

6

2
E

i

2
y

2
l

k

sp
.
Building a Tree

8

e
8

10

r

4

4

4
s

n

6

2

a
E

i

2
y

2
l

k

sp
.
Building a Tree

10
16

4

6

2
E

i

2
y

2
l

k

sp

e

8

.

4

4
r

s

n

a
Building a Tree

10

16

4

6

2
E

i

2
y

2
l

k

e

8

sp

4

4

.
r

s

n

a
Building a Tree
26
16

10
4
2
E

i

e

6
2
y

2
l

k

8

.

4

4

sp
r

s

n

a
Building a Tree
•After
enqueueing
this node
there is only
one node left
in priority
queue.

26
16

10
4
2
E

i

e

6
2
y

2
l

k

8

.

4

4

sp
r

s

n

a
Building a Tree
Dequeue the single node
left in the queue.

26

This tree contains the
new code words for each
character.

4
2

Frequency of root node
should equal number of
characters in text.

Eerie eyes seen near lake.

16

10
e

6
2

2

E i y l k .

sp

8
4

4

r s n a

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

26
16

10
4
2

e

6
2

2

E i y l k .

sp

8
4

4

r s n a
Encoding the File
Traverse Tree for Codes
Char
E
i
y
l
k
.
space
e
r
s
n
a

Code
0000
0001
0010
0011
0100
0101
011
10
1100
1101
1110
1111

26
16

10
4
2

e

6
2

2

E i y l k .

sp

8
4

4

r s n a
Encoding the File
• Rescan text and
encode file using
new code words
Eerie eyes seen near lake.

0000101100000110011
1000101011011010011
1110101111110001100
1111110100100101

Char
E
i
y
l
k
.
space
e
r
s
n
a

Code
0000
0001
0010
0011
0100
0101
011
10
1100
1101
1110
1111
Encoding the File
Results
• Have we made
0000101100000110011
things any
1000101011011010011
better?
1110101111110001100
• 73 bits to encode
1111110100100101
the text
• ASCII would take
8 * 26 = 208 bits
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

101000110111101111
01111110000110101

26
16

10
4
2

e

6
2

2

E i y l k .

sp

8
4

4

r s n a
•Characters codes must be sent to the
decoder with encoded file.
• If the file is small this may results
in increasing the file size.
•Data must be scanned before encoding to
get the frequency distribution
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

More Related Content

What's hot

What's hot (20)

Quick sort
Quick sortQuick sort
Quick sort
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
COMPILER DESIGN
COMPILER DESIGNCOMPILER DESIGN
COMPILER DESIGN
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
Sorting in python
Sorting in python Sorting in python
Sorting in python
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Data Structure and Algorithms Hashing
Data Structure and Algorithms HashingData Structure and Algorithms Hashing
Data Structure and Algorithms Hashing
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Recursive Descent Parsing
Recursive Descent Parsing  Recursive Descent Parsing
Recursive Descent Parsing
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Heap sort
Heap sortHeap sort
Heap sort
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Hashing
HashingHashing
Hashing
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 

Viewers also liked (9)

Adaptive Huffman Coding
Adaptive Huffman CodingAdaptive Huffman Coding
Adaptive Huffman Coding
 
Ch 04 Arithmetic Coding (Ppt)
Ch 04 Arithmetic Coding (Ppt)Ch 04 Arithmetic Coding (Ppt)
Ch 04 Arithmetic Coding (Ppt)
 
Huffman codes
Huffman codesHuffman codes
Huffman codes
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Arithmetic coding
Arithmetic codingArithmetic coding
Arithmetic coding
 
Data compression huffman coding algoritham
Data compression huffman coding algorithamData compression huffman coding algoritham
Data compression huffman coding algoritham
 
Arithmetic Coding
Arithmetic CodingArithmetic Coding
Arithmetic Coding
 
Huffman Coding
Huffman CodingHuffman Coding
Huffman Coding
 
Huffman Coding
Huffman CodingHuffman Coding
Huffman Coding
 

Similar to Huffman > Data Structures & Algorithums

huffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppthuffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppt
SadiaSharmin40
 

Similar to Huffman > Data Structures & Algorithums (20)

Huffman Tree And Its Application
Huffman Tree And Its ApplicationHuffman Tree And Its Application
Huffman Tree And Its Application
 
Data Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding AlgorithmData Structure and Algorithms Huffman Coding Algorithm
Data Structure and Algorithms Huffman Coding Algorithm
 
Huffman tree
Huffman tree Huffman tree
Huffman tree
 
Huffman Codes
Huffman CodesHuffman Codes
Huffman Codes
 
Huffman
HuffmanHuffman
Huffman
 
CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdfCS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdf
 
CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdfCS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdf
 
huffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppthuffman algoritm upload for understand.ppt
huffman algoritm upload for understand.ppt
 
huffman codes and algorithm
huffman codes and algorithm huffman codes and algorithm
huffman codes and algorithm
 
huffman_nyu.ppt ghgghtttjghh hhhhhhhhhhh
huffman_nyu.ppt ghgghtttjghh hhhhhhhhhhhhuffman_nyu.ppt ghgghtttjghh hhhhhhhhhhh
huffman_nyu.ppt ghgghtttjghh hhhhhhhhhhh
 
Huffman
HuffmanHuffman
Huffman
 
Greedy Algorithms Huffman Coding.ppt
Greedy Algorithms  Huffman Coding.pptGreedy Algorithms  Huffman Coding.ppt
Greedy Algorithms Huffman Coding.ppt
 
Huffman.pptx
Huffman.pptxHuffman.pptx
Huffman.pptx
 
Farhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman codingFarhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman coding
 
Komdat-Kompresi Data
Komdat-Kompresi DataKomdat-Kompresi Data
Komdat-Kompresi Data
 
Huffman
HuffmanHuffman
Huffman
 
Huffman1
Huffman1Huffman1
Huffman1
 
Huffman 2
Huffman 2Huffman 2
Huffman 2
 
Module-IV 094.pdf
Module-IV 094.pdfModule-IV 094.pdf
Module-IV 094.pdf
 
Des lecture
Des lectureDes lecture
Des lecture
 

More from Ain-ul-Moiz Khawaja (17)

Algo>Queues
Algo>QueuesAlgo>Queues
Algo>Queues
 
Application of Stacks
Application of StacksApplication of Stacks
Application of Stacks
 
Algo>Stacks
Algo>StacksAlgo>Stacks
Algo>Stacks
 
Analysis of Algorithum
Analysis of AlgorithumAnalysis of Algorithum
Analysis of Algorithum
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Algo>Abstract data type
Algo>Abstract data typeAlgo>Abstract data type
Algo>Abstract data type
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Sorting algos > Data Structures & Algorithums
Sorting algos  > Data Structures & AlgorithumsSorting algos  > Data Structures & Algorithums
Sorting algos > Data Structures & Algorithums
 
Graphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & AlgorithumsGraphs > Discrete structures , Data Structures & Algorithums
Graphs > Discrete structures , Data Structures & Algorithums
 
Data Structures & Algorithms
Data Structures & AlgorithmsData Structures & Algorithms
Data Structures & Algorithms
 
Turn over
Turn overTurn over
Turn over
 
Attribution Theories
Attribution TheoriesAttribution Theories
Attribution Theories
 
Attribution Theory
Attribution TheoryAttribution Theory
Attribution Theory
 
Absenteeism
AbsenteeismAbsenteeism
Absenteeism
 
HRM Employee Turnover
HRM Employee TurnoverHRM Employee Turnover
HRM Employee Turnover
 

Recently uploaded

Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 

Huffman > Data Structures & Algorithums