Professional Documents
Culture Documents
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
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
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
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
ABC
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
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
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
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)
Example : D B E A C
10
Example : A B D E C
11
Example : D E B C A
12
B D H E F
C G
Q W Y
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
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.
16
17
18
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.
19
* * + A B
Intersession May 2009 UiTMT (MP, EZ, NIK) 20
D E
* $ * F
21
22
23
25
12 16 $ 4
A + B
4
C
2 2
26
5
B
24
* C
6
$ D
4 3
2
E
27