Binary Expression Trees

download Binary Expression Trees

of 14

Transcript of Binary Expression Trees

  • 8/14/2019 Binary Expression Trees

    1/14

    1

    CS308

    Data Structures

    An application of binary trees:

    Binary Expression Trees

  • 8/14/2019 Binary Expression Trees

    2/14

    2

    A special kind of binary tree in which:

    1. Each leaf node contains a single operand

    2. Each nonleaf node contains asingle binaryoperator

    3. The left and right subtrees of an operator

    node represent subexpressions that mustbe evaluated beforeapplying the operator at

    the root of the subtree.

    A Binary Expression Tree is . . .

  • 8/14/2019 Binary Expression Trees

    3/14

    3

    A Four-Level Binary Expression

    *

    -

    8 5

    /

    +

    4

    3

    2

  • 8/14/2019 Binary Expression Trees

    4/14

    4

    Levels Indicate Precedence

    The levels of the nodes in the tree indicate

    their relative precedence of evaluation (wedo not need parentheses to indicate precedence).

    Operations at higher levels of the tree areevaluated later than those below them.

    The operation at the root is always the lastoperation performed.

  • 8/14/2019 Binary Expression Trees

    5/14

    5

    A Binary Expression Tree

    What value does it have?

    ( 4 + 2 ) * 3 = 18

    *

    +

    4

    3

    2

  • 8/14/2019 Binary Expression Trees

    6/14

    6

    Easy to generate the infix, prefix,

    postfix expressions (how?)

    Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) )

    Prefix: * - 8 5 / + 4 2 3

    Postfix: 8 5 - 4 2 + 3 / *

    *

    -

    8 5

    /

    +

    4

    3

    2

  • 8/14/2019 Binary Expression Trees

    7/14

    7

    Inorder Traversal: (A + H) / (M - Y)

    /

    +

    A H

    -

    M Y

    tree

    Print left subtree first Print right subtree last

    Print second

  • 8/14/2019 Binary Expression Trees

    8/14

    8

    Preorder Traversal: / + A H - M Y

    /

    +

    A H

    -

    M Y

    tree

    Print left subtree second Print right subtree last

    Print first

  • 8/14/2019 Binary Expression Trees

    9/14

    9

    /

    +

    A H

    -

    M Y

    tree

    Print left subtree first Print right subtree second

    Print last

    Postorder Traversal: A H + M Y - /

  • 8/14/2019 Binary Expression Trees

    10/14

    10

    class ExprTree

    *

    +

    4

    3

    2

    ExprTree

    ~ExprTree

    Build

    Evaluate.

    .

    .

    private:

    TreeNode*

    root;

  • 8/14/2019 Binary Expression Trees

    11/14

    11

    Each node contains two pointers

    struct TreeNode

    {

    InfoNode info ; // Data member

    TreeNode* left ; // Po in ter to left ch i ldTreeNode* right ; // Poin ter to r igh t ch ild

    };

    left info right

    NULL 6000whichType operand

    OPERAND 7

  • 8/14/2019 Binary Expression Trees

    12/14

    12

    InfoNode has 2 forms

    enum OpType { OPERATOR, OPERAND } ;

    struct InfoNode

    {OpType whichType;

    union // ANONYMOUS union

    {

    char operation ;

    int operand ;

    }};

    whichType operation

    OPERATOR +

    whichType operand

    OPERAND 7

  • 8/14/2019 Binary Expression Trees

    13/14

    int Eval ( TreeNode* ptr )

    { switch ( ptr->info.whichType )

    {

    case OPERAND : return ptr->info.operand ;

    case OPERATOR :

    switch ( tree->info.operation )

    {

    case + : return ( Eval ( ptr->left ) + Eval ( ptr->right ) ) ;

    case - : return ( Eval ( ptr->left ) - Eval ( ptr->right ) ) ;

    case * : return ( Eval ( ptr->left ) * Eval ( ptr->right ) ) ;

    case / : return ( Eval ( ptr->left ) / Eval ( ptr->right ) ) ;}

    }

    }

    13

  • 8/14/2019 Binary Expression Trees

    14/14

    14

    Building a Binary Expression Tree from an

    expression in prefix notation

    Insert new nodes, each time moving to the left

    until an operand has been inserted.

    Backtrack to the last operator, and put the

    next node to its right.

    Continue in the same pattern.