Computer Notes - Data Structures - 23

Embed Size (px)

Citation preview

  • 8/3/2019 Computer Notes - Data Structures - 23

    1/31

    Class No.23

    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 - 23

    2/31

    Expression Tree

    The inner nodes contain operators while leafnodes contain operands.

    a

    c

    +

    b

    g

    *

    +

    +

    d

    *

    *

    e

    f

    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 - 23

    3/31

    Expression Tree

    The tree is binary because the operators arebinary.

    a

    c

    +

    b

    g

    *

    +

    +

    d

    *

    *

    e

    f

    http://ecomputernotes.com

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

    4/31

    Expression Tree

    This is not necessary. A unary operator (!, e.g.)will have only one subtree.

    a

    c

    +

    b

    g

    *

    +

    +

    d

    *

    *

    e

    f

    http://ecomputernotes.com

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

    5/31

    Expression Tree

    Inorder traversal yields: a+b*c+d*e+f*g

    a

    c

    +

    b

    g

    *

    +

    +

    d

    *

    *

    e

    f

    http://ecomputernotes.com

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

    6/31

    Enforcing Parenthesis

    void inorder(TreeNode* treeNode)

    {

    if( treeNode != NULL ){

    cout getLeft());

    cout

  • 8/3/2019 Computer Notes - Data Structures - 23

    7/31

    Expression Tree

    Inorder : (a+(b*c))+(((d*e)+f)*g)

    a

    c

    +

    b

    g

    *

    +

    +

    d

    *

    *

    e

    f

    http://ecomputernotes.com

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

    8/31

    Expression Tree

    Postorder traversal: a b c * + d e * f + g * +which is the postfix form.

    a

    c

    +

    b

    g

    *

    +

    +

    d

    *

    *

    e

    f

    http://ecomputernotes.com

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

    9/31

    Constructing Expression Tree

    Algorithm to convert postfix expression into anexpression tree.

    We already have an expression to convert aninfix expression to postfix.

    Read a symbol from the postfix expression.

    If symbol is an operand, put it in a one node treeand push it on a stack.

    If symbol is an operator, pop two trees from thestack, form a new tree with operator as the rootand T1 and T2 as left and right subtrees andpush this tree on the stack.

    http://ecomputernotes.com

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

    10/31

    Constructing Expression Tree

    a b + c d e + * *

    stack

    http://ecomputernotes.com

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

    11/31

    Constructing Expression Tree

    a b + c d e + * *

    ba

    Stack is growing left to right

    If symbol is anoperand, put it in

    a one node treeand push it on astack.

    top

    http://ecomputernotes.com

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

    12/31

    Constructing Expression Tree

    a b + c d e + * *

    ba

    Stack is growing left to right

    +

    If symbol is anoperator, pop two

    trees from the stack,form a new tree withoperator as the rootand T1 and T2 as leftand right subtrees andpush this tree on the

    stack.

    http://ecomputernotes.com

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

    13/31

    Constructing Expression Tree

    a b +c d e + * *

    ba

    + dc e

    http://ecomputernotes.com

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

    14/31

    Constructing Expression Tree

    a b +c d e+ * *

    ba

    + c

    ed

    +

    http://ecomputernotes.com

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

    15/31

    Constructing Expression Tree

    a b +c d e+* *

    ba

    +

    c

    ed

    +

    *

    http://ecomputernotes.com

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

    16/31

    Constructing Expression Tree

    a b +c d e+**

    ba

    +

    c

    ed

    +

    *

    *

    http://ecomputernotes.com

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

    17/31

    Other Uses of Binary Trees

    Huffman Encoding

    http://ecomputernotes.com

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

    18/31

    Huffman Encoding

    Data compression plays a significant role incomputer networks.

    To transmit data to its destination faster, it isnecessary to either increase the data rate of the

    transmission media or to simply send less data. Improvements with regard to the transmission

    media has led to increase in the rate.

    The other options is to send less data by means

    of data compression. Compression methods are used for text, images,

    voice and other types of data (space probes).

    http://ecomputernotes.com

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

    19/31

    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 image

    compression scheme.

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

    http://ecomputernotes.comhttp://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 - 23

    20/31

    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.

  • 8/3/2019 Computer Notes - Data Structures - 23

    21/31

    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.

  • 8/3/2019 Computer Notes - Data Structures - 23

    22/31

    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.

  • 8/3/2019 Computer Notes - Data Structures - 23

    23/31

    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

  • 8/3/2019 Computer Notes - Data Structures - 23

    24/31

    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.

  • 8/3/2019 Computer Notes - Data Structures - 23

    25/31

    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.

  • 8/3/2019 Computer Notes - Data Structures - 23

    26/31

    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

  • 8/3/2019 Computer Notes - Data Structures - 23

    27/31

    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

  • 8/3/2019 Computer Notes - Data Structures - 23

    28/31

    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

  • 8/3/2019 Computer Notes - Data Structures - 23

    29/31

    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

  • 8/3/2019 Computer Notes - Data Structures - 23

    30/31

    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

  • 8/3/2019 Computer Notes - Data Structures - 23

    31/31

    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