BinarySearchTrees
BinarySearchTrees
31 mai 2021
Outline
Exercises
Search in a link list
Examples :
typedef struct{
int key;
struct node* parent; parent pointer
InorderTreeWalk(x)
if x 6= NIL
InorderTreeWalk(x.left) ;
print(x.key) ;
InorderTreeWalk(x.right) ;
Preorder tree traversal
PreorderTreeWalk(x)
if x 6= NIL
print(x.key) ;
PreorderTreeWalk(x.left) ;
PreorderTreeWalk(x.right) ;
Postorder tree traversal
PostorderTreeWalk(x)
if x 6= NIL
PostorderTreeWalk(x.left) ;
PostorderTreeWalk(x.right) ;
print(x.key) ;
Cost of tree traversals
Traversal of an empty subtree, for the test x 6= NIL, i.e. T (0), cost c
For n > 0, suppose a tree traversal is called on a node x where the left
subtree has k nodes and the right subtree has n − k − 1 nodes
T (n) ≤ T (k) + T (n − k − 1) + d
= ((c + d)k + c) + ((c + d)(n − k − 1) + c) + d
= (c + d)n + c − (c + d) + c + d
= (c + d)n + c
Search for a key k
TreeSearch(x, k)
if (x = NULL or k = x.key)
return x ;
if (k < x.key)
return TreeSearch(x.left, k) ;
else
return TreeSearch(x.right, k) ;
Cost Θ(h) (where h is the height of the tree) since search is performed
along one path in the tree
Operations on BSTs : Iterative Search
TreeSearch(x, k)
while (x != NULL and k != x.key)
if (k < x.key)
x = x.left ;
else
x = x.right ;
return x ;
Get the key with the minimum or the maximum value. These
algorithms return a pointer to the node with the min or max value
Tree-Minimum(x)
while x.left 6= NULL
x = x.left
return x
Tree-Maximum(x)
while x.right 6= NULL
x = x.right
return x
Θ(h)
Successor operation
Given a node x, get its successor node in the sorted order of the keys.
Successor :
I x has a right subtree : successor is
minimum node in right subtree
I x has no right subtree : successor is
first ancestor of x whose left child is
also ancestor of x
I Intuition : As long as you move to
the left up the tree, you’re visiting
smaller nodes.
Successor Operation
Tree-Successor(x)
if x.right 6= NULL
return Tree-Minimum(x.right)
y = x.p
while y 6= NIL and x == y.right
x = y ; y = y.p
return y
Tree-Insert(T, z)
1 y = NIL
2 x = T.root
3 while x 6= NIL
4 y=x
5 if z.key < x.key
6 x = x.left
7 else x = x.right
8 z.p = y
9 if y == NIL /* Tree was empty */
10 T.root = z
11 elseif z.key < y.key
12 y.left = z
13 else y.right = z
BST Search/Insert : Running Time
The height of a binary search tree in the worst case is h = O(n) when
tree is just a linear string of left or right children
This worst case happens if keys are selected to be inserted in the tree
in increasing or decreasing order
I We kept all analysis in terms of h so far for now
I We can maintain h = O(lg n) in a similar way as for quick sort by
randomly picking among the keys the next one that is inserted in
the tree
Operations of BSTs : Delete
Tree-Delete(T, z)
if z.left == NIL
r.p = q ; q.right or q.left = x
elseif z.right == NIL
l.p = q ; q.right or q.left = l
else
y = Tree-minimum(z.right)
BST delete case 3
Case 3 : z has two children
I z’s successor (y ) is the minimum node in z’s right subtree
I y has either no children or one right child (but no left child)
I Delete y from the tree (via Case 1 or 2)
I Replace z’s key and satellite data with y ’s.
6 15 15
delete z
5 16 6 16
3 12 20 3 12 20
10 13 18 23 10 13 18 23
y 6 7
7
BST delete case 3
Tree-Delete(T, z)
if z.left == NIL
r.p = q ; q.right or q.left = x
elseif z.right == NIL
6 15
l.p = q ; q.right or q.left = l delete z
else 5 16
y = Tree-minimum(z.right) 3 12 20
if y.p 6= z
10 13 18 23
y.right.p = y.p ; y.p.left = y.right
y.right = z.right y 6
y.right.p = y 7
y.p = z.p
z.p.left = y
y.left = z.left
y.left.p = y
Sorting with BST
Informal code for sorting array A of length n :
BSTSort(A)
for i=1 to n
Tree-Insert(A[i]) ;
InorderTreeWalk(root) ;
1. Draw a binary search tree for the nodes 11, 8, 14, 5, 9, 13, 16, 3,
6, 10, 17, 1
2. Draw the binary search tree from inserting the following sequence
of keys : 49 75 92 24 107 26 112 101 19 2 11 25 59 16 28 95
3. Draw a binary search tree containing keys 8, 27, 13, 15, 32, 20,
12, 50, 29, 11, inserted in this order. Then, add keys 14, 18, 30,
31, in this order, and again draw the tree. Then delete keys 29
and 27, in this order, and again draw the tree
4. Beginning with an empty binary search tree, what binary search
tree is formed when you insert the following values in the order
given ?
4.1 W, T, N, J, E, B, A
4.2 W, T, N, A, B, E, J
4.3 A, B, W, J, N, T, E
Exercise 5
1
3
2 8
6 9
5.1 Show the steps to insert two nodes with both keys as 7.
5.2 Show the steps to delete the node with key=8 (after inserting two nodes with
key=7).
Exercises 6 and 7
6
2 9
1
8 15
5
13 18
11
Considering the BST above, show the steps to delete the node with key=9.
2 11
1
8 15
5
10
Considering the BST above, show the steps to delete the node with key=6.
Exercises
8. Suppose that we have numbers between 1 and 1000 in a binary search tree,
and we want to search for the number 363. Which of the following sequences
could not be the sequence of nodes examined ?
8.1 2, 252, 401, 398, 330, 344, 397, 363.
8.2 924, 220, 911, 244, 898, 258, 362, 363.
8.3 925, 202, 911, 240, 912, 245, 363.
8.4 2, 399, 387, 219, 266, 382, 381, 278, 363.
8.5 935, 278, 347, 621, 299, 392, 358, 363.
9. Draw the binary search tree obtained when the keys 1, 2, 3, 4, 5, 6, 7 are
inserted in the given order into an initially empty tree. What is the problem of
the tree you get ? Why is it a problem ? How could you modify the insertion
algorithm to solve this problem ?
10. How would you represent a binary search tree using an array ? Give the array
representation the following keys inserted consecutively 8, 27, 13, 15, 32, 20 in
a binary search tree. Draw the binary search tree corresponding to the array
representation below
0 1 2 3 4 5 6 7 8 9 10 11 12
23 2 35 1 13 29 80 11 14 27
Exercises