lec8 - Lecture 8 Treaps Find, insert, delete, split, and...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Page 1 of 33 CSE 100, UCSD: LEC8 Lecture 8 Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs Reading: “Randomized Search Trees” paper (Aragon & Seidel): http://people.ischool.berkeley.edu/~aragon/pubs/rst96.pdf Weiss, Chapter 12 section 5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 2 of 33 CSE 100, UCSD: LEC8 Trees, heaps, and treaps A binary search tree (BST) is a binary tree: Each Node in a BST contains a key; key values are comparable to each other A BST has the BST ordering property: For every node X, the key in X is greater than all keys in the left subtree of X, and less than all keys in the right subtree of X A heap is a binary tree: Each Node in a heap contains a priority; priority values are comparable to each other A heap has the heap ordering property: For every node X, the priority in X is greater than or equal to all priorities in the left and right subtrees of X A treap is a binary tree: Nodes in a treap contain both a key, and a priority A treap has the BST ordering property with respect to its keys, and the heap ordering property with respect to its priorities
Background image of page 2
Page 3 of 33 CSE 100, UCSD: LEC8 Treaps: an example Suppose keys are letters, with alphabetic ordering; priorities are integers, with numeric ordering. This tree is a treap: G,50 C,35 B,24 A,21 E,33 H,29 I,25 L,16 J,13 K,9 D,8
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Page 4 of 33 CSE 100, UCSD: LEC8 Uniqueness of treaps Given a set of (key,priority) pairs, with all the key values unique, you could always construct a treap containing those (key,priority) pairs: Start with an empty treap Insert the (key,priority) pairs in decreasing order of priority, using the usual binary search tree insert algorithm that pays attention to the key values only The result is a treap: BST ordering of keys is enforced by the BST insert, heap ordering of priorities is enforced by inserting in priority sorted order If the priority values as well as the key values are unique, the treap containing the (key,priority) pairs is unique For example, the treap on the previous page is the unique treap containing these pairs: (G,50),(C,35),(E,33),(H,29),(I,25),(B,24),(A,21),(L,16),(J,13),(K,9),(D,8) Of course we will really be interested in algorithms that create a treap from (key,priority) pairs no matter what order they are inserted in.
Background image of page 4
Page 5 of 33 CSE 100, UCSD: LEC8 Operations on treaps Treaps permit insert, delete, and find operations (also others but these are basic) Finding a key in a treap is very easy: just use the usual BST search algorithm Insert and delete of keys are slightly more complicated, since the operations must respect both the BST and heap ordering properties as invariants Recall that insert and delete in heaps use “bubble up” and “trickle down” exchanges to restore the heap ordering property Those same operations won’t work in treaps, because they can destroy the BST ordering property The trick is to use AVL rotations instead of exchanges: An AVL rotation always preserves BST ordering, and so it can be used to move a (key,priority) pair up or down the tree to correct a failure of heap ordering without disturbing BST ordering
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 33

lec8 - Lecture 8 Treaps Find, insert, delete, split, and...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online