12.07.2015 Views

Problem Set 4 Solutions

Problem Set 4 Solutions

Problem Set 4 Solutions

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4 Handout 18: <strong>Problem</strong> <strong>Set</strong> 4 <strong>Solutions</strong>(c) Explain how TREAP-INSERT works. Explain the idea in English and give pseudocode.(Hint: Execute the usual binary search tree insert and then perform rotations to restorethe min-heap order property.)Solution: The hint gives the idea: do the usual binary search tree insert and thenperform rotations to restore the min-heap order property.TREAP-INSERT (T, x) inserts x into the treap T (by modifying T ). It requires that xhas defined key and priority values. We have used the subroutines TREE-INSERT,RIGHT-ROTATE, and RIGHT-ROTATE as defined in CLRS.TREAP-INSERT (T, x)1 TREE-INSERT (T, x)2 while x ̸= root[T ] and priority[x] < priority[p[x]]3 do if x = left[p[x]]4 then RIGHT-ROTATE (T, p[x])5 else LEFT-ROTATE (T, p[x])Note that parent pointers simplify the code but are not necessary. Since we only needto know the parent of each node on the path from the root to x (after the call toTREE-INSERT), we can keep track of these ourselves.(d) Show that the expected running time of TREAP-INSERT is O(lg n).Solution:TREAP-INSERT first inserts an item in the tree using the normal binary search treeinsert and then performs a number of rotations to restore the min-heap property.The normal binary-search-tree insertion algorithm TREE-INSERT always places thenew item at a new leaf of tree. Therefore the time to insert an item into a treap isproportional to the height of a randomly built binary search tree, which as we saw inlecture is O(lg n) in expectation.The maximum number of rotations occurs when the new item receives a priority lessthan all priorities in the tree. In this case it needs to be rotated from a leaf to the root.An upper bound on the number of rotations is therefore the height of a randomly builtbinary search tree, which is O(lg n) in expectation. (We will see that this is a fairlyloose bound.) Because each rotation take constant time, the expected time to rotate isO(lg n).Thus the expected running time of TREAP-INSERT is O(lg n +lg n) = O(lg n).TREAP-INSERT performs a search and then a sequence of rotations. Although searching and rotatinghave the same asymptotic running time, they have different costs in practice. A search readsinformation from the treap without modifying it, while a rotation changes parent and child pointerswithin the treap. On most computers, read operations are much faster than write operations. Thuswe would like TREAP-INSERT to perform few rotations. We will show that the expected numberof rotations performed is bounded by a constant (in fact, less than 2)!

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!