You are on page 1of 27

CHAPTER 6 : TREE

6.1 INTRODUCTION 6.1.1 What is Tree? 6.1.2 What is Binary Tree? 6.1.3 6 1 3 Binary Tree Types 6.1.4 Tree Traversal EXPRESSION TREE 6.2.1 What is Expression Tree 6.2.2 Building the expression tree g p 6.2.3 Traversing the expression tree 6.2.4 Evaluating the expression value

6.2 62

Intersession May 2009 UiTMT (MP, EZ, NIK)

Introduction to Tree
What is Tree ?
A tree is a non-linear or two-dimensional data structure with special properties. Tree nodes contains two or more link (Binary Tree :contains nodes with two links)
root

A tree can be divided into three subset : First subset root Second subset left sub tree Third subset right sub tree left subtree

right subtree

Intersession May 2009 UiTMT (MP, EZ, NIK)

Introduction to Tree (cont)


Tree components are: Root node first node in a tree. Child each link in the root node refers to. Left child first node in left sub tree (root node of left sub tree) Right child - first node in right sub tree (root node of right sub tree) Siblings nodes in the branch and same level. f Leaf node a node with no children.

Intersession May 2009 UiTMT (MP, EZ, NIK)

Binary Tree
What is Binary Tree
BT can have maximum of two child (branches) only . Left sub tree is built on the left side of a node Right sub tree is built on the right side of a node

Binary tree graphical representation


Root node R t d

Left Subtree

B
Right Subtree

A
Node A and C is a Leaf Node A and D are siblings Node C is Child node of D
Intersession May 2009 UiTMT (MP, EZ, NIK)

D C

Binary Tree (cont)


Example : ABC Binary Tree
Descriptions : - has 9 nodes - node B is the left subtree of node A - node C is the right subtree of node A - right subtree of node B and E is null D (eg : no right subtree for node E) - node D, G, H, I is a leaf (has null left and right subtree) g ) - the depth of Tree ABC is 3 (max level, where first level starts with 0)
A B E G C F H Level 0 L l Level 1 Level 2 I Level 3

ABC

Intersession May 2009 UiTMT (MP, EZ, NIK)

Binary 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 Example : where n is number of levels 2 (4) 1
A B D F G E C

7 nodes
Level 0 Level 1 Level 2 Level 3

Intersession May 2009 UiTMT (MP, EZ, NIK)

Binary Tree Types (cont)


b)

Full Binary Tree (FBT) A tree with leaves only at the last level. y Example of leaf: Node H, I, J, K, L, M, N, O
A Level 0

B D H I J E K L F M

Level 1 Level 2

G N O

Level 3

Intersession May 2009 UiTMT (MP, EZ, NIK)

Binary Tree Types (cont)


Formula to calculate the number of nodes in FBT:
a)

If FBT has M nodes at level L, then level L + 1 will L have 2 x M nodes. Example : if level 2 has 4 nodes then level h l l 3 has 2 x 4 8 nodes d

b) At Level L, each FBT has 2L nodes Example : at level 3 there will be 23

8 nodes

c) Total nodes in FBT is addition of all nodes at each level. Example : with 3 level there are 20 + 21 + 22 + 23 th 15 nodes d
Intersession May 2009 UiTMT (MP, EZ, NIK) 8

Tree 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 Inorder traversal (left, root, right) Preorder t P d traversal ( t left, right) l (root, l ft i ht) Postorder traversal (left, right, root)

Intersession May 2009 UiTMT (MP, EZ, NIK)

Tree Traversal (cont)


INORDER traversal For each tree or subtree traversal starts 1. from LEFT node, 2. the ROOT and then 3. the RIGHT node
A B D E C

Example : D B E A C

Intersession May 2009 UiTMT (MP, EZ, NIK)

10

Tree Traversal (cont)


PREORDER traversal For each tree or subtree traversal starts 1. from ROOT node, 2. the LEFT and then 3. the RIGHT node
A B D E C

Example : A B D E C

Intersession May 2009 UiTMT (MP, EZ, NIK)

11

Tree Traversal (cont)


POSTORDER traversal For each tree or subtree traversal starts 1. from LEFT node, 2. the RIGHT and then 3. the ROOT node
A B D E C

Example : D E B C A

Intersession May 2009 UiTMT (MP, EZ, NIK)

12

Tree Traversal (cont)


Example (a) :
A

B D H E F

C G

Inorder I d DHBEAFCG Preorder A B D H E C F G Postorder H D E B F G C A


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

Tree Traversal (cont)


Example (b) :
P

Q W Y

Inorder I d YWQ XPR Preorder P Q W Y X R Postorder Y W X Q R P


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

Expression Tree
What is Expression Tree
one of the applications of Binary Tree ( ) f h li i f i (BT) used to represent arithmetic expression p p (infix, prefix and postfix) can be built with prefix and postfix expression only Any infix only. 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 f p tree if it comesfrom the same infix expression
Intersession May 2009 UiTMT (MP, EZ, NIK) 15

Building Expression Tree


Building Expression Tree
a)

Using prefix expression Read prefix expression from LEFT to RIGHT. Any operator must be at the ROOT, and any operand must be at the leaf ust t e ea Always open the LEFT branch first until no more branch can be opened. Then open the RIGHT branch.

Intersession May 2009 UiTMT (MP, EZ, NIK)

16

Building Expression Tree (cont) (cont)


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

Intersession May 2009 UiTMT (MP, EZ, NIK)

17

Building Expression Tree (cont) (cont)


Example (b) : p prefix => + A * - B C $ D * E F Left Right
+ A B C D E * $ * F

Intersession May 2009 UiTMT (MP, EZ, NIK)

18

Building Expression Tree (cont)


b)

Using postfix expression gp f p Read postfix expression from RIGHT to LEFT. Any operator must be at the ROOT, and any operand must be at the leaf Always open the RIGHT branch first until no more branch can be opened Then open the LEFT branch. branch opened.

Intersession May 2009 UiTMT (MP, EZ, NIK)

19

Building Expression Tree (cont) (cont)


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

* * + A B
Intersession May 2009 UiTMT (MP, EZ, NIK) 20

D E

Building Expression Tree (cont) (cont)


Example (b) : postfix => A B C D E F * $ * + Left Right
+ A B C D E
Intersession May 2009 UiTMT (MP, EZ, NIK)

* $ * F
21

Expression Tree Traversal


Traversing The Expression Tree
Applying a different traversing technique to an expression tree will result a different mathematical expression. There are three traversing technique :
INORDER Traversal gives i fi expression O l i infix i (LEFT, ROOT, RIGHT) (OPERAND,OPERATOR,OPERAND) PREORDER Traversal gives prefix expression (ROOT, LEFT (ROOT LEFT, RIGHT) (OPERATOR OPERAND OPERAND) (OPERATOR,OPERAND,OPERAND) POSTORDER Traversal gives postfix expression (LEFT, RIGHT, ROOT) (OPERAND,OPERAND,OPERATOR)

Intersession May 2009 UiTMT (MP, EZ, NIK)

22

Expression Tree Traversal (cont)


Example (a) :
* * + A B D E

Inorder => A + B * C * D - E Preorder => * * + A B C D E Postorder => A B + C * D E - *

Intersession May 2009 UiTMT (MP, EZ, NIK)

23

Expression Tree Traversal (cont)


Example (b) :
$ $ + A B C + D E F

Inorder => A + B $ C D + E $ F Preorder => $ - $ + A B C + D E F Postorder => A B + C $ D E + - F $ os o de


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

Expression Tree Evaluation


Evaluating Expression Tree
Steps : Replace leaf (operand or variable) with the given value Start with LEFT leaf then OPERATOR and RIGHT f every sub tree for b Evaluate the expression tree using the arithmetic process. p g p

Intersession May 2009 UiTMT (MP, EZ, NIK)

25

Expression Tree Evaluation (cont)


Example (a) : if A= B =C=D=E=F=2 then RESULT is 144
144
$

12 16 $ 4
A + B

4
C

2 2

Intersession May 2009 UiTMT (MP, EZ, NIK)

26

Expression Tree Evaluation (cont)


Example (b) : if A= 5, B = 4, C=3, D=2, E=1 then RESULT is -19
-19
A

5
B

24
* C

6
$ D

4 3

2
E

Intersession May 2009 UiTMT (MP, EZ, NIK)

27

You might also like