Exercises
14.2-2 Can the black-heights of nodes in a red-black tree be maintained as fields in the nodes of the tree without affecting the asymptotic performance of any of the red-black tree operations? Show how, or argue why not.
The answer is yes. Red-black trees has two primitive operation. One is change a node's color and the other is rotations. Changing color only effects the node and its ancestors, which is O(logn) nodes. When rotating is performed, the nodes involved in rotating may have their b-h value changed, and so do their ancestors, which is also O(logn) nodes in all. Thus, maintaining this field results in an extra cost of O(logn) per operation, which does not affect the asymptotic performance of any rb-tree operation.
14.2-3 Can the depths of nodes in a red-black tree be efficiently maintained as fields in the nodes of the tree? Show how, or argue why not.
The answer is yes if an additional field, maintain, is maintained.
14.2-5 We wish to augment red-black trees with an operation RB-ENUMERATE(x,a,b) that outputs all the keys k such that a<=k<=b in a red-black tree rooted at x. Describe how RB-ENUMERATE can be implemented in Θ(m+logn) time, where m is the number of keys that are output and n is the number of internal nodes in the tree.(Hint: There is no need to add new fields to the red-black tree).
First, locate the smallest node which is not smaller than a. This takes Θ(logn). Then we're to prove that m-1 successive calls of SUCCESSOR operation takes Θ(m) time.
Consider the spanning tree T that connects these m nodes. Recall that these m nodes are successive. Thus if we draw the route the SUCCESSOR operations passes, each edge in T must be visited at least once and at most twice. Thus, the time for SUCCESSOR operations is Θ(m).
14.3-6 Show how to maintain a dynamic set Q of numbers that supports the operation MIN-GAP, which gives the magnitude of the difference of the two closest numbers in Q. Make the operations INSERT, DELETE, and MIN-GAP as efficient as possible, and analyze their running times.
Use any balanced binary search tree to store Q, and maintain another tree T. When a number x is inserted, consider x's predecessor y and successor z. Insert x-y and z-x into T(if exists). When a number x is deleted, also delete such x-y and z-x from T(if exists). The MIN-GAP call can be simply implemented by calling T's MINIMUM operation. Thus, all operations can be performed in Θ(logn).
14.2-2 Can the black-heights of nodes in a red-black tree be maintained as fields in the nodes of the tree without affecting the asymptotic performance of any of the red-black tree operations? Show how, or argue why not.
The answer is yes. Red-black trees has two primitive operation. One is change a node's color and the other is rotations. Changing color only effects the node and its ancestors, which is O(logn) nodes. When rotating is performed, the nodes involved in rotating may have their b-h value changed, and so do their ancestors, which is also O(logn) nodes in all. Thus, maintaining this field results in an extra cost of O(logn) per operation, which does not affect the asymptotic performance of any rb-tree operation.
14.2-3 Can the depths of nodes in a red-black tree be efficiently maintained as fields in the nodes of the tree? Show how, or argue why not.
The answer is yes if an additional field, maintain, is maintained.
14.2-5 We wish to augment red-black trees with an operation RB-ENUMERATE(x,a,b) that outputs all the keys k such that a<=k<=b in a red-black tree rooted at x. Describe how RB-ENUMERATE can be implemented in Θ(m+logn) time, where m is the number of keys that are output and n is the number of internal nodes in the tree.(Hint: There is no need to add new fields to the red-black tree).
First, locate the smallest node which is not smaller than a. This takes Θ(logn). Then we're to prove that m-1 successive calls of SUCCESSOR operation takes Θ(m) time.
Consider the spanning tree T that connects these m nodes. Recall that these m nodes are successive. Thus if we draw the route the SUCCESSOR operations passes, each edge in T must be visited at least once and at most twice. Thus, the time for SUCCESSOR operations is Θ(m).
14.3-6 Show how to maintain a dynamic set Q of numbers that supports the operation MIN-GAP, which gives the magnitude of the difference of the two closest numbers in Q. Make the operations INSERT, DELETE, and MIN-GAP as efficient as possible, and analyze their running times.
Use any balanced binary search tree to store Q, and maintain another tree T. When a number x is inserted, consider x's predecessor y and successor z. Insert x-y and z-x into T(if exists). When a number x is deleted, also delete such x-y and z-x from T(if exists). The MIN-GAP call can be simply implemented by calling T's MINIMUM operation. Thus, all operations can be performed in Θ(logn).