2 Handout 18: <strong>Problem</strong> <strong>Set</strong> 4 <strong>Solutions</strong>have been formed if the nodes had been inserted into a normal binary search tree in the order givenby their (randomly chosen) priorities. In other words, priority[x i ] < priority[x j ] means that x i iseffectively inserted before x j .(a) Given a set of nodes x 1 ,x 2 ,...,x n with keys and priorities all distinct, show that thereis a unique treap with these nodes.Solution:Prove by induction on the number of nodes in the tree. The base case is a tree withzero nodes, which is trivially unique. Assume for induction that treaps with k − 1 orfewer nodes are unique. We prove that a treap with k nodes is unique. In this treap, theitem x with minimum priority must be at the root. The left subtree has items with keys< key[x] and the right subtree has items with keys > key[x]. This uniquely defines theroot and both subtrees of the root. Each subtree is a treap of size ≤ k − 1, so they areunique by induction.Alternatively, one can also prove this by considering a treap in which nodes are insertedin order of their priority. Assume for induction that the treap with the k − 1nodes with smallest priority is unique. For k = 0 this is trivially true. Now considerthe treap with the k nodes with smallest priority. Since we know that the structureof a treap is the same as the structure of a binary search tree in which the keys areinserted in increasing priority order, the treap with the k nodes with smallest priorityis the same as the treap with the k − 1 nodes with smallest priority after inserting thek-th node. Since BST insert is a deterministic algorithm, there is only one place thek-th node could be inserted. Therefore the treap with k nodes is also unique, provingthe inductive hypothesis.(b) Show that the expected height of a treap is O(lg n), and hence the expected time tosearch for a value in the treap is O(lg n).Solution: The idea is to realize that a treap on n nodes is equivalent to a randomlybuilt binary search tree on n nodes. Recall that assigning priorities to nodes as theyare inserted into the treap is the same as inserting the n nodes in the increasing orderdefined by their priorities. So if we assign the priorities randomly, we get a randomorder of n priorities, which is the same as a random permutation of the n inputs, sowe can view this as inserting the n items in random order.The time to search for an item is O(h) where h is the height of the tree. As we saw inlecture, E[h] = O(lg n). (The expectation is taken over permutations of the n nodes,i.e., the random choices of the priorities.)Let us see how to insert a new node x into an existing treap. The first thing we do is assign x arandom priority priority[x]. Then we call the insertion algorithm, which we call TREAP-INSERT,whose operation is illustrated in Figure 2.
Handout 18: <strong>Problem</strong> <strong>Set</strong> 4 <strong>Solutions</strong>3G: 4 G: 4B: 7H: 5C: 25B: 7H: 5A: 10 E: 23 K: 65A: 10 E: 23K: 65I: 73C: 25I: 73(a)(b)G: 4G: 4D: 9B: 7H: 5B: 7H: 5A: 10 E: 23K: 65A: 10 E: 23K: 65C: 25I:73D: 9I:73D: 9 C: 25(c)(d)G: 4F: 2B: 7H: 5F: 2. . .B: 7G: 4A: 10D: 9K: 65A: 10D: 9H: 5C: 25E: 23I:73C: 25E: 23 K: 65I:73(e)(f)Figure 2: Operation of TREAP-INSERT. As in Figure 1, each node x is labeled with key[x] :priority[x]. (a) Original treap prior to insertion. (b) The treap after inserting a node with keyC and priority 25. (c)–(d) Intermediate stages when inserting a node with key D and priority 9.(e) The treap after insertion of parts (c) and (d) is done. (f) The treap after inserting a node withkey F and priority 2.