data structure (tree continued)
TRANSCRIPT
Tree
Adam M.B.
TREE TRAVERSAL
Types of Tree Traversal
• Preorder : Node – Left - Right
• Inorder : Left – Node - Right
• Postorder : Left – Right - Node
Ilustration (1)
H
A
L
K
B
C J
Preorder (NLR) : HA KCB JLInorder (LNR) : HA KCB J LPostorder (LRN) : HA KCB JL
Ilustration (2)
Preorder (NLR) : ABDE C GHInorder (LNR) :Postorder (LRN) :
A
B
H
CD
E F
GI
I FABD E CGHI FABDE CGHI F
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
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
TREE TRAVERSAL INTO BINARY TREE
Example 1Preorder :Inorder :
J
B
H
CD
E F
GI
BDEI F GJC HDI EB F CJH G
Example 2Inorder :Postorder :
J
B
H
CD
E F
GI
I EDH F CBG JDI EB F CJH G
Make binary tree from these result of tree traversal:• Inorder : BCDFGKMPSUWY
Preorder : MFDBCKGSPWUY• Postorder : EGHCIMFBNPJLKDA
Inorder : EBGCHFMIANJPDLK
Exercise
m
TREE TRAVERSAL USING STACK
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.
Node have 2
Using PreorderNotes :
• Arrow down (↓) means PUSH.
• Arrow up (↑) means POP.
• LS : Left Son
• RS : Right Son
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
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.
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
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.
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
Traverse this tree using stack (3 ways):
Exercise 1
A
B
H
C
D E
FI
Make binary tree from this statement then traverse binary tree using stack (3 ways):
Exercise 2
E = A + (B-D)H – F GK
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
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
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