TREE (JAVA)

download TREE (JAVA)

of 27

Transcript of TREE (JAVA)

  • 8/3/2019 TREE (JAVA)

    1/27

    CHAPTER 6 :CHAPTER 6 : TREETREE

    6.1 INTRODUCTION

    6.1.1 What is Tree?6.1.2 What is Binary Tree?. .

    6.1.4 Tree Traversal

    .6.2.1 What is Expression Tree6.2.2 Building the expression tree

    6.2.3 Traversing the expression tree6.2.4 Evaluating the expression value

    1Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    2/27

    Introduction to TreeIntroduction to TreeWhat is Tree ?

    A tree is a non-linearor two-dimensional data structurewith special properties.

    Tree nodes contains two or more link

    (Binary Tree :contains nodes with two links)

    A tree can be divided into three subset :

    root

    Second subset left sub tree

    Third subset right sub tree left subtree right subtree

    2Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    3/27

    IntroductionIntroduction to Tree (cont)to Tree (cont)

    Tree com onents re:

    Root node first node in a tree.Child each link in the root node refers to.

    tree)

    Right child- first node in right sub tree (root node of rightsub tree)

    Siblings nodes in the branch and same level.

    Lea node a node with no children.

    3Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    4/27

    Binary TreeBinary Tree

    What is Binary TreeBT can have maximum of two child (branches) only .

    Left sub tree is built on the left side of a node

    Binary tree graphical representation

    Right sub tree is built on the right side of a node

    oo no e

    BRight SubtreeLeft Subtree

    CNode A and D are siblings

    Node A and C is a Leaf

    4Intersession May 2009 UiTMT (MP, EZ, NIK)

    Node C is Child node of D

  • 8/3/2019 TREE (JAVA)

    5/27

    Binary Tree (cont)Binary Tree (cont)

    Example : ABC Binary Tree

    Descriptions :

    B C

    -

    - node B is the left subtree of node A

    - node C is the right subtree of node A-

    eve

    Level 1

    G

    F

    H I

    (eg : no right subtree for node E)

    - node D, G, H, I is a leaf (has null left

    and ri ht subtree)

    Level 2

    - the depth of Tree ABC is 3 (max level,where first level starts with 0)

    Level 3ABC

    5Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    6/27

    Binary Tree TypesBinary Tree Types

    a) Strictly Binary Tree (SBT)

    Each node thats not a leaf has both left and right subtree

    Formula to calculate the number of nodes :2n-1 where n is number of levels

    Example : 2 (4) 1 7 nodes

    A Level 0

    B C

    D E

    Level 1

    GF

    Level 3

    6Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    7/27

    Binary Tree TypesBinary Tree Types ((cont)cont)

    b) Full Binary Tree (FBT)

    A tree with leaves onl at the last level.

    Example ofleaf: Node H, I, J, K, L, M, N, OLevel 0

    Level 1C

    Level 2D E F G

    Level 3

    IH ONMLKJ

    7Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    8/27

    Binary Tree TypesBinary Tree Types ((cont)cont)

    Formula to calculate the number of nodes in FBT:

    ,

    have 2 x M nodes.Example : if level 2 has 4 nodes then

    eve as x no es

    b) At Level L, each FBT has 2L nodesExample :

    at level 3 there will be 23 8 nodesc) Total nodes in FBT is addition of all nodes at each level.

    Example : with 3 levelere are + + + no es

    8Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    9/27

    Tree TraversalTree Traversal

    A tree data structure can store many information in a particular

    order to access and store.

    Accessing data in tree data structure is called tree traversal.

    Tree traversal is done recursively. Whenever a subtree is met,

    the traversal order starts again

    There are three ways to traverse the tree

    Inordertraversal (left, root, right)

    reor er raversa roo , e , r gPostordertraversal (left, right, root)

    9Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    10/27

    Tree Traversal (cont)Tree Traversal (cont)

    INORDER traversal

    For each tree or subtree traversal starts

    1. fromLEFTnode,

    3. theRIGHTnode

    A

    B C

    D E

    Example : D B E A C

    10Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    11/27

    Tree Traversal (cont)Tree Traversal (cont)

    PREORDER traversal

    For each tree or subtree traversal starts

    1. fromROOTnode,

    3. theRIGHTnode

    A

    B C

    Example : A B D E C

    11Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    12/27

    Tree Traversal (cont)Tree Traversal (cont)

    POSTORDER traversal

    For each tree or subtree traversal starts

    1. fromLEFTnode,

    3. theROOTnode

    B C

    D E

    Example : D E B C A

    12Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    13/27

    Tree Traversal (cont)Tree Traversal (cont)

    Example (a) :

    B C

    D F GE

    H

    nor er Preorder A B D H E C F G

    Postorder H D E B F G C A

    13Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    14/27

    Tree Traversal (cont)Tree Traversal (cont)

    Example (b) :

    Q R

    W

    X

    Y

    nor er Preorder P Q W Y X R

    Postorder Y W X Q R P

    14Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    15/27

    ExpressionExpression TreeTree

    What is Expression Tree

    one of t e app ications of Binary Tree (BT)

    used to re resent arithmetic ex ression

    (infix, prefix and postfix)

    .

    expression must be converted to prefix or postfix

    expression in order to build the expression tree

    prefix or postfix expression will produce the same expression

    tree if it comesfrom the same infix expression

    15Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    16/27

    Building ExpressionBuilding Expression TreeTree

    Building Expression Tree

    a) Using prefix expression

    Readprefix expression from LEFT to RIGHT.

    Any operator must be at the ROOT, and any operand

    must be at the leaf

    Always open the LEFT branch first until no more branch

    can be opened. Then open the RIGHT branch.

    16Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    17/27

    Building ExpressionBuilding Expression Tree (Tree (cont)cont)

    Example (a) :Infix => (A + B) * C * ( D - E)

    prefix => * * + A B C D E

    Le t Ri ht

    *

    * -

    + D E

    A B

    C

    17Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    18/27

    Building ExpressionBuilding Expression Tree (Tree (cont)cont)

    Example (b) :refix => + A * - B C $ D * E F

    Left Right

    +

    A *

    - $

    *

    B

    E F

    18Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    19/27

    Building ExpressionBuilding Expression Tree (cont)Tree (cont)

    b Usin ost ix ex ression

    Readpostfix expression from RIGHT to LEFT.

    be at the leaf

    Always open the RIGHT branch first until no more branch. .

    19Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    20/27

    Building ExpressionBuilding Expression Tree (Tree (cont)cont)

    Example (a) :Infix => A + B * C * D - E

    postfix => A B + C * D E - *

    * -

    + D E

    A

    C

    20Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    21/27

    Building ExpressionBuilding Expression Tree (Tree (cont)cont)

    Example (b) :

    postfix => A B C D E F * $ * +Left Right

    +

    A *

    - $

    DB C*

    21Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    22/27

    ExpressionExpression TreeTree TraversalTraversal

    Traversing The Expression Tree

    Applying a different traversing technique to an expressiontree will result a different mathematical expression.

    There are three traversing technique :

    INORDER Traversal gives infix expression

    (LEFT, ROOT, RIGHT) (OPERAND,OPERATOR,OPERAND)PREORDER Traversal gives prefix expression

    , , , ,POSTORDER Traversal gives postfix expression

    (LEFT, RIGHT, ROOT) (OPERAND,OPERAND,OPERATOR)

    22Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    23/27

    Expression TreeExpression Tree Traversal (cont)Traversal (cont)

    Example (a) :

    *

    * -

    + D EC

    B

    => + * * -

    Preorder => * * + A B C D EPostorder => A B + C * D E - *

    23Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    24/27

    Expression TreeExpression Tree Traversal (cont)Traversal (cont)

    Example (b) :

    -F

    C

    $ +

    +

    BA

    Inorder => A + B $ C D + E $ FPreorder => $ - $ + A B C + D E FPostorder => A B + C D E + - F

    24Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    25/27

    Expression TreeExpression Tree EvaluationEvaluation

    Evaluating Expression Tree

    Steps :

    Replace leaf(operand or variable) with the given value

    Start with LEFT leaf

    then OPERATOR andor every su tree

    Evaluate the expression tree using the arithmetic process.

    25Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    26/27

    Expression TreeExpression Tree Evaluation (cont)Evaluation (cont)

    Example (a) :

    144

    $

    -F

    212

    $ $16 4

    DC

    B

    +

    A

    C

    2 2

    2 2 2

    26Intersession May 2009 UiTMT (MP, EZ, NIK)

  • 8/3/2019 TREE (JAVA)

    27/27

    Expression TreeExpression Tree Evaluation (cont)Evaluation (cont)

    Example (b) :

    if A= 5, B = 4, C=3, D=2, E=1 then RESULT is -19

    -19

    -

    A*

    524

    C

    *B

    $ 2

    46

    ED 2 1

    27Intersession May 2009 UiTMT (MP, EZ, NIK)