Top Banner

of 30

Computer Notes - Data Structures - 24

Apr 06, 2018

Download

Documents

ecomputernotes
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/3/2019 Computer Notes - Data Structures - 24

    1/30

    1

    Class No.24

    Data Structures

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    2/30

    2

    Huffman Encoding

    Huffman code is method for the compression forstandard text documents.

    It makes use of a binary tree to develop codes ofvarying lengths for the letters used in the original

    message.

    Huffman code is also part of the JPEG imagecompression scheme.

    The algorithm was introduced by David Huffmanin 1952 as part of a course assignment at MIT.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    3/30

    3

    Huffman Encoding

    To understand Huffman encoding, it isbest to use a simple example.

    Encoding the 32-character phrase:"traversing threaded binary trees",

    If we send the phrase as a message in anetwork using standard 8-bit ASCII codes,we would have to send 8*32= 256 bits.

    Using the Huffman algorithm, we can sendthe message with only 116 bits.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    4/30

    4

    Huffman Encoding

    List all the letters used, including the "space"character, along with the frequency with whichthey occur in the message.

    Consider each of these (character,frequency)

    pairs to be nodes; they are actually leaf nodes,as we will see.

    Pick the two nodes with the lowest frequency,

    and if there is a tie, pick randomly amongstthose with equal frequencies.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    5/30

    5

    Huffman Encoding

    Make a new node out of these two, and makethe two nodes its children.

    This new node is assigned the sum of thefrequencies of its children.

    Continue the process of combining the twonodes of lowest frequency until only one node,the root, remains.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    6/30

    6

    Huffman Encoding

    Original text:traversing threaded binary treessize: 33 characters (space and newline)

    NL : 1SP : 3a : 3b : 1

    d : 2e : 5g : 1h : 1

    i : 2n : 2r : 5s : 2t : 3v : 1y : 1

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    7/30

    7

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2

    2 is equal to sumof the frequencies ofthe two children nodes.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    8/30

    8

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2

    There a number of ways to combinenodes. We have chosen just one suchway.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    9/30

    9

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    10/30

    10

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    4 4

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    11/30

    11

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    5444

    6

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    12/30

    12

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    5444

    86 9 10

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    13/30

    13

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    5444

    86

    14

    9

    19

    10

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    14/30

    14

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    5444

    86

    14

    9

    19

    10

    33

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    15/30

    15

    Huffman Encoding

    List all the letters used, including the "space"character, along with the frequency with whichthey occur in the message.

    Consider each of these (character,frequency)pairs to be nodes; they are actually leaf nodes,as we will see.

    Pick the two nodes with the lowest frequency,and if there is a tie, pick randomly amongstthose with equal frequencies.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    16/30

    16

    Huffman Encoding

    Make a new node out of these two, and makethe two nodes its children.

    This new node is assigned the sum of the

    frequencies of its children. Continue the process of combining the two

    nodes of lowest frequency until only one node,the root, remains.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    17/30

    17

    Huffman Encoding

    Start at the root. Assign 0 to left branch and 1 tothe right branch.

    Repeat the process down the left and right

    subtrees. To get the code for a character, traverse the tree

    from the root to the character leaf node and readoff the 0 and 1 along the path.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    18/30

    18

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    5444

    86

    14

    9

    19

    10

    3310

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    19/30

    19

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    5444

    86

    14

    9

    19

    10

    3310

    1010

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    20/30

    20

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    5444

    86

    14

    9

    19

    10

    3310

    1010

    1 010

    1010

    10 10 1 0 10

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    21/30

    21

    Huffman Encoding

    v

    1

    y

    1

    SP

    3

    r

    5

    h

    1

    e

    5

    g

    1

    b

    1

    NL

    1

    s

    2

    n

    2

    i

    2

    d

    2

    t3

    a

    3

    2 2 2

    5444

    86

    14

    9

    19

    10

    3310

    1010

    1 010

    1010

    10 10 1 0 10

    1 0 10 10

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    22/30

    22

    Huffman Encoding

    Huffman character codes

    NL 10000SP 1111a 000

    b 10001d 0100e 101g 10010h 10011i 0101

    n 0110r 110s 0111t 001v 11100y 11101

    Notice that the code isvariable length.

    Letters with higherfrequencies have shortercodes.

    The tree could have beenbuilt in a number of ways;

    each would yieldeddifferent codes but thecode would still beminimal.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    23/30

    23

    Huffman Encoding

    Original: traversing threaded binary trees

    Encoded:

    0011100001110010111001110101011010010111100110011110101000010010101001

    11110000101011000011011101111100111010110101110000

    t r a v e

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    24/30

    24

    Huffman Encoding

    Original: traversing threaded binary treesWith 8 bits per character, length is 264.

    Encoded:001110000111001011100111010101101001011110011001111010100001001010100111110000101011000011011101111100111

    010110101110000

    Compressed into 122 bits, 54% reduction.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    25/30

    25

    Mathematical Properties of

    Binary Trees

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    26/30

    26

    Properties of Binary Tree

    Property: A binary tree with N internal nodeshas N+1 external nodes.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    27/30

    27

    Properties of Binary Tree

    A binary tree with N internal nodes has N+1 external nodes.

    D F

    B C

    G

    A

    E

    FE

    internal nodes: 9external nodes: 10

    external node

    internal node

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    28/30

    28

    Properties of Binary Tree

    Property: A binary tree with N internal nodes has2N links: N-1 links to internal nodes and N+1 links

    to external nodes.

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    29/30

    29

    Threaded Binary Tree

    Property: A binary tree with N internal nodes has 2N links: N-1 links to

    internal nodes and N+1 links to external nodes.

    D F

    B C

    G

    A

    E

    FE

    Internal links: 8External links: 10

    external link

    internal link

    http://ecomputernotes.com

    http://ecomputernotes.com/http://ecomputernotes.com/
  • 8/3/2019 Computer Notes - Data Structures - 24

    30/30

    30

    Properties of Binary Tree

    Property: A binary tree with N internal nodes has2N links: N-1 links to internal nodes and N+1 links

    to external nodes.

    In every rooted tree, each node, except theroot, has a unique parent.

    Every link connects a node to its parent, sothere are N-1 links connecting internal nodes.

    Similarly, each of the N+1 external nodes hasone link to its parent.

    Thus N-1+N+1=2Nlinks.

    http://ecomputernotes com

    http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/http://ecomputernotes.com/