Text Compression Huffman Coding

79
Text Compression Text Compression Huffman Coding Huffman Coding James Adkison James Adkison 02/07/2008 02/07/2008

description

Text Compression Huffman Coding. James Adkison 02/07/2008. Assumptions / Givens. A bit is represented by a ‘1’ or ‘0’ A byte is any combination of 8 bits All ASCII characters are stored in 1 byte, except the ‘\n’ character which is stored as two bytes the ‘\n’ and ‘\r’. Notation. - PowerPoint PPT Presentation

Transcript of Text Compression Huffman Coding

Page 1: Text Compression Huffman Coding

Text CompressionText CompressionHuffman CodingHuffman Coding

James AdkisonJames Adkison

02/07/200802/07/2008

Page 2: Text Compression Huffman Coding

Assumptions / GivensAssumptions / Givens

• A bit is represented by a ‘1’ or ‘0’A bit is represented by a ‘1’ or ‘0’

• A byte is any combination of 8 bitsA byte is any combination of 8 bits

• All ASCII characters are stored in 1 All ASCII characters are stored in 1 byte, except the ‘\n’ character which byte, except the ‘\n’ character which is stored as two bytes the ‘\n’ and ‘\r’is stored as two bytes the ‘\n’ and ‘\r’

Page 3: Text Compression Huffman Coding

NotationNotation

• Square brackets ‘[’ ‘]’ are range Square brackets ‘[’ ‘]’ are range inclusiveinclusive

• Parenthesis ‘(’ ‘)’ are range exclusiveParenthesis ‘(’ ‘)’ are range exclusive

• Example: [0, 6) includes 0 and excludes Example: [0, 6) includes 0 and excludes 6 so the range is 0 to 5 or [0, 5]6 so the range is 0 to 5 or [0, 5]

• Traversing a Hoffman Tree to the left Traversing a Hoffman Tree to the left produces a ‘0’ bit and the right produces a ‘0’ bit and the right produces a ‘1’ bitproduces a ‘1’ bit

Page 4: Text Compression Huffman Coding

DefinitionsDefinitions

• Bit string: any combination of two or Bit string: any combination of two or more bitsmore bits

• Text = ASCII text = Uncompressed Text = ASCII text = Uncompressed text = Decoded texttext = Decoded text

• Encoded text = Huffman encoding = Encoded text = Huffman encoding = Compressed textCompressed text

Page 5: Text Compression Huffman Coding

Definitions Continued…Definitions Continued…

• Leaf Node: Has 1 parent and [0, 1) Leaf Node: Has 1 parent and [0, 1) childrenchildren

• Non-leaf Node: Has 1 parent and [1, Non-leaf Node: Has 1 parent and [1, 2] children2] children

• Root Node: Has 0 parents and [0, 2] Root Node: Has 0 parents and [0, 2] childrenchildren

Page 6: Text Compression Huffman Coding

2 5

1

6

3 4

10

98

11

7

Root Node Non-leaf Node Leaf Node

Binary Binary TreeTree

Page 7: Text Compression Huffman Coding

Root Node Non-leaf Node Leaf Node

HuffmaHuffman Treen Tree0 1

0

0 0

0 11

11

Page 8: Text Compression Huffman Coding

‘000’

Root Node Non-leaf Node Leaf Node

HuffmaHuffman Treen Tree0 1

0

0 0

0 11

11

Page 9: Text Compression Huffman Coding

‘000’ ’010’

Root Node Non-leaf Node Leaf Node

HuffmaHuffman Treen Tree0 1

0

0 0

0 11

11

Page 10: Text Compression Huffman Coding

‘000’ ’010’ ‘011’

Root Node Non-leaf Node Leaf Node

HuffmaHuffman Treen Tree0 1

0

0 0

0 11

11

Page 11: Text Compression Huffman Coding

‘000’ ’010’ ‘011’ ‘101’

Root Node Non-leaf Node Leaf Node

HuffmaHuffman Treen Tree0 1

0

0 0

0 11

11

Page 12: Text Compression Huffman Coding

‘000’ ’010’ ‘011’

’11’

‘101’

Root Node Non-leaf Node Leaf Node

HuffmaHuffman Treen Tree0 1

0

0 0

0 11

11

Page 13: Text Compression Huffman Coding

‘000’ ’010’ ‘011’

’11’

‘101’

Root Node Non-leaf Node Leaf Node

HuffmaHuffman Treen Tree0 1

0

0 0

0 11

11

Page 14: Text Compression Huffman Coding

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

HuffmaHuffman Treen Tree

Page 15: Text Compression Huffman Coding

‘t’

‘y’

‘e’

‘w’

‘r’ ‘q’

0

10

1

10

‘y’ : 0

‘w’ : 1

‘t’ : 00

‘e’ : 01

‘r’ : 10

‘q’ : 11

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 61 bytes

Compression: 79.7 percent

HuffmaHuffman Treen Tree

Page 16: Text Compression Huffman Coding

‘t’

‘y’

‘e’

‘w’

‘r’ ‘q’

0

10

1

10

‘y’ : 0

‘w’ : 1

‘t’ : 00

‘e’ : 01

‘r’ : 10

‘q’ : 11

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 61 bytes

Compression: 79.7 percent

HuffmaHuffman Treen Tree

Decode: 1110110000

Page 17: Text Compression Huffman Coding

‘t’

‘y’

‘e’

‘w’

‘r’ ‘q’

0

10

1

10

‘y’ : 0

‘w’ : 1

‘t’ : 00

‘e’ : 01

‘r’ : 10

‘q’ : 11

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 61 bytes

Compression: 79.7 percent

HuffmaHuffman Treen Tree

Decode: 1110110000

1: w

Page 18: Text Compression Huffman Coding

‘t’

‘y’

‘e’

‘w’

‘r’ ‘q’

0

10

1

10

‘y’ : 0

‘w’ : 1

‘t’ : 00

‘e’ : 01

‘r’ : 10

‘q’ : 11

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 61 bytes

Compression: 79.7 percent

HuffmaHuffman Treen Tree

Decode: 1110110000

11: ww

11: q

Page 19: Text Compression Huffman Coding

‘t’

‘y’

‘e’

‘w’

‘r’ ‘q’

0

10

1

10

‘y’ : 0

‘w’ : 1

‘t’ : 00

‘e’ : 01

‘r’ : 10

‘q’ : 11

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 61 bytes

Compression: 79.7 percent

HuffmaHuffman Treen Tree

Decode: 1110110000

111: www

111: wq

111: qw

Page 20: Text Compression Huffman Coding

‘t’

‘y’

‘e’

‘w’

‘r’ ‘q’

0

10

1

10

‘y’ : 0

‘w’ : 1

‘t’ : 00

‘e’ : 01

‘r’ : 10

‘q’ : 11

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 61 bytes

Compression: 79.7 percent

HuffmaHuffman Treen Tree

Decode: 1110110000

111: www

111: wq

111: qw

Page 21: Text Compression Huffman Coding

‘t’

‘y’

‘e’

‘w’

‘r’ ‘q’

0

10

1

10

‘y’ : 0

‘w’ : 1

‘t’ : 00

‘e’ : 01

‘r’ : 10

‘q’ : 11

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 61 bytes

Compression: 79.7 percent

HuffmaHuffman Treen Tree

Decode: 1110110000

111: www

111: wq

111: qw

Page 22: Text Compression Huffman Coding

‘t’

‘y’

‘e’

‘w’

‘r’ ‘q’

0

10

1

10

‘y’ : 0

‘w’ : 1

‘t’ : 00

‘e’ : 01

‘r’ : 10

‘q’ : 11

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 61 bytes

Compression: 79.7 percent

BadBadHuffmaHuffman Treen Tree

Page 23: Text Compression Huffman Coding

‘t’

‘y’

‘e’

‘w’

‘r’ ‘q’

0

10

1

10

‘y’ : 0

‘w’ : 1

‘t’ : 00

‘e’ : 01

‘r’ : 10

‘q’ : 11

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 61 bytes

Compression: 79.7 percent

BadBadHuffmaHuffman Treen Tree

Encode: q w e r t y

Code: 11 1 01 10 00 0

Page 24: Text Compression Huffman Coding

Huffman Tree ConstructionHuffman Tree Construction

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Page 25: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Process Text File & Build Array of Process Text File & Build Array of NodesNodes

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘y’,1

Page 26: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Process Text File & Build Array of Process Text File & Build Array of NodesNodes

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘y’,1 ‘q’,1

Page 27: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Process Text File & Build Array of Process Text File & Build Array of NodesNodes

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘y’,1 ‘w’,1‘q’,1

Page 28: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Process Text File & Build Array of Process Text File & Build Array of NodesNodes

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘y’,1 ‘w’,1 ‘t’,1‘q’,1

Page 29: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Process Text File & Build Array of Process Text File & Build Array of NodesNodes

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘y’,1 ‘w’,1 ‘t’,1 ‘e’,1‘q’,1

Page 30: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Process Text File & Build Array of Process Text File & Build Array of NodesNodes

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘y’,1 ‘w’,1 ‘t’,1 ‘r’,1‘e’,1‘q’,1

Page 31: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Process Text File & Build Array of Process Text File & Build Array of NodesNodes

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘y’,2 ‘w’,1 ‘t’,1 ‘r’,1‘e’,1‘q’,1

Page 32: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Process Text File & Build Array of Process Text File & Build Array of NodesNodes

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘y’,57 ‘w’,58 ‘t’,40 ‘r’,47‘e’,43‘q’,55

Page 33: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Process Text File & Build Array of Process Text File & Build Array of NodesNodes

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘y’,57 ‘w’,58 ‘t’,40 ‘r’,47‘e’,43‘q’,55

Each distinct character onlyappears in the array once along

with the # of times it occurs

Page 34: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Sort the arraySort the array

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40 ‘r’,47 ‘q’,55 ‘w’,58‘y’,57‘e’,43

Page 35: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40 ‘r’,47 ‘q’,55 ‘w’,58‘y’,57‘e’,43

Page 36: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40

‘r’,47 ‘q’,55 ‘w’,58‘y’,57

‘e’,43

83

Page 37: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40

‘r’,47 ‘q’,55 ‘w’,58‘y’,57

‘e’,43

83

Page 38: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40

‘r’,47 ‘q’,55 ‘w’,58‘y’,57

‘e’,43

83

Page 39: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40

‘w’,58‘y’,57

‘e’,43

83

‘r’,47 ‘q’,55

102

Page 40: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40

‘w’,58‘y’,57

‘e’,43

83

‘r’,47 ‘q’,55

102

Page 41: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40

‘w’,58‘y’,57

‘e’,43

83

‘r’,47 ‘q’,55

102

Page 42: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40 ‘e’,43

83

‘r’,47 ‘q’,55

102

‘y’,57 ‘w’,58

115

Page 43: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40 ‘e’,43

83

‘r’,47 ‘q’,55

102

‘y’,57 ‘w’,58

115

Page 44: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40 ‘e’,43

83

‘r’,47 ‘q’,55

102

‘y’,57 ‘w’,58

115

Page 45: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:‘t’,40 ‘e’,43

83

‘r’,47 ‘q’,55

102

‘y’,57 ‘w’,58

115185

Page 46: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:‘t’,40 ‘e’,43

83

‘r’,47 ‘q’,55

102‘y’,57 ‘w’,58

115 185

Page 47: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:‘t’,40 ‘e’,43

83

‘r’,47 ‘q’,55

102‘y’,57 ‘w’,58

115 185

Page 48: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40 ‘e’,43

83

‘r’,47 ‘q’,55

102‘y’,57 ‘w’,58

115 185

300

Page 49: Text Compression Huffman Coding

Huffman Tree Construction:Huffman Tree Construction:Constructing the treeConstructing the tree

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

‘t’,40 ‘e’,43

83

‘r’,47 ‘q’,55

102‘y’,57 ‘w’,58

115 185

300

Page 50: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

yqwteryqreytqywteweryqtwreqwewtryqwetytqrwqrtwtwqyqtwreqqywywytrwtqywqyqyewrqwyqwyrewqytqwtyrwyeqyrweytrtryytwrwqererrwtwqtretyytwtreryqqwewqywterqwyyqyqtweyqwreywqryqwreyqytqweytqweyqwreyqweyqwreyqwteryqwreyqwteryqwteryqwreyqwtreyqwtreyqwreyqwtreyqtwtryrereyqyqtwyweyrerrtqrwrwetqtrqywretqwwytqeyqwe

Input File:

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Page 51: Text Compression Huffman Coding

Huffman Tree:Huffman Tree:Compression ComputationCompression Computation

•NN : # of distinct characters : # of distinct characters•nnii : # of times a character at the i : # of times a character at the ithth

index occursindex occurs•bbii : # of bits used to encode a : # of bits used to encode a

character at the icharacter at the ithth index index• 8 : # of bits stored in a byte8 : # of bits stored in a byte

1

0

8/N

iii bn ‘y’,57 ‘w’,58 ‘t’,40 ‘r’,47‘e’,43‘q’,55

Page 52: Text Compression Huffman Coding

Compression Computation Compression Computation ExampleExample

• Produces:Produces:(57 * 2) + (55 * 3) + (58 * 2) + (40 * 3) + (43 * 3) + (47 * 3)(57 * 2) + (55 * 3) + (58 * 2) + (40 * 3) + (43 * 3) + (47 * 3)

88

= 98.125 -> 99 bytes= 98.125 -> 99 bytes

1

0

8/N

iii bn ‘y’,57 ‘w’,58 ‘t’,40 ‘r’,47‘e’,43‘q’,55

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

Page 53: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Page 54: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer:

Page 55: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer:

Page 56: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer:

Page 57: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: q

Page 58: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: q

Page 59: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: q

Page 60: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qw

Page 61: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qw

Page 62: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qw

Page 63: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qw

Page 64: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwe

Page 65: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwe

Page 66: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwe

Page 67: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwe

Page 68: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwer

Page 69: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwer

Page 70: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwer

Page 71: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwer

Page 72: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwert

Page 73: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwert

Page 74: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwert

Page 75: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwerty

Page 76: Text Compression Huffman Coding

‘y’,57

115

‘w’,58

‘t’,40

185

‘r’,47

83

300

‘e’,43 ‘q’,55

102

0

10

1

1

11

0

0

‘y’ : 00

‘w’ : 01

‘t’ : 100

‘e’ : 101

‘r’ : 110

‘q’ : 111

0

Huffman

Encoding:

Uncompressed: 300 bytes

Compressed: 99 bytes

Compression: 67 percent

HuffmaHuffman Treen Tree

Decode: 1110110111010000

Answer: qwerty

Page 77: Text Compression Huffman Coding

Huffman EncodingHuffman EncodingReal World ExampleReal World Example

• Huffman (C++)Huffman (C++)

• Text I/O DirectoryText I/O Directory

Page 78: Text Compression Huffman Coding

Huffman HomeworkHuffman Homework

1.1. Construct a Huffman tree for the Construct a Huffman tree for the following input file. You only need to following input file. You only need to show the final tree. These are the only show the final tree. These are the only characters in the file and the # of times characters in the file and the # of times they occur: (a, 50)(b, 60)(c, 70)(d, 80)they occur: (a, 50)(b, 60)(c, 70)(d, 80)

2.2. How many bytes will the compressed How many bytes will the compressed text file occupy?text file occupy?

Page 79: Text Compression Huffman Coding

Works CitedWorks Cited

• ““ASCII.” ASCII.” Wikipedia The Free EncyclopediaWikipedia The Free Encyclopedia. 2008. . 2008. 21 January 2008. <21 January 2008. <http://http://en.wikipedia.orgen.wikipedia.org/wiki/ASCII/wiki/ASCII>>

• Dewdney, A. K. Dewdney, A. K. The New Turing OmnibusThe New Turing Omnibus. New . New York: Henry Holt, 1989. Pages 345 – 350.York: Henry Holt, 1989. Pages 345 – 350.

• Line Termination: Operating Systems Use Line Termination: Operating Systems Use Different ConventionsDifferent Conventions. 21 January 2008. <. 21 January 2008. <http://homepage.smc.edu/morgan_david/CS41/linhttp://homepage.smc.edu/morgan_david/CS41/lineterminators.htmeterminators.htm>>