24
Tree Adam M.B.

Data Structure (Tree Continued)

Embed Size (px)

Citation preview

Page 1: Data Structure (Tree Continued)

Tree

Adam M.B.

Page 2: Data Structure (Tree Continued)

TREE TRAVERSAL

Page 3: Data Structure (Tree Continued)

Types of Tree Traversal

• Preorder : Node – Left - Right

• Inorder : Left – Node - Right

• Postorder : Left – Right - Node

Page 4: Data Structure (Tree Continued)

Ilustration (1)

H

A

L

K

B

C J

Preorder (NLR) : HA KCB JLInorder (LNR) : HA KCB J LPostorder (LRN) : HA KCB JL

Page 5: Data Structure (Tree Continued)

Ilustration (2)

Preorder (NLR) : ABDE C GHInorder (LNR) :Postorder (LRN) :

A

B

H

CD

E F

GI

I FABD E CGHI FABDE CGHI F

Page 6: Data Structure (Tree Continued)

Case

Prefix : / + A- B DHInfix :Postfix:

/

+

^

-

A

B

F

*

D

* ^

E = A + BDH – F G - K

A + B * D^ H- F )( / G( - )E = K

H

-

KG

GK-F/+A -B D H* ^ G K-F

/+A -BDH *^ GK -F

Subtree Kiri Subtree Kanan

Root

Page 7: Data Structure (Tree Continued)

Make binary tree from this general tree and do the tree traversal process:

Exercise

A

B

H

C

D E F GI

R

P

Q

Page 8: Data Structure (Tree Continued)

TREE TRAVERSAL INTO BINARY TREE

Page 9: Data Structure (Tree Continued)

Example 1Preorder :Inorder :

J

B

H

CD

E F

GI

BDEI F GJC HDI EB F CJH G

Page 10: Data Structure (Tree Continued)

Example 2Inorder :Postorder :

J

B

H

CD

E F

GI

I EDH F CBG JDI EB F CJH G

Page 11: Data Structure (Tree Continued)

Make binary tree from these result of tree traversal:• Inorder : BCDFGKMPSUWY

Preorder : MFDBCKGSPWUY• Postorder : EGHCIMFBNPJLKDA

Inorder : EBGCHFMIANJPDLK

Exercise

Page 12: Data Structure (Tree Continued)

m

TREE TRAVERSAL USING STACK

Page 13: Data Structure (Tree Continued)

Node have 2

Using PreorderRules :

• If scanned node has 2 children then

PUSH its right son (RS).

• If scanned node hasn’t child then

POP element of stack.

Page 14: Data Structure (Tree Continued)

Node have 2

Using PreorderNotes :

• Arrow down (↓) means PUSH.

• Arrow up (↑) means POP.

• LS : Left Son

• RS : Right Son

Page 15: Data Structure (Tree Continued)

Node have 2

Using Preorder

A

B

H

CD

E F

GI

Preorder (NLR) :Head B D E I C

F G H A

BDEICFGHALS RS LS

RS B RS BLS RS RS

RS C

RS C

Page 16: Data Structure (Tree Continued)

Node have 2

Using InorderRules and Note :

• If scanned node has left child then

PUSH itself (Scanned Node

Address).

• If scanned node hasn’t child then

POP element of stack.

• Add = Address.

Page 17: Data Structure (Tree Continued)

Node have 2

Using Inorder

A

B

H

CD

E F

GI

Inorder (LNR) :Head B D E I E

B C F G

DIEBFGHCALS RS LS

Add. B Add. ERS LS

Add. C

Add. BAdd. E

Add. C

RS RS H

RSC A

Page 18: Data Structure (Tree Continued)

Node have 2

Using PostorderRules :• If scanned node has 2 children then PUSH

itself (Scanned Node Address) and its right child.

• If scanned node has only 1 child then PUSH itself (Scanned Node Address).

• If scanned node hasn’t child then POP element of stack.

Page 19: Data Structure (Tree Continued)

Node have 2

Using Postorder

A

B

H

CD

E F

GI

Postorder (LRN) :Head B D E I E

D C F G

IEDHGFACBLS RS LS

RS BAdd. E

LS

RS B

Add. D

G F A C

H

Add B

RS

Add C

Add D Add E

RS CAdd C Add F

RS

Add G Add G

Add F RS C

B

Add B

Page 20: Data Structure (Tree Continued)

Traverse this tree using stack (3 ways):

Exercise 1

A

B

H

C

D E

FI

Page 21: Data Structure (Tree Continued)

Make binary tree from this statement then traverse binary tree using stack (3 ways):

Exercise 2

E = A + (B-D)H – F GK

Page 22: Data Structure (Tree Continued)

Make binary in linked list form from these result of tree traversal and traverse the result using stack in postorder way:Preorder : RFCAEDLHPVTZXWYInorder : ACDEFHLPRTVWXYZ

Exercise 3

Page 23: Data Structure (Tree Continued)

Make binary tree from this general tree and traverse the result using preorder, inorder, and postorder:

Exercise 4

S

B

L

D

E F J K

MC

G H

I

N P

X

A PY

Page 24: Data Structure (Tree Continued)

Contact Person:Adam Mukharil Bachtiar

Informatics Engineering UNIKOMJalan Dipati Ukur Nomor. 112-114 Bandung 40132

Email: [email protected]: http://adfbipotter.wordpress.com

Copyright © Adam Mukharil Bachtiar 2012

GRACIASTHANK YOU