Chapter 12
Exercise 12.1-1
We can use VisuAlgo for demonstrating BSTs of various heights. For each session, choose the command Create to create an empty tree. Afterward, select Insert(v) and copy-paste the matching sequence, as shown below. We can control the height of a BST by entering numbers in specific order.
Height 2:
10,4,5,1,17,21,16Height 3:
4,10,5,1,17,21,16Height 4:
4,5,1,10,17,21,16Height 5:
1,4,5,10,17,21,16Height 6:
1,4,5,10,16,17,21
Exercise 12.1-2
Available in the latest revision of the IM.
The solution in the IM talks about the max-heap instead of the min-heap property. This doesn't change the conclusion, but may cause confusion.
Exercise 12.1-3
Available in the latest revision of the IM.
Exercise 12.1-4
Available in the latest revision of the IM.
Exercise 12.1-5
Available in the latest revision of the IM.
Exercise 12.2-1
Available in the latest revision of the IM.
Exercise 12.2-2
Available in the latest revision of the IM.
Exercise 12.2-3
Available in the latest revision of the IM.
Exercise 12.2-4
Available in the latest revision of the IM.
Exercise 12.2-5
Available in the latest revision of the IM.
Exercise 12.2-6
Available in the latest revision of the IM.
Exercise 12.2-7
Available in the latest revision of the IM.
Exercise 12.2-8
Available in the latest revision of the IM.
Exercise 12.2-9
Available in the latest revision of the IM.
Exercise 12.3-1
Available in the latest revision of the IM.
Exercise 12.3-2
Available in the latest revision of the IM.
Exercise 12.3-3
Available in the latest revision of the IM.
Exercise 12.3-4
When the node to be deleted or its successor farther down the tree is a leaf node.
Exercise 12.3-5
Available in the latest revision of the IM.
Exercise 12.3-6
Available in the latest revision of the IM.
The solution in the IM repeats the same issue as the book. Namely, the Tree-Delete procedure from the book violates the single level of abstraction (SLA) principle at line 5. It would be better to simply call Tree-Successor(z). The price for breaking this principle is even higher in the IM, since it also introduces inefficiency besides thwarting other code quality attributes, like readability. It makes no sense to find the successor if that is already available inside a newly introduced field succ. So, calling Tree-Successor(T, z) would make things proper, as it is tuned to work with this new field.
Exercise 12.3-7
Replace lines 5-12 with the following snippet:
else y = Tree-Predecessor(z) // y is z's predecessor
if y != z.left // is y farther down the tree?
Transplant(T, y, y.left) // replace y by its left child
y.left = z.left // z's left child becomes
y.left.p = y // y's left child
Transplant(T, z, y) // replace z by its predecessor y
y.right = z.right // and give z's right child to y,
y.right.p = y // which had no right childTo implement a fair strategy mentioned in the book, you may simulate coin flipping with a pseudorandom number generator. Based on the outcome, you would execute the "successor" or "predecessor" block of code.
Problem 12-1
a.
The performance deteriorates to , since the tree becomes a linear chain of nodes.
b.
This strategy results in a balanced tree with height . Therefore, inserting equal keys demands time.
c.
Each new node could be prepended in constant time to a list of nodes with equal keys. Thus, inserting equal keys demands time.
d.
The worst-case performance is the same as in part (a). This may occur if the random number generator always returns the same value. On average, according to the law of large numbers, we may expect to have a balanced situation similar to part (b).
Problem 12-2
Available in the latest revision of the IM.
There is a bug in the pseudocode in the IM. The argument in the last line should be x.right instead of x.left.
Problem 12-3
Available in the latest revision of the IM.
Problem 12-4
a.
There is only one empty binary tree, so .
A tree with nodes may have nodes in its left subtree and nodes in its right subtree. For a given we can have structurally different binary trees, as all unique ways to setup the left and right subtrees contribute to the total. Since, binary trees are positional trees (see section B.5.3 of the book), we must count each distribution of nodes (specified by ) separately. Therefore, .
b.
Let's start by calculating , treating as the Cauchy product of two power series. We have
We can use the formula for a quadratic equation to get
For a limit to exist when , we should choose as in the book (with minus in the numerator). In this case, we can apply L'Hôpital's rule and show that .
c.
Denote and let's calculate its derivatives.
In the last line, is a double factorial. We can transform it into an ordinary factorial by using the corresponding conversion formula, so .
The Taylor expansion of around the point is given by
We can now substitute instead of in (see part (b)) to get
d.
We need to find an asymptotic approximation of .
See the solution of Exercise C.1.13 to understand why is used here instead of .
Last updated