Title: Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree
1Traversing a Binary TreeBinary Search Tree
Insertion Deleting from a Binary Search Tree
Lecture 12
2Traversing a Binary TreeInorder Traversal
3The Scenario
- Imagine we have a binary tree
- We want to traverse the tree
- Its 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
4Use 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
5Use 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
6Use 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
7Use 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
8Use 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
9Use 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
10Use 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
11Use 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
12Use 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
13Use 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
14Use 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
15Use 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
16Use 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
17Use 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
18Use 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
19Use 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
20Use 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
21Use 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
22Use 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
23Use 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
24Use 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
25Use 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
26Outline of In-Order Traversal
- Three principle steps
- Traverse Left
- Do work (Current)
- Traverse Right
- Work can be anything
- Separate work from traversal
27- Traverse the tree In order
- Visit the trees left sub-tree
- Visit the current and do work
- Visit right sub-tree
28In-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 ltgt NIL ) then
- In_Order( cur.left_child )
- Do_Something( cur.data )
- In_Order( cur.right_child )
- endif
- endprocedure // In_Order
29Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
P
R
30Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
P
R
31Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
P
R
32Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
33Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
34Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
35Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
36Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
L
37Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
L
38Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
L
L
39Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
L
L
40Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1
L
L
P
41Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1
L
L
P
42Proc 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
43Proc 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
44Proc 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
45Proc 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
46Proc 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
47Proc 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
48Proc 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
49Proc 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
50Proc 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
51Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
52Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
53Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
R
54Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
R
55Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
R
56Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7
L
P
R
L
L
P
P
R
R
57Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
L
P
R
L
L
P
P
R
R
58Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
L
P
R
L
L
P
P
R
R
59Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
R
L
P
R
L
L
P
P
R
R
60Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
R
L
P
R
L
L
P
P
R
R
61Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
R
L
L
P
R
L
L
P
P
R
R
62Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9
P
R
L
L
P
R
L
L
P
P
R
R
63Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
L
L
P
P
R
R
64Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
L
L
P
P
R
R
65Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
R
L
L
P
P
R
R
66Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
R
L
L
P
P
R
R
67Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
R
L
L
P
P
R
R
68Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
R
L
Output 1 3 7 9 14
P
R
L
L
P
P
R
R
L
L
P
P
R
R
69Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
70Continue?
Yes!
Enough Already!
71Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
72Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
73Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
74Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
75Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22
P
R
L
L
P
P
R
R
L
L
P
P
R
R
76Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22
P
R
L
L
L
P
P
R
R
L
L
P
P
R
R
77Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22
P
R
L
L
L
P
P
R
R
L
L
P
P
R
R
78Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
L
L
P
P
R
R
79Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
L
L
P
P
R
R
80Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
R
L
L
P
P
R
R
81Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
R
L
L
P
P
R
R
82Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
R
R
83Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
R
R
84Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
85Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
86Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
87Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
88Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
89Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
90Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
91Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
92Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
93Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
R
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
94Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
R
L
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
95Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67
P
P
R
R
L
L
L
L
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
96Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
97Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
L
L
L
P
P
P
R
R
R
98Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
P
P
P
R
R
R
99Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
P
P
P
R
R
R
100Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
R
R
R
101Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
R
R
R
102Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
103Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
104Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
105Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
106Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
107Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
108Proc InOrderPrint(pointer) pointer NOT NIL?
InOrderPrint(left child) print(data)
InOrderPrint(right child)
root
L
L
P
P
R
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
109Algorithm Example . . . InOrderPrint(root) . . .
root
L
P
R
L
L
Output 1 3 7 9 14 22 36 44 67 94 97
P
P
R
R
L
L
L
L
P
P
P
P
R
R
R
R
L
L
L
L
P
P
P
P
R
R
R
R
110Summary
- 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.
111Questions?
112Binary Search Tree Insertion
113Tree Node Defined
- In general
- Node definesa record
- data isoftype lttypegt
- left, right isoftype ptr toa Node
- endrecord
- In this example
- Node definesa record
- data isoftype num
- left, right isoftype ptr toa Node
- endrecord
114Scenario
- 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
115Finding the Correct Location
Start withthis tree
12
41
3
98
7
116Finding the Correct Location
Where would 4 be added?
12
41
3
98
7
117Finding the Correct Location
4
To the topdoesnt work
12
41
3
98
7
118Finding the Correct Location
12
41
4
98
3
In the middledoesnt work
7
119Finding the Correct Location
12
41
3
98
7
At the bottomDOES work!
4
120Finding 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. - Well recurse to get to the bottom (i.e. when
current nil)
121Finding the Correct Location
- if (current nil)
- DO ADD NODE WORK HERE
- elseif (current.data gt value_to_add) then
- // recurse left
- Insert(current.left, value_to_add)
- else
- // recurse right
- Insert(current.right, value_to_add)
- endif
122Adding the Node
- Current is an in/out pointer
- We need information IN to evaluate current
- We need to send information OUT because were
changing the tree (adding a node) - Once weve 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)
123Adding the Node
- current lt- new(Node)
- current.data lt- value_to_add
- current.left lt- nil
- current.right lt- nil
124The Entire Module
- procedure Insert(cur iot in/out Ptr toa Node,
- data_in iot in num)
- if(cur NIL) then
- cur lt- new(Node)
- cur.data lt- data_in
- cur.left lt- NIL
- cur.right lt- NIL
- elseif(cur.data gt data_in)
- Insert(cur.left, data_in)
- else
- Insert(cur.right, data_in)
- endif
- endprocedure // Insert
125Tracing 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.
126Head iot Ptr toa Node head lt- NIL Insert(head, 42)
127Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
128Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
129Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
130Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
131Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
132Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
133Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
134Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
135Head iot Ptr toa Node head lt- NIL Insert(head, 42)
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
136. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
42
137. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
138. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
139. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
140. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
141. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
data_in 23
142. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
143. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
144. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
145. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
146. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 23
147. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
42
23
148. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
149. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
150. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
151. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
152. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
153. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
154. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
155. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
data_in 35
156. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
157. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
158. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
159. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
160. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
161. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 35
162. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
42
23
35
163Continue?
yes...
Ive had enough!
164. . Insert(head, 23) Insert(head,
35) Insert(head, 47) . .
head
procedure Insert( cur iot in/out Ptr toa Node,
data_in iot in num) if(cur NIL) then cur lt-
new(Node) cur.data lt- data_in cur.left lt-
NIL cur.right lt- NIL elseif(cur.data gt
data_in) Insert(cur.left, data_in) else
Insert(cur.right, data_in) endif endprocedure
// Insert
42
23
35
data_in 47
165. . Insert(head, 23) Insert(head,
35) Insert(head, 4