100 likes | 410 Views
TREE TRAVERSALS. BINARY TREE. TREE TRAVERSALS . One of the common operation performed on binary tree is TRAVERSAL. Pass through the tree, enumerating each nodes at once. The contents of each node are printed as we enumerate it. DIFFERENT TRAVERSALS:.
E N D
TREE TRAVERSALS BINARY TREE
TREE TRAVERSALS • One of the common operation performed on binary tree is TRAVERSAL. • Pass through the tree, enumerating each nodes at once. • The contents of each node are printed as we enumerate it.
DIFFERENT TRAVERSALS: • 3 different traversals method are defined PREORDER POSTORDER INORDER These methods are recursively defined. Thus traversing the ROOT, the LEFT SUBTREE & RIGHT SUBTREE
LNR TRAVERSALS PREORDER (Depth-First Order) INORDER Traverse the left subtree. Visit the ROOT. Traverse the right subtree Visit the ROOT. Traverse the left subtree. Traverse the right subtree POSTORDER (Symmetric) Traverse the left subtree. Traverse the right subtree Visit the ROOT. NLR LRN
PREORDER (NLR) N L R 1 N L R N L R 2 3 N L R NLR N L R N L R 6 7 4 5 6 1 2 4 5 3 7 PREORDER:
INORDER (LNR) L N R 1 LNR LNR 2 3 LNR LNR LNR LNR 6 7 4 5 3 4 2 5 1 6 7 INORDER:
POSTORDER (LRN) LRN 1 LRN LRN 2 3 LRN LRN LRN LRN 6 7 4 5 3 4 5 2 6 7 1 PREORDER:
TRAVERSE A B C E F D G H I
ANSWERS • PREORDER: A B D G C E H I F. • INORDER: D G B A H E I C F • POSTORDER: G D B H I E F C A