SlideShare a Scribd company logo
1 of 14
Huffman Coding
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:-
-S160980@rguktsklm.ac.in

More Related Content

What's hot

Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequenceKiran K
 
1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfaSampath Kumar S
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler DesignKuppusamy P
 
Genetic algorithms
Genetic algorithmsGenetic algorithms
Genetic algorithmsSaiful Islam
 
Error control coding bch, reed-solomon etc..
Error control coding   bch, reed-solomon etc..Error control coding   bch, reed-solomon etc..
Error control coding bch, reed-solomon etc..Madhumita Tamhane
 
Shannon-Fano algorithm
Shannon-Fano algorithmShannon-Fano algorithm
Shannon-Fano algorithmMANISH T I
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)swapnac12
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 

What's hot (20)

Coding
CodingCoding
Coding
 
Convolution Codes
Convolution CodesConvolution Codes
Convolution Codes
 
Longest common subsequence
Longest common subsequenceLongest common subsequence
Longest common subsequence
 
1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa
 
Matched filter
Matched filterMatched filter
Matched filter
 
Basics of coding theory
Basics of coding theoryBasics of coding theory
Basics of coding theory
 
5 linear block codes
5 linear block codes5 linear block codes
5 linear block codes
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Hamming codes
Hamming codesHamming codes
Hamming codes
 
Reed solomon codes
Reed solomon codesReed solomon codes
Reed solomon codes
 
Genetic algorithms
Genetic algorithmsGenetic algorithms
Genetic algorithms
 
Error control coding bch, reed-solomon etc..
Error control coding   bch, reed-solomon etc..Error control coding   bch, reed-solomon etc..
Error control coding bch, reed-solomon etc..
 
Huffman tree
Huffman tree Huffman tree
Huffman tree
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Linear Predictive Coding
Linear Predictive CodingLinear Predictive Coding
Linear Predictive Coding
 
Shannon-Fano algorithm
Shannon-Fano algorithmShannon-Fano algorithm
Shannon-Fano algorithm
 
Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)Performance analysis(Time & Space Complexity)
Performance analysis(Time & Space Complexity)
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
SHA 1 Algorithm
SHA 1 AlgorithmSHA 1 Algorithm
SHA 1 Algorithm
 
Secure Hash Algorithm
Secure Hash AlgorithmSecure Hash Algorithm
Secure Hash Algorithm
 

Similar to Huffman Coding: Compression Using Variable-Length Binary Codes

Huffman coding || Huffman Tree
Huffman coding || Huffman TreeHuffman coding || Huffman Tree
Huffman coding || Huffman TreeGurunadh Guru
 
HuffmanCoding01.doc
HuffmanCoding01.docHuffmanCoding01.doc
HuffmanCoding01.docQwertty3
 
j001adcpresentation-2112170415 23.pdf
j001adcpresentation-2112170415      23.pdfj001adcpresentation-2112170415      23.pdf
j001adcpresentation-2112170415 23.pdfHarshSharma71048
 
Huffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh AgarwalHuffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh AgarwalEkansh Agarwal
 
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).pdfMuhammadUmerIhtisham
 
Hufman coding basic
Hufman coding basicHufman coding basic
Hufman coding basicradthees
 
Farhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman codingFarhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman codingFarhana Shaikh
 
Error Detection N Correction
Error Detection N CorrectionError Detection N Correction
Error Detection N CorrectionAnkan Adhikari
 
DSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdfDSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdfGaneshPawar819187
 
Module-IV 094.pdf
Module-IV 094.pdfModule-IV 094.pdf
Module-IV 094.pdfSamrajECE
 

Similar to Huffman Coding: Compression Using Variable-Length Binary Codes (20)

Huffman coding || Huffman Tree
Huffman coding || Huffman TreeHuffman coding || Huffman Tree
Huffman coding || Huffman Tree
 
Huffman coding01
Huffman coding01Huffman coding01
Huffman coding01
 
HuffmanCoding01.doc
HuffmanCoding01.docHuffmanCoding01.doc
HuffmanCoding01.doc
 
j001adcpresentation-2112170415 23.pdf
j001adcpresentation-2112170415      23.pdfj001adcpresentation-2112170415      23.pdf
j001adcpresentation-2112170415 23.pdf
 
Huffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh AgarwalHuffman Algorithm and its Application by Ekansh Agarwal
Huffman Algorithm and its Application by Ekansh Agarwal
 
Komdat-Kompresi Data
Komdat-Kompresi DataKomdat-Kompresi Data
Komdat-Kompresi Data
 
Python lecture 06
Python lecture 06Python lecture 06
Python lecture 06
 
Huffman analysis
Huffman analysisHuffman analysis
Huffman analysis
 
Lossless
LosslessLossless
Lossless
 
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
 
Lossless
LosslessLossless
Lossless
 
Huffman
HuffmanHuffman
Huffman
 
Huffman
HuffmanHuffman
Huffman
 
Hufman coding basic
Hufman coding basicHufman coding basic
Hufman coding basic
 
Farhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman codingFarhana shaikh webinar_huffman coding
Farhana shaikh webinar_huffman coding
 
Error Detection N Correction
Error Detection N CorrectionError Detection N Correction
Error Detection N Correction
 
Arithmetic Coding
Arithmetic CodingArithmetic Coding
Arithmetic Coding
 
Data structures' project
Data structures' projectData structures' project
Data structures' project
 
DSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdfDSA Presentetion Huffman tree.pdf
DSA Presentetion Huffman tree.pdf
 
Module-IV 094.pdf
Module-IV 094.pdfModule-IV 094.pdf
Module-IV 094.pdf
 

Recently uploaded

Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 

Recently uploaded (20)

Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 

Huffman Coding: Compression Using Variable-Length Binary Codes

  • 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
  • 10. 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
  • 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