ps3
ps3
Problem 1
Suppose we are given a set S of n items, each with a value and a weight. For any element x ∈ S, we
define two subsets:
• S<x is the set of elements of S whose value is less than the value of x.
• S>x is the set of elements of S whose value is more than the value of x.
For any subset R ⊆ S, let w(R) denote the sum of the weights of elements in R. The magical-mean of
S is any element x such that w(S<x ) ≤ w(S)/2 and w(S>x ) ≤ w(S)/2.
In this problem, your input consists of two unsorted arrays S[1 · · · n] and W [1 · · · n], where for each
index i, the ith element has value S[i] and weight W [i]. You may assume that all values are distinct and
all weights are positive.
(a) Describe and analyze an algorithm to compute the magical-mean of a weighted set in O(n log n)
time. (Hint: Use sorting.)
(b) Describe and analyze an algorithm to compute the magical-mean of a weighted set in O(n) time.
1. RECURSION
Problem 2
• The left subtree of T is a binary search tree.
Prove that any n-node
• All valuesbinary
in thesearch tree canare
left subtree besmaller
transformed intovalue
than the a height-balanced
at the root. binary search tree
with the same noderight
• The values, using
subtree T is aO(n)
of only rotations.
binary In a height-balanced binary search tree, the
search tree.
difference between the height of the left and the right subtrees
• All values in the right subtree are larger than the ofvalue
everyatnode
the is at most 1. (Hint: You
root.
might want to first transform the input binary tree into a chain.)
Consider the following pair of operations on binary trees:
• Rotate an arbitrary node upward.22
Problem 3 y x
x y
Let T be a binary tree whose nodes store distinct
C
numerical
A
values. In class we have introduced the
rotation operation. Here, we introduce
A Banother operation called
B swap,
C which swaps the left and right
subtrees of a node. See below for an example.
• Swap the left and right subtrees of an arbitrary node.
x x
A B B A
1 4 1 2 3 3 4 2 5 3 5 3 5 2 2 5
5 2 4 1 4 4 1 5 1 2 2 1 3 1 3
1
5 5 5 1 1
Figure 1.29. “Sorting” a binary tree: rotate 2, rotate 2, swap 3, rotate 3, rotate 4, swap 3, rotate 2, swap 4.
On the other hand, you may compute anything you like for free, as long as that computation does
not modify the tree. In this problem, the running time of your algorithm is defined to be the number of
rotations and swaps that it performs.
Problem 4
(a) Show the red-black trees that result after successively inserting the keys 88, 77, 66, 22, 33, 11 into
an initially empty red-black tree. Note, you need to show the red-black tree after each insertion.
(b) Following part (a), now show the red-black trees that result from the successive deletion of the keys
in the order 11, 22, 33, 66, 77, 88. Again, you need to show the red-black tree after each deletion.
Problem 5
The join operation takes two ordered sets S1 and S2 and an element x such that for any x1 ∈ S1 and
x2 ∈ S2 , we have x1 .key ≤ x.key ≤ x2 .key. It returns a set S = S1 ∪ {x} ∪ S2 . In this problem, we
investigate how to implement the join operation on red-black trees.
(a) Suppose that you store the black-height of a red-black tree T as the new attribute T.bh. Argue
that RBI NSERT and RBD ELETE (i.e., insertion and deletion operations) can maintain the bh attribute
without requiring extra storage in the nodes of the tree and without increasing the asymptotic running
times. Also, show how to determine the black height of each node visited while descending through T ,
using O(1) time per node visited.
Let T1 and T2 be red-black trees and x be an element such that for any nodes x1 ∈ T1 and x2 ∈ T2 , we
have x1 .key ≤ x.key ≤ x2 .key. You will show how to implement the operation RBJ OIN(T1 , x, T2 ),
which destroys T1 and T2 and returns a red-black tree T = T1 ∪ {x} ∪ T2 . Let n be the total number of
nodes in T1 and T2 .
(b) Assume that T1 .bh ≥ T2 .bh. Describe an O(log n)-time procedure that finds a black node y in T1
with the largest key from among those nodes whose black height is T2 .bh.
(c) Let Ty be the subtree rooted at y. Describe how Ty ∪ {x} ∪ T2 can replace Ty in O(1) time without
breaking the binary-search-tree property.
(d) What color should you make x so that red-black tree properties 1, 3, and 5 are maintained? Describe
how to enforce properties 2 and 4 in O(log n) time.
(e) Argue that no generality is lost by making the assumption in part (b). In particular, briefly describe
how to deal with the symmetric situation that arises when T1 .bh ≤ T2 .bh.
(f) Argue that the running time of RBJ OIN is O(log n).
Problem 6
(a) Assume that you are given a size n sorted array A = (x1 , · · · , xn ). Design an algorithm that builds
a random treap containing the elements in A in O(n) worst-case time. You may assume that you have
access to a function R ANDOM() that returns an integer in [N ] uniformly at random, where N ≫ n.
Moreover, each invocation of R ANDOM() takes O(1) time.
(b) Recall that we say an event happens “with high probability (in n)” if it happens with probability at
least 1 − 1/n. Prove the following basic facts about skip lists, where n is the number of keys.
1. The max level is O(log n) with high probability.
2. The total number of nodes in the skip list is O(n) in expectation.
3. The total number of nodes in the skip list is O(n) with high probability. (Hint: You may need to
apply concentration inequalities like Chernoff bounds.)