SlideShare a Scribd company logo
1 of 14
Huffman Coding
bY P.GuruNadh
IIIT SRIKAKULAM
Huffman Coding
● Huffman coding is proposed by David A. Huffman in 1952
● It is the one of the application of Binary Search Trees.
● It is Used for compression of Files.
● Compression is Nothing but reducing the size.
● We use this Huffman coding in WinRar,WinZip,7zip Applications etc..,
Huffman Tree
● It is used for to give unique encoding to a particular code or string
● Properties Of Huffman Tree
○ It should contain root node
○ Internal nodes are indicated with circles( ) and it should contain weights.
○ External nodes are indicated with squares( )and they should contain Frequency &
Characters.
Normal Encoding
● Let we take an example
● Let there are 7 characters in a string and each character is represented in 8
bits(ASCII CODES)
ASCII
a-97
b-98
c-100
d-101
BINARY(8 bits)
a-01100001
b-01100010
c-01100011
d-01100100
For representing abcdaab we need 7*8=56 bits
To reduce the bits we go for frequent characters.
Binary Encoding Representation
a b c d
a a b
01100001 01100010 01100011 01100100 01100001 01100001
01100001 01100010
Frequent Characters Technique
● In frequent characters technique the frequent occur character will take less bits and
rarely occur character will take large bits.
⮚ Let’s take the previous example , in this method we first calculate frequency of
characters.
⮚ Highest frequency character is represented with less bits and lowest frequency
character is represented with large bits.
Representation
a-0
b-10
c-110
d-111
By this technique for representing abcdaab we
need only 13 bits.
a b c d
a a b
0 10 110 111
0 0 10
Construction Of Huffman Tree
1.Scan text to be compressed and count occurrence of all characters.
2.Sort characters based on number of occurrences in text.
3.Take 2 characters which are having least frequency as leaf nodes.
4.And sum the data of that nodes and take it as it’s parent node.
5.Take next least occurred node and compare with parent node.
6.If compared node > parent node place at right side of parent node.
7.Else place at left side of parent node and repeat the procedure from step 4.
8.Perform a traversal of tree to determine all code words.
Huffman Tree Logic
HT( )
{
N=Priority Queue ;
Sort(N);
for(i=1 to n-1)
{
create_Node; //Z
Z.left = min(Z);
Z.right = Next_min(N);
Z.frequency=Z.left + Z.right;
push(z.frequency)
}
}
Key words of Huffman Tree
● Internal path length
○ The longest path from root node to corresponding internal node.
● External path length
○ The longest path from root node to corresponding external node.
● Let n be the number of internal nodes then,
○ External path length = Internal path length+2*n
● External weighted nodes = External path length * Frequency of External Node
● Sum of External weighted nodes = storage
Examples
● String = ab ab cba ; Occurrences = a🡪3;b🡪3;c🡪1;space🡪2;eof🡪1;
1 1
c
eof
🡪
2
1 1
c
eof
🡪
2
1 1
c
eof
2 2
1
Space
c,eof=1;space=2;a,b=3
Continue….
Continue…..
1
c
eof
2 2
1
Space
4
🡪
1
c
eof
2 2
1
Space
4
🡪
1
c
eof
2 2
1
Space
4
3
a
Continue…
● Continue…..
1
c
eof
2 2
1
Space
43
1
c
eof
2 2
1
Space
43
🡪
7
a a 🡪
1
c
eof
2 2
Space
43
7
a
1
b
3
Continue..
● Encode with 0’s at left side and 1’s at right side nodes of every parent node to get the representation
of characters in bits.
🡪
10
0
1
Representation
b-0
a-10
c-1100
Space-111
eof-1101
Calculations For Example
● For Previous Example Huffman tree
● Internal Length = Sum of Internal Lengths of internal nodes
○ 1(7)+2(4)+3(2)+0(10)=6
● External length = Sum of External Lengths of External nodes
○ 4(c)+4(eof)+3(space)+2(a)+1(b)=14
● Storage = sum of weighted external nodes = sum of(external path
length*frequency)
○ (4*1)+(4*1)+(3*2)+(2*3)+(1*3) = 23
10,7,4,2 Are Internal Nodes
c,eof,space,a,b Are External
Nodes
1,1,2,3,3 Are Frequencies Of
External Nodes
Any Queries….?
Mail to:-
-S160552@rguktsklm.ac.in

More Related Content

What's hot

Huffman's Alforithm
Huffman's AlforithmHuffman's Alforithm
Huffman's Alforithm
Roohaali
 
Data compression huffman coding algoritham
Data compression huffman coding algorithamData compression huffman coding algoritham
Data compression huffman coding algoritham
Rahul Khanwani
 
Module 4 Arithmetic Coding
Module 4 Arithmetic CodingModule 4 Arithmetic Coding
Module 4 Arithmetic Coding
anithabalaprabhu
 

What's hot (19)

Huffman Coding
Huffman CodingHuffman Coding
Huffman Coding
 
Huffman Student
Huffman StudentHuffman Student
Huffman Student
 
Huffman coding
Huffman codingHuffman coding
Huffman coding
 
Lec32
Lec32Lec32
Lec32
 
Huffman Coding
Huffman CodingHuffman Coding
Huffman Coding
 
Huffman's Alforithm
Huffman's AlforithmHuffman's Alforithm
Huffman's Alforithm
 
Huffman Code Decoding
Huffman Code DecodingHuffman Code Decoding
Huffman Code Decoding
 
Huffman Algorithm By Shuhin
Huffman Algorithm By ShuhinHuffman Algorithm By Shuhin
Huffman Algorithm By Shuhin
 
Shannon Fano
Shannon FanoShannon Fano
Shannon Fano
 
Huffman coding
Huffman coding Huffman coding
Huffman coding
 
Text compression
Text compressionText compression
Text compression
 
Lossless
LosslessLossless
Lossless
 
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
 
Data compression huffman coding algoritham
Data compression huffman coding algorithamData compression huffman coding algoritham
Data compression huffman coding algoritham
 
Data Compression - Text Compression - Run Length Encoding
Data Compression - Text Compression - Run Length EncodingData Compression - Text Compression - Run Length Encoding
Data Compression - Text Compression - Run Length Encoding
 
Multimedia lossless compression algorithms
Multimedia lossless compression algorithmsMultimedia lossless compression algorithms
Multimedia lossless compression algorithms
 
Huffman
HuffmanHuffman
Huffman
 
Module 4 Arithmetic Coding
Module 4 Arithmetic CodingModule 4 Arithmetic Coding
Module 4 Arithmetic Coding
 
Arithmetic coding
Arithmetic codingArithmetic coding
Arithmetic coding
 

Similar to Huffman coding || Huffman Tree

Error Detection N Correction
Error Detection N CorrectionError Detection N Correction
Error Detection N Correction
Ankan Adhikari
 

Similar to Huffman coding || Huffman Tree (20)

Python lecture 06
Python lecture 06Python lecture 06
Python lecture 06
 
HuffmanCoding01.doc
HuffmanCoding01.docHuffmanCoding01.doc
HuffmanCoding01.doc
 
DSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdfDSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdf
 
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
 
Komdat-Kompresi Data
Komdat-Kompresi DataKomdat-Kompresi Data
Komdat-Kompresi Data
 
j001adcpresentation-2112170415 23.pdf
j001adcpresentation-2112170415      23.pdfj001adcpresentation-2112170415      23.pdf
j001adcpresentation-2112170415 23.pdf
 
Huffman analysis
Huffman analysisHuffman analysis
Huffman analysis
 
RSA
RSARSA
RSA
 
Hufman coding basic
Hufman coding basicHufman coding basic
Hufman coding basic
 
Error Detection N Correction
Error Detection N CorrectionError Detection N Correction
Error Detection N Correction
 
AES-Advanced Encryption Standard
AES-Advanced Encryption StandardAES-Advanced Encryption Standard
AES-Advanced Encryption Standard
 
Cryptographic Algorithms: DES and RSA
Cryptographic Algorithms: DES and RSACryptographic Algorithms: DES and RSA
Cryptographic Algorithms: DES and RSA
 
Mysql Performance Optimization Indexing Algorithms and Data Structures
Mysql Performance Optimization Indexing Algorithms and Data StructuresMysql Performance Optimization Indexing Algorithms and Data Structures
Mysql Performance Optimization Indexing Algorithms and Data Structures
 
Arithmetic Coding
Arithmetic CodingArithmetic Coding
Arithmetic Coding
 
Farhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman codingFarhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman coding
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
MIPS Architecture
MIPS ArchitectureMIPS Architecture
MIPS Architecture
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
Lossless
LosslessLossless
Lossless
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
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
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
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
 
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in  Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in Uttam Nagar (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
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
 
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
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
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...
 

Huffman coding || Huffman Tree

  • 2. Huffman Coding ● Huffman coding is proposed by David A. Huffman in 1952 ● It is the one of the application of Binary Search Trees. ● It is Used for compression of Files. ● Compression is Nothing but reducing the size. ● We use this Huffman coding in WinRar,WinZip,7zip Applications etc..,
  • 3. Huffman Tree ● It is used for to give unique encoding to a particular code or string ● Properties Of Huffman Tree ○ It should contain root node ○ Internal nodes are indicated with circles( ) and it should contain weights. ○ External nodes are indicated with squares( )and they should contain Frequency & Characters.
  • 4. Normal Encoding ● Let we take an example ● Let there are 7 characters in a string and each character is represented in 8 bits(ASCII CODES) ASCII a-97 b-98 c-100 d-101 BINARY(8 bits) a-01100001 b-01100010 c-01100011 d-01100100 For representing abcdaab we need 7*8=56 bits To reduce the bits we go for frequent characters. Binary Encoding Representation a b c d a a b 01100001 01100010 01100011 01100100 01100001 01100001 01100001 01100010
  • 5. Frequent Characters Technique ● In frequent characters technique the frequent occur character will take less bits and rarely occur character will take large bits. ⮚ Let’s take the previous example , in this method we first calculate frequency of characters. ⮚ Highest frequency character is represented with less bits and lowest frequency character is represented with large bits. Representation a-0 b-10 c-110 d-111 By this technique for representing abcdaab we need only 13 bits. a b c d a a b 0 10 110 111 0 0 10
  • 6. Construction Of Huffman Tree 1.Scan text to be compressed and count occurrence of all characters. 2.Sort characters based on number of occurrences in text. 3.Take 2 characters which are having least frequency as leaf nodes. 4.And sum the data of that nodes and take it as it’s parent node. 5.Take next least occurred node and compare with parent node. 6.If compared node > parent node place at right side of parent node. 7.Else place at left side of parent node and repeat the procedure from step 4. 8.Perform a traversal of tree to determine all code words.
  • 7. Huffman Tree Logic HT( ) { N=Priority Queue ; Sort(N); for(i=1 to n-1) { create_Node; //Z Z.left = min(Z); Z.right = Next_min(N); Z.frequency=Z.left + Z.right; push(z.frequency) } }
  • 8. Key words of Huffman Tree ● Internal path length ○ The longest path from root node to corresponding internal node. ● External path length ○ The longest path from root node to corresponding external node. ● Let n be the number of internal nodes then, ○ External path length = Internal path length+2*n ● External weighted nodes = External path length * Frequency of External Node ● Sum of External weighted nodes = storage
  • 9. Examples ● String = ab ab cba ; Occurrences = a🡪3;b🡪3;c🡪1;space🡪2;eof🡪1; 1 1 c eof 🡪 2 1 1 c eof 🡪 2 1 1 c eof 2 2 1 Space c,eof=1;space=2;a,b=3
  • 11. Continue… ● Continue….. 1 c eof 2 2 1 Space 43 1 c eof 2 2 1 Space 43 🡪 7 a a 🡪 1 c eof 2 2 Space 43 7 a 1 b 3
  • 12. Continue.. ● Encode with 0’s at left side and 1’s at right side nodes of every parent node to get the representation of characters in bits. 🡪 10 0 1 Representation b-0 a-10 c-1100 Space-111 eof-1101
  • 13. Calculations For Example ● For Previous Example Huffman tree ● Internal Length = Sum of Internal Lengths of internal nodes ○ 1(7)+2(4)+3(2)+0(10)=6 ● External length = Sum of External Lengths of External nodes ○ 4(c)+4(eof)+3(space)+2(a)+1(b)=14 ● Storage = sum of weighted external nodes = sum of(external path length*frequency) ○ (4*1)+(4*1)+(3*2)+(2*3)+(1*3) = 23 10,7,4,2 Are Internal Nodes c,eof,space,a,b Are External Nodes 1,1,2,3,3 Are Frequencies Of External Nodes