2.07k likes | 2.12k Views
Lecture 12. Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree. Traversing a Binary Tree Inorder Traversal. The Scenario. Imagine we have a binary tree We want to traverse the tree It’s not linear We need a way to visit all nodes
E N D
Lecture 12 Traversing a Binary TreeBinary Search Tree Insertion Deleting from a Binary Search Tree
The Scenario • Imagine we have a binary tree • We want to traverse the tree • It’s not linear • We need a way to visit all nodes • Three things must happen: • Deal with the entire left sub-tree • Deal with the current node • Deal with the entire right sub-tree
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 9 – do left At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 2 – do left At 9 – do left At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 2 – do left At 9 – do left At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 2 – do current At 9 – do left At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 2 – do right At 9 – do left At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 2 – do right At 9 – do left At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 2 - done At 9 – do left At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 9 – do current At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 9 – do right At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 9 – do right At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 9 – done At 13 – do left
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 13 – do current
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 13 – do right
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 42 – do left At 13 – do right
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 42 – do left At 13 – do right
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 42 – do current At 13 – do right
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 42 – do right At 13 – do right
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 42 – do right At 13 – do right
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 42 – done At 13 – do right
Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 13 – done
Outline of In-Order Traversal • Three principle steps: • Traverse Left • Do work (Current) • Traverse Right • Work can be anything • Separate work from traversal
Traverse the tree “In order”: • Visit the tree’s left sub-tree • Visit the current and do work • Visit right sub-tree
In-Order Traversal Procedure procedure In_Order(cur iot in Ptr toa Tree_Node) // Purpose: perform in-order traversal, call // Do_Something for each node // Preconditions: cur points to a binary tree // Postcondition: Do_Something on each tree // node in “in-order” order • if( cur <> NIL ) then • In_Order( cur^.left_child ) • Do_Something( cur^.data ) • In_Order( cur^.right_child ) • endif endprocedure // In_Order
22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R
22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R
22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R
7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R
7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R
7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L
7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L
94 97 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L L
94 97 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L L
94 97 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L L L
94 97 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L L L
97 94 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P
97 94 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P
1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P R
1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P R
1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P R
1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P L P R
1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P L P R
9 22 1 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P R L P R
9 22 1 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P R L P R
9 22 1 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P R L L P R
9 22 1 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P R L L P R