traversing a binary tree binary search tree insertion deleting from a binary search tree
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Traversing a Binary TreeBinary Search Tree Insertion
Deleting from a Binary Search Tree
Traversing a Binary TreeInorder 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
• 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
2
42
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
2
42
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
2
42
At 13 – do leftAt 9 – 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
2
42
At 13 – do leftAt 9 – do leftAt 2 – 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
2
42
At 13 – do leftAt 9 – do leftAt 2 – do left
At NIL
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
2
42
At 13 – do leftAt 9 – do left
At 2 – 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
2
42
At 13 – do leftAt 9 – do left
At 2 – 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
2
42
At 13 – do leftAt 9 – do left
At 2 – do rightAt NIL
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
2
42
At 13 – do leftAt 9 – do leftAt 2 - done
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
2
42
At 13 – do leftAt 9 – 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
2
42
At 13 – do leftAt 9 – 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
2
42
At 13 – do leftAt 9 – do right
At NIL
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
2
42
At 13 – do leftAt 9 – done
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
2
42
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
2
42
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
2
42
At 13 – do rightAt 42 – 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
2
42
At 13 – do rightAt 42 – do left
At NIL
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
2
42
At 13 – do rightAt 42 – 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
2
42
At 13 – do rightAt 42 – 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
2
42
At 13 – do rightAt 42 – do right
At NIL
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
2
42
At 13 – do rightAt 42 – done
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
2
42
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
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
L
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
L
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
L
L
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
L
L
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
L
L
L
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9
LPR
L
L
L
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1
LPR
L
L
L
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1
LPR
L
L
L
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1
LPR
L
L
L
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1
LPR
L
L
L
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1
LPR
L
L
L
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3
LPR
L
L
L
LPR
P
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3
LPR
L
L
L
LPR
P
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3
LPR
L
L
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3
LPR
L
L
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3
LPR
L
L
LPR
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3
LPR
L
L
LPR
L
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7
LPR
L
L
LPR
L
LPR
P
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7
LPR
L
L
LPR
L
LPR
P
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7
LPR
L
L
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7
LPR
L
L
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7
LPR
L
L
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7
LPR
L
L
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9
LPR
L
LP
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9
LPR
L
LP
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9
LPR
L
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9
LPR
L
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9
LPR
L
LPR
LPR
LPR
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9
LPR
L
LPR
LPR
LPR
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14
LPR
L
LPR
LPR
LPR
LPR
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14
LPR
L
LPR
LPR
LPR
LPR
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14
LPR
L
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14
LPR
L
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14
LPR
L
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14
LPR
L
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22
LPR
LP
LPR
LPR
LPR
LPR
LPR
Continue?
Yes!EnoughAlready!
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22
LPR
LP
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
LPR
P
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
LPR
LPR
P
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
L
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94 97
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94 97
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LP
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94 97
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94 97
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94 97
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94 97
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Proc InOrderPrint(pointer)
pointer NOT NIL?
InOrderPrint(left child)
print(data)
InOrderPrint(right child)
22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94 97
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Algorithm Example
. . .
InOrderPrint(root)
. . .22
root
67
363 14
447
94
971
9Output: 1 3 7 9 14 22 36 44 67 94 97
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
LPR
Summary
• An In-Order traversal visits every node– Recurse left first– Do something with current– Recurse right last
• The “left, current, right” logic is repeated recursively at every node.
• For a BST, an in-order traversal accesses the elements in ascending order.
Questions?
Binary Search Tree Insertion
Tree Node Defined
In general:Node definesa record data isoftype <type> left, right isoftype ptr toa Nodeendrecord
In this example:Node definesa record data isoftype num left, right isoftype ptr toa Nodeendrecord
Scenario
• We have a Binary Search Tree– It can be empty– Or have some elements in it already
• We want to add an element to it– Inserting/adding involves 2 steps:• Find the correct location• Do the steps to add a new node
• Must maintain “search” structure
Finding the Correct Location
12
3
7
41
98
Start withthis tree
Finding the Correct Location
12
3
7
41
98
Where would4 be added?
Finding the Correct Location
12
3
7
41
98
To the topdoesn’t work
4
Finding the Correct Location
12
3
7
41
98In the middledoesn’t work
4
Finding the Correct Location
12
3
7
41
98
At the bottomDOES work!
4
Finding the Correct Location
• Must maintain “search” structure– Everything to left is less than current– Everything to right is greater than current
• Adding at the “bottom” guarantees we keep search structure.
• We’ll recurse to get to the “bottom” (i.e. when current = nil)
Finding the Correct Location
if (current = nil)
DO “ADD NODE” WORK HERE
elseif (current^.data > value_to_add) then
// recurse left
Insert(current^.left, value_to_add)
else
// recurse right
Insert(current^.right, value_to_add)
endif
Adding the Node
• Current is an in/out pointer– We need information IN to evaluate current– We need to send information OUT because
we’re changing the tree (adding a node)
• Once we’ve found the correct location:– Create a new node– Fill in the data field
(with the new value to add)– Make the left and right pointers point to nil
(to cleanly terminate the tree)
Adding the Node
current <- new(Node)
current^.data <- value_to_add
current^.left <- nil
current^.right <- nil
The Entire Module
procedure Insert(cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
Tracing Example
The following example shows a trace of the BST insert.
• Begin with an empty BST (a pointer)
• Add elements 42, 23, 35, 47 in the correct positions.
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
head
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
head
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
head
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
Head iot Ptr toa Nodehead <- NILInsert(head, 42)
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
23
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
23
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
23
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
23
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 23
23
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
data_in = 35
35
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
35
Continue?
yes... I’ve had enough!
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
47
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
47
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
47
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
47
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
data_in = 47
35
procedure Insert( cur iot in/out Ptr toa Node, data_in iot in num) if(cur = NIL) then cur <- new(Node) cur^.data <- data_in cur^.left <- NIL cur^.right <- NIL elseif(cur^.data > data_in) Insert(cur^.left, data_in) else Insert(cur^.right, data_in) endifendprocedure // Insert
47
head
42
.
.Insert(head, 23)Insert(head, 35)Insert(head, 47)..
23
35
47
Summary
• Preserve “search” structure!• Inserting involves 2 steps:– Find the correct location• For a BST insert, always insert at the
“bottom” of the tree– Do commands to add node• Create node• Add data• Make left and right pointers point to nil
Questions?
Deleting from a Binary Search Tree
(BST)
The Scenario
• We have a Binary Search Tree and want to remove some element based upon a match.
• Must preserve “search” property• Must not lose any elements (i.e. only
remove the one element)
BST Deletion
• Search for desired item.
• If not found, then return NIL or print error.
• If found, perform steps necessary to accomplish removal from the tree.
Four Cases for Deletion
• Delete a leaf node• Delete a node with only one child (left)• Delete a node with only one child (right)• Delete a node with two children
Cases 2 and 3 are comparable and only need slight changes in the conditional statement used
Delete a Leaf Node
Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree.
cur <- nil
14
50
94
9 71 116
1086613
42
Delete a Leaf Node
Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree.
cur <- nil
Let’s delete 42.
14
50
94
9 71 116
1086613
42
Delete a Leaf Node
Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree.
cur <- nil
Move the pointer;now nothing pointsto the node.
14
50
94
9 71 116
1086613
42
Delete a Leaf Node
Simply use an “in/out” pointer and assign it to “nil”. This will remove the node from the tree.
cur <- nil
The resulting tree.
14
50
94
9 71 116
1086613
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left or a right child.
Point the current pointer to the appropriate child:
cur <- cur^.left_childor
cur <- cur^.right_child
14
50
94
71 116
10866
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left or a right child.
Point the current pointer to the appropriate child:
cur <- cur^.left_childor
cur <- cur^.right_child
Let’s delete 14.
14
50
94
71 116
10866
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left or a right child.
Point the current pointer to the appropriate child:
cur <- cur^.right_child
Move the pointer; now nothing points to the node.
14
50
94
71 116
10866
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left or a right child.
Point the current pointer to the appropriate child:
cur <- cur^.right_child
The resulting tree.
50
94
71 116
10866
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left or a right child.
Point the current pointer to the appropriate child:
cur <- cur^.left_childor
cur <- cur^.right_child
Let’s delete 71.
14
50
94
71 116
10866
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left or a right child.
Point the current pointer to the appropriate child:
cur <- cur^.left_child
Move the pointer; now nothing points to the node.
14
50
94
71 116
10866
42
Delete a Node with One Child
Use an “in/out” pointer.
Determine if it has a left or a right child.
Point the current pointer to the appropriate child:
cur <- cur^.left_child
The resulting tree.
14
50
94
116
108
6642
Delete a Node with Two Children
Copy a replacementvalue from a descendantnode. - Largest from left - Smallest from right
Then delete that descendant node to remove the duplicate value. - We know this will be an easier case.
14
50
94
71 116
10866
42
Delete a Node with Two Children
14
50
94
71 116
10866
42
Let’s delete 50.
Delete a Node with Two Children
14
50
94
71 116
10866
42
Look to the leftsub-tree.
Delete a Node with Two Children
14
50
94
71 116
10866
42
42Find and copy thelargest value(this will erase theold value but createsa duplicate).
Delete a Node with Two Children
14
42
94
71 116
10866
42
The resulting treeso far.
Delete a Node with Two Children
14
42
94
71 116
10866
42
Now delete theduplicate fromthe left sub-tree.
Delete a Node with Two Children
14
42
94
71 116
10866
The final resultingtree – still has searchstructure.
Delete a Node with Two Children
14
50
94
71 116
10866
42
Let’s delete 94.
Delete a Node with Two Children
14
50
94
71 116
10866
42
Look to the rightsub-tree.
Delete a Node with Two Children
14
50
94
71 116
10866
42
Find and copy thesmallest value(this will erase theold value but createsa duplicate).
108
108
Delete a Node with Two Children
14
50
71 116
10866
42
The resulting treeso far.
108
Delete a Node with Two Children
14
50
71 116
10866
42
Now delete theduplicate fromthe left sub-tree.
Delete a Node with Two Children
14
50
71 116
66
42
The final resultingtree – still has searchstructure.
108
Summary
• Deleting a node from a binary search tree involves two steps:– Search for the element– Then perform the deletion
• We must preserve the search structure and only delete the element which matches.
• Four cases:– Deleting a leaf node– Deleting a node with only the left child– Deleting a node with only the right child– Deleting a node with both children
Questions?