Sunteți pe pagina 1din 27

CHAPTERCHAPTER 66 :: TREETREE

6.1 INTRODUCTION

6.1.1 What is Tree? 6.1.2 What is Binary Tree? Binary Tree Types

6.1.4 Tree Traversal

6.2 EXPRESSION TREE

6.2.1 What is Expression Tree

6.2.2 Building the expression tree

6.2.3 Traversing the expression tree

6.2.4 Evaluating the expression value

IntroductionIntroduction toto TreeTree
IntroductionIntroduction toto TreeTree
What is Tree ? A tree is a non-linear or two-dimensional data structure with special
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

IntroductionIntroduction toto TreeTree (cont)(cont)

Tree components are: Root node – first node in a tree. Child – each link
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.
Leaf node – a node with no children.

BinaryBinary TreeTree

What is Binary Tree

BT – can have maximum of two child (branches) only . can have maximum of two child (branches) only .

Left sub tree is built on the left side of a nodeBT – can have maximum of two child (branches) only . Right sub tree is built

Right sub tree is built on the right side of a nodeonly . Left sub tree is built on the left side of a node Binary tree

Binary tree graphical representation

Left Subtree

Node A and C is a Leaf Node A and D are siblings

Node C is Child node of D

Root node

B
B
B
B
B
B
B

B

B
B
B
B

A

C

Right Subtree

D

BinaryBinary TreeTree (cont)(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 (eg : no right subtree for node E)

- node D, G, H, I is a leaf (has null left and right subtree)

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

D

B

G

E

A

ABC

C

H

Level 0

Level 1

F

Level 2

I

Level 3

BinaryBinary TreeTree TypesTypes

a) Strictly Binary Tree (SBT)

Each node that’s not a leaf has both left and right subtree not a leaf has both left and right subtree

Formula to calculate the number of nodes : to calculate the number of nodes :

Example :

2n-1

where n is number of levels

2 (4) – 1 7 nodes

D

B

F G

A

Level 0

 

C

Level 1

E

L eve

l 2

Level 3

BinaryBinary TreeTree TypesTypes ((cont)cont)

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

H

D

B

I

J

E

K

A

L

F

M

C

N

G

Level 0

Level 1

Level 2

O

Level 3

BinaryBinary TreeTree TypesTypes ((cont)cont)

Formula to calculate the number of nodes in FBT:

a ) If FBT has M nodes at level L, then level L + 1 will have 2 x M nodes.

Example :

if level 2 has 4 nodes then level 3 has 2 x 4 8 nodes

b) At Level L, each FBT has 2 L nodes Example :

at level 3 there will be 2 3 8 nodes

c) Total nodes in FBT is addition of all nodes at each level.

Example :

with 3 level there are 2

0

+ 2

1

+ 2

2

3

+ 2

15 nodes

TreeTree TraversalTraversal

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 . traversal.

Tree traversal is done recursively. Whenever a subtree is met , the traversal order starts again subtree is met, the traversal order starts again

There are three ways to traverse the tree three ways to traverse the tree

Inorder traversal traversal

Preorder t raversa l traversal

Postorder traversal traversal

(left, root, right) (root, left, right) (left, right, root)

TreeTree TraversalTraversal (cont)(cont)

INORDER traversal

TreeTree TraversalTraversal (cont)(cont) INORDER traversal For each tree or subtree – traversal starts 1. from LEFT

For each tree or subtree traversal starts

1. from LEFT node,

2. the ROOT and then

3. the RIGHT node

D

Example : D B E A C

B

E

A

C

TreeTree TraversalTraversal (cont)(cont)

PREORDER traversal

TreeTree TraversalTraversal (cont)(cont) PREORDER traversal For each tree or subtree – traversal starts 1. from ROOT

For each tree or subtree traversal starts

1. from ROOT node,

2. the LEFT and then

3. the RIGHT node

D

B

Example : A B D E C

E

A

C

TreeTree TraversalTraversal (cont)(cont)

POSTORDER traversal

TreeTree TraversalTraversal (cont)(cont) POSTORDER traversal For each tree or subtree – traversal starts 1. from LEFT

For each tree or subtree traversal starts

1. from LEFT node,

2. the RIGHT and then

3. the ROOT node

D

B

Example : D E B C A

E

A

C

TreeTree TraversalTraversal (cont)(cont)

Example (a) :

B

TreeTree TraversalTraversal (cont)(cont) Example (a) : B D H E A I nor der – D
TreeTree TraversalTraversal (cont)(cont) Example (a) : B D H E A I nor der – D

D

H

E

A

Inorder – D H B E A F C G Preorder – A B D H E C F G Postorder – H D E B F G C A

C

A F C G Preorder – A B D H E C F G Postorder –

F

G

TreeTree TraversalTraversal (cont)(cont)

Example (b) :

Y

Q

TreeTree TraversalTraversal (cont)(cont) Example (b) : Y Q W X P I nor der – Y
TreeTree TraversalTraversal (cont)(cont) Example (b) : Y Q W X P I nor der – Y

W

X

P

Inorder – Y W Q X P R Preorder – P Q W Y X R Postorder – Y W X Q R P

R

ExpressionExpression TreeTree

What is Expression Tree

one of th e app li cati ons o f Bi nary Tree ( BT ) f the applications of Binary Tree (BT)

used to rep resent arithmetic ex p ression (infix, prefix and postfix) present arithmetic expression (infix, prefix and postfix)

can b e b u ilt w ith prefix and postfix express i on on l be built with prefix and postfix expression only. Any infix 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 in f ix ex will produce the same expression tree if it comesfrom the same infix expression

BuildingBuilding ExpressionExpression TreeTree

Building Expression Tree a) Using prefix expression Read prefix expression from LEFT to RIGHT. Any
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
Always open the LEFT branch first until no more branch
can be opened. Then open the RIGHT branch.

BuildingBuilding ExpressionExpression TreeTree (cont)(cont)

Example (a) :

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

A

prefix =>

* * + A B C – D E

Left

Right

+

*

B

*

C

D

-

E

BuildingBuilding ExpressionExpression TreeTree (cont)(cont)

Example (b) :

prefix =>

A

B

+

-

+ A * - B C $ D * E F

Left

Right

C

*

D

$

E

*

F

BuildingBuilding ExpressionExpression TreeTree (cont)(cont)

b) Using postfix expression Read postfix expression from RIGHT to LEFT. Any operator must be
b) Using postfix expression
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.

BuildingBuilding ExpressionExpression TreeTree (cont)(cont)

Example (a) :

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

A

postfix =>

+

*

B

A B + C * D E - *

Left

*

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

C

Right

D

-

E

BuildingBuilding ExpressionExpression TreeTree (cont)(cont)

Example (b) :

A

postfix =>

B

+

-

A B C – D E F * $ * +

Left

C

*

D

Right

$

E

*

F

ExpressionExpression TreeTree TraversalTraversal

Traversing The Expression Tree

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

There are three traversing technique :tree will result a different mathematical expression. INORDER Traversa l gives in fi x expression (LEFT,

expression. There are three traversing technique : INORDER Traversa l gives in fi x expression (LEFT,

INORDER Traversal gives infix expression (LEFT, ROOT, RIGHT) (OPERAND,OPERATOR,OPERAND) PREORDER Traversal gives prefix expression (ROOT, LEFT, RIGHT) (OPERATOR,OPERAND,OPERAND) POSTORDER Traversal gives postfix expression (LEFT, RIGHT, ROOT) (OPERAND,OPERAND,OPERATOR)

gives postfix expression (LEFT, RIGHT, ROOT) (OPERAND,OPERAND,OPERATOR) Intersession May 2009 UiTMT (MP, EZ, NIK) 22
gives postfix expression (LEFT, RIGHT, ROOT) (OPERAND,OPERAND,OPERATOR) Intersession May 2009 UiTMT (MP, EZ, NIK) 22
ExpressionExpression TreeTree TraversalTraversal (cont)(cont) Example (a) : * * - + D E C A
ExpressionExpression TreeTree TraversalTraversal (cont)(cont)
Example (a) :
*
*
-
+
D
E
C
A
B
Inorder =>
Preorder =>
A + B * C * D - E
* * + A B C – D E
Postorder => A B + C * D E - *

ExpressionExpression TreeTree TraversalTraversal (cont)(cont)

Example (b) :

A

+

$

B

-

C

D

$

+

Inorder => Preorder => Postorder =>

E

F

A + B $ C D + E $ F $ - $ + A B C + D E F A B + C $ D E + - F $

ExpressionExpression TreeTree EvaluationEvaluation

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

ExpressionExpression TreeTree EvaluationEvaluation (cont)(cont)

Example (a) :

if A= B =C=D=E=F=2 then RESULT is 144

A

4

2

+

16 $

B

12

2

2

-

C

D

144

$

2

$

4

C

F

2

2

ExpressionExpression TreeTree EvaluationEvaluation (cont)(cont)

Example (b) :

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

A 5

-19

-

B 4

* 24

C

3

* 6

D

$

2

2 E

1