Draw (or describe how you would draw) the BST that results when you insert the keys E A S Y Q U E S T I O N,
in that order (associating the value i with the ith key, as per the convention in the text)
into an initially empty tree. How many compares are needed to build the tree?
Draw (or describe how you would draw) the sequence of BSTs that results when you delete the keys from the tree
of Exercise 3.2.1, one by one, in the order they were inserted.
Suppose that a certain BST has keys that are integers between 1 and 10, and we
search for 5. Which sequence below cannot be the sequence of keys examined?
a. 10, 9, 8, 7, 6, 5
b. 4, 10, 8, 7, 53
c. 1, 10, 2, 9, 3, 8, 4, 7, 6, 5
d. 2, 7, 3, 8, 4, 5
e. 1, 2, 10, 4, 8, 5
Assistant
Answer to Question 1:
The BST that results from inserting the keys E A S Y Q U E S T I O N into an initially empty tree (associating each key with its insertion index) is constructed as follows:
Insert 'E': Becomes the root node.
Insert 'A': 'A' < 'E', so it becomes the left child of 'E'.
Insert 'S': 'S' > 'E', so it becomes the right child of 'E'.
Insert 'Y': 'Y' > 'E' and 'Y' > 'S', so it becomes the right child of 'S'.
Insert 'Q': 'Q' > 'E' and 'Q' < 'S', so it becomes the left child of 'S'.
Insert 'U': 'U' > 'E', 'U' > 'S', 'U' < 'Y', so it becomes the left child of 'Y'.
Insert 'E': Already exists; update value.
Insert 'S': Already exists; update value.
Insert 'T': 'T' > 'E', 'T' > 'S', 'T' < 'Y', 'T' < 'U', so it becomes the left child of 'U'.
Insert 'I': 'I' > 'E', 'I' < 'S', 'I' < 'Q', so it becomes the left child of 'Q'.
Insert 'O': 'O' > 'E', 'O' < 'S', 'O' > 'Q', so it becomes the right child of 'Q'.
Insert 'N': 'N' > 'E', 'N' < 'S', 'N' > 'Q', 'N' < 'O', so it becomes the left child of 'O'.
The total number of key comparisons made during the insertions is 26.
Answer to Question 2:
Deleting the keys one by one in the order they were inserted, the sequence of BSTs evolves as:
Delete 'E': Replace 'E' with its in-order successor 'I'. Remove 'I' from its previous position.
Delete 'A': Remove 'A' (a leaf node).
Delete 'S': Replace 'S' with its in-order successor 'T'. Remove 'T' from its previous position.
Delete 'Y': 'Y' has one child ('U'); replace 'Y' with 'U'.
Delete 'Q': 'Q' has one child ('O'); replace 'Q' with 'O'.
Delete 'U': Remove 'U' (a leaf node).
Delete 'T': 'T' has one child ('O'); replace 'T' with 'O'.
Delete 'I': 'I' has one child ('O'); replace 'I' with 'O'.
Delete 'O': 'O' has one child ('N'); replace 'O' with 'N'.
Delete 'N': Remove 'N' (the last node).
After these deletions, the BST becomes empty.
Answer to Question 3:
Option b cannot be the sequence of keys examined because it includes a key '53', which is outside the range of 1 to 10.