Tree Adam M B TREE TRAVERSAL Types of
Tree Adam M. B.
TREE TRAVERSAL
Types of Tree Traversal • Preorder : Node – Left - Right • Inorder : Left – Node - Right • Postorder : Left – Right - Node
Ilustration (1) Preorder (NLR) : HACBK JL Inorder (LNR) : ABCHJK L Postorder (LRN) : BC A JLKH H A K C B J L
Ilustration (2) A B D C E I F G Preorder (NLR) : ABDEIC F GH Inorder (LNR) : DI E BF GH CA Postorder (LRN) : IEDHGF C BA H
Case E = A + BDH – F G-K E =( A + B * D ^ H- F ) / ( G- K) Subtree Kiri / F G ^ D K Prefix : / - + A* B^DH F - GK Infix : A+ B * D^H - F /G - K Postfix: A BDH^ * + F - GK -/ * B Subtree Kanan - + A Root H
Exercise Make binary tree from this general tree and do the tree traversal process: A B D R I C E Q F G H P
TREE TRAVERSAL INTO BINARY TREE
Example 1 Preorder Inorder : BDEI CFHGJ : DIEBHFGCJ B D C E I J F H G
Example 2 Inorder Postorder : DIEBHFGCJ : I EDHGFJ CB B D C E I J F H G
Exercise Make binary tree from these result of tree traversal: • Inorder Preorder : BCDFGKMPSUWY : MFDBCKGSPWUY • Postorder : EGHCIMFBNPJLKDA Inorder : EBGCHFMIANJPDLK
m TREE TRAVERSAL USING STACK
Node have 2 Using Preorder Rules : • 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 Preorder Notes : • Arrow down (↓) means PUSH. • Arrow up (↑) means POP. • LS : Left Son • RS : Right Son
Node have 2 Using Preorder (NLR) : BDEICFGHA B Head D B LS D RS E LS I C C RS B E I F A G LS F RS G RS B RS H RS C H A RS C
Node have 2 Using Inorder Rules 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 (LNR) : DIEBFGHCA B Head D B LS D RS E LS I E C Add. E Add. B E I F A B RS G C LS Add. C H C RS A F RS G Add. E Add. B RS H Add. C
Node have 2 Using Postorder Rules : • 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 (LRN) : IEDHGFACB B Head D B C E I F LS D Add B RS B A G H Add D LS D C RS B Add C RS C G F Add F RS RS C F E LS I Add. E Add. D Add E RS Add F A Add C G E RS Add G C Add B H Add G B
Exercise 1 Traverse this tree using stack (3 ways): A B D C E I H F
Exercise 2 Make binary tree from this statement then traverse binary tree using stack (3 ways): E = A + (B-D)H – F GK
Exercise 3 Make binary in linked list form from these result of tree traversal and traverse the result using stack in postorder way: Preorder : RFCAEDLHPVTZXWY Inorder : ACDEFHLPRTVWXYZ
Exercise 4 Make binary tree from this general tree and traverse the result using preorder, inorder, and postorder: S B F E C G I D M H J N Y X K P L A P
THANK YOU GRACIAS Contact Person: Adam Mukharil Bachtiar Informatics Engineering UNIKOM Jalan Dipati Ukur Nomor. 112 -114 Bandung 40132 Email: adfbipotter@gmail. com Blog: http: //adfbipotter. wordpress. com Copyright © Adam Mukharil Bachtiar 2012
- Slides: 24