0% found this document useful (0 votes)
9 views

Hwk4 Solution

The document provides solutions to homework problems related to binary search trees. It includes examples of searching for a number in a BST, properties of successors and predecessors of nodes with two children, analyzing the time complexity of sorting using a BST, and examples of inserting keys into an initially empty red-black tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Hwk4 Solution

The document provides solutions to homework problems related to binary search trees. It includes examples of searching for a number in a BST, properties of successors and predecessors of nodes with two children, analyzing the time complexity of sorting using a BST, and examples of inserting keys into an initially empty red-black tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CECS 419/619 COMPUTER ALGORITHMS Fall 2016

Homework Set 4

Student Name: Khaled Gamal Abdel Maksoud


Student ID: 1280703

Problem 1 (20 points)


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?
a. 2, 252, 401, 398, 330, 344, 397, 363.
b. 924, 220, 911, 244, 898, 258, 362, 363.
c. 925, 202, 911, 240, 912, 245, 363.
d. 2, 399, 387, 219, 266, 382, 381, 278, 363.
e. 935, 278, 347, 621, 299, 392, 358, 363.
Solution
a. The part of the binary search tree (BST) representing sequence {2, 252, 401, 398, 330, 344, 397, 363} is
shown in Figure a. Clearly, there are no violations of the BST property. Accordingly, the given sequence
could be a possible sequence to search for 363.
b. The part of the binary search tree (BST) representing sequence {924, 220, 911, 244, 898, 258, 362, 363} is
shown in Figure b. Clearly, there are no violations of the BST property. Accordingly, the given sequence
could be a possible sequence to search for 363.
c. The part of the binary search tree (BST) representing sequence {925, 202, 911, 240, 912, 245, 363} is
shown in Figure c. Clearly, there is one violation of the BST property. Accordingly, the given sequence
could NOT be a possible sequence to search for 363.
d. The part of the binary search tree (BST) representing sequence {2, 399, 387, 219, 266, 382, 381, 278, 363}
is shown in Figure d. Clearly, there are no violations of the BST property. Accordingly, the given sequence
could be a possible sequence to search for 363.
e. The part of the binary search tree (BST) representing sequence {935, 278, 347, 621, 299, 392, 358, 363} is
shown in Figure e. Clearly, there is one violation of the BST property. Accordingly, the given sequence
could NOT be a possible sequence to search for 363.

Page 1 of 8
2 924

252 220 925

401 911 202

398 244 911

Violation: 912 lies in


330 898 240 the left sub-tree of 911
and 912 > 911

344 258 912

397 262 245

363 363 363

Figure a Figure b Figure c

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

Insert 33 as a left child of 40


Color (33) = Red

40

33

Insert 57 as a right child of 40


Color (57) = Red

40

33 57

Insert 12 as a left child of 33


Color (12) = Red

Page 5 of 8
40

33 57

12

Apply Case 1:
Color (33) = Black
Color (57) = Black
Color (40) = Red

40

33 57

12

Root should be black:


Color (40) = Black

40

33 57

12

Page 6 of 8
Insert 71 as a right child of 57
Color (71) = Red

40

33 57

12 71

Insert 25 as a right child of 12


Color (25) = Red

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

You might also like