Hwk4 Solution
Hwk4 Solution
Homework Set 4
Page 1 of 8
2 924
2
935
399
278
387
347
219
Violation: 299 lies in
621 the right sub-tree of
266 347 and 299 < 347
299
382
392
381
278 358
363 363
Figure d Figure e
Page 2 of 8
Problem 2 (20 points)
Show that if a node in a binary search tree has two children, then its successor has no left child and its
predecessor has no right child.
Solution
Let x be a node in a binary search tree that has two children. Accordingly, the left and right sub-trees of x are
non-empty.
We will prove that the successor of x has no left child.
Let y be the successor of x. Then, by definition, y lies in the right sub-tree of x and Key[y] is the smallest key
such that:
Key[x] < Key[y]
If y has a left child z then, based on binary search tree property, z also lies on the right sub-tree of x and we
have:
Key[z] < Key[y], and
Key[x] < Key[z]
Accordingly, we have:
Key[x] < Key[z] < Key[y] which contradicts our assumption that y is the successor of x.
Therefore, y cannot have a left child otherwise, this left child can be the successor of x.
Now, we will prove that the predecessor of x has no right child.
Let w be the predecessor of x. Then, by definition, w lies in the left sub-tree of x and Key[w] is the largest key
such that:
Key[x] > Key[w]
If w has a right child q then, q also lies in the left sub-tree of x, and based on binary search tree property, we
have:
Key[q] > Key[w], and
Key[x] > Key[q]
Accordingly, we have:
Key[x] > Key[q] > Key[w] which contradicts our assumption that w is the predecessor of x.
Therefore, w cannot have a right child otherwise, this right child can be the predecessor of x.
.
Page 3 of 8
Problem 3 (20 points)
We can sort a given set of n numbers by first building a binary search tree containing these numbers (using
TREE-INSERT repeatedly to insert the numbers one by one) and then printing the numbers by an in order tree
walk. What are the worst-case and best-case running times for this sorting algorithm?
Solution
The total running time T(n) for the sorting algorithm mentioned above can be expressed as follows:
T(n) = T1(n) + T2(n) (A)
where:
T1(n): running time to build the binary search tree (BST), and
T2(n): running time to print the BST.
Note that T2(n) is of O(n) in all cases since we will visit all the n nodes to print the BST (linear time).
T1(n) depends on the height h of the BST at each insert operation. The worst-case and best-case scenarios are
detailed in the following text.
Worst Case Scenario
In this case, the BST is completely unbalanced and we have a chain of n nodes constructing the height of the
tree. Therefore, each insert operation will take a running time of O(n). Then, we will repeat the insert operation
for n nodes which yields a total running time T1(n) of O(n2). Using equation A above, we get:
T(n) = O(n2) + O(n) = O(n2) [ O(n2) absorbs O(n) ]
Accordingly, the worst-case running time of this sorting algorithm is of O(n2).
Best Case Scenario
In this case, the BST is perfectly balanced. Therefore, each insert operation will take a running time of O(lg n)
where lg n is the height of the BST. Then, we will repeat the insert operation for n nodes which yields a total
running time T1(n) of O(n lg n). Using equation A above, we get:
T(n) = O(n lg n) + O(n) = O(n lg n) [ O(n lg n) absorbs O(n) ]
Accordingly, the best-case running time of this sorting algorithm is of O(n lg n)
Page 4 of 8
Problem 4 (40 points)
Generate a sequence of 6 keys. Each key is a random integer in the range [0 100].
Show the red-black trees that result after successively inserting each of the 6 keys into an initially empty red-
black tree. (Use different colors/shades/markers to distinguish between the red and black nodes).
Solution
Let the sequence of random keys be 40, 33, 57, 12, 71, 25.
Now, we will successively insert each of the 6 keys into an initially empty red-black tree as illustrated below.
First, we will insert 40 as the root of the red-black tree (root should always be black).
40
40
33
40
33 57
Page 5 of 8
40
33 57
12
Apply Case 1:
Color (33) = Black
Color (57) = Black
Color (40) = Red
40
33 57
12
40
33 57
12
Page 6 of 8
Insert 71 as a right child of 57
Color (71) = Red
40
33 57
12 71
40
33 57
12 71
25
Apply Case 2:
LEFT_ROTATE (12)
Page 7 of 8
40
33 57
25 71
12
Apply Case 3:
Color (25) = Black
Color (33) = Red
RIGHT_ROTATE (33)
40
25 57
12 33 71
We are done!
Page 8 of 8