1 / 41

15-211 Fundamental Data Structures and Algorithms

15-211 Fundamental Data Structures and Algorithms. Splay Trees. Ananda Guna February 8, 2005. Announcements. Homework 2 available! Due Monday, Feb.16, 11:59pm! More involved than hw1? Perhaps. Start early! It is very important that you go to recitations: We will discuss splaying.

todd
Télécharger la présentation

15-211 Fundamental Data Structures and Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 15-211Fundamental Data Structures and Algorithms Splay Trees Ananda Guna February 8, 2005

  2. Announcements • Homework 2 available! • Due Monday, Feb.16, 11:59pm! • More involved than hw1? Perhaps. • Start early! • It is very importantthat you go to recitations: • We will discuss splaying

  3. Splay Trees (self adjusting trees)

  4. Balanced BST’s • O(log n) performance • Best, average and worst cases • Need to store balance information per node • expensive • Difficult to implement • Insertions and deletions are difficult • Even for easy inputs we still have O(log n) behavior • No cost savings

  5. Binary search trees • Simple binary search trees can have bad behavior for some insertion sequences. • Average case O(log N), worst case O(N). • AVL trees maintain a balance invariant to prevent this bad behavior. • Accomplished via rotations during insert. • Splay trees achieve amortized running time of O(log N). • Accomplished via rotations during find.

  6. Amortized running time • The analysis that allows us to conclude that we spend the same (or less) amount of time over a sequence of operations is called amortized analysis. • If we say that the amortized running time of a sequence of operations is O(f(N)): • Some operations might be more than O(f(N)), other less. • But the average over the entire sequence is O(f(N)).

  7. Splay trees • Splay trees provide a guarantee that any sequence of M operations (starting from an empty tree) will require O(Mlog N) time. • Hence, each operation has amortized cost of O(log N). • It is possible that a single operation requires O(N) time. • But there are no bad sequences of operations on a splay tree.

  8. A basic observation • Let’s suppose that a node requires O(N) time to find. • If and when this happens, we must move it somewhere closer to the root so that if we access it again, it will definitely require less than O(N) time. • If we don’t do this, then O(log N) amortized behavior is not possible.

  9. Danny Sleator • The inventor of splay trees. • Winner of Kannelakis award. • See his splay tree demo at http://www.cs.cmu.edu/~sleator • (And definitely a procrastinator.)

  10. The basic idea • Every time a node is accessed, move it to the root. • The move can be accomplished by performing AVL rotations. • Practical benefits: • In practice, nodes are often accessed multiple times. • AVL rotations make trees more balanced.

  11. Using rotations • Suppose we perform a find operation, which accesses node n. • Then perform AVL rotations, starting from n, to move it up to the root. • Doing this requires O(d) time, where d is the depth of n. • But a subsequent access of n will require only O(1) time, and the tree will be better balanced, too.

  12. A simple example 0 move 5 to root \ 1 \ 2 \ 3 \ 4 \ 5

  13. Example Ctd.. This is the "move-to-root heuristic".

  14. Example 2 (move 2 to root) 6 / \ 1 7 / \ 0 3 / \ 2 5 / 4

  15. Work sheet

  16. Move to Root • Move-to-root() is an example of a "self-adjusting" heuristic • Every time we search for a node x, we do move-to-root(x) • Works well if we search for few different nodes • Idea is to try to maintain log(n) performance over a sequence of operations.

  17. Important Concept - Amortized Time • Question: Is it possible that by applying move-to-root() on every search gives amortized time bounded by O(log n) for any access pattern? • Answer:

  18. Bottom Up Splaying

  19. Basic Cases Zig-Zag: z z x \ \ / \ y ===> x ===> z y / \ x y

  20. Basic Cases Zig-Zig: z y x / / \ \ y ===>x z ===> y / \ x z

  21. Basic Cases • Zig y x / ===> \ x y

  22. Example 6 / \ 1 7 / \ 0 2 \ 3 \ 5 / 4

  23. Work area

  24. Splaying, case 1 • There are four cases to consider. • Case 1: The node is already the root. • Nothing to do.

  25. X Y Z Splaying, case 2 • Case 2: Accessed node’s parent is the root. • Perform a single rotation. Z X Y

  26. Splaying, cases 3 and 4 • The next two cases cover the situation in which the accessed node has a grandparent.

  27. a b b a Z X X Y1 Y2 Z Y1 Y2 Splaying, case 3 • Case 3: Zig-zag (left). • Perform an AVL double rotation.

  28. Splaying, case 4 • Case 4: Zig-zig (left). • Special rotation. a b Z W b Y a X X W Y Z

  29. Symmetry • And there are symmetric cases for zig-zag and zig-zig to the right.

  30. 0 0 1 1 2 2 3 3 4 6 5 5 6 4 zig-zig right Splay tree example Insert {0, 1, 2, 3, 4, 5, 6}, then find 6

  31. 0 0 1 1 2 6 3 3 6 2 5 5 4 4 zig-zag right Splay tree example, cont’d

  32. 0 6 1 1 6 0 3 3 2 5 2 5 4 4 zig-zag right Splaying example, cont’d

  33. 6 1 0 3 2 5 4 Result of splaying • Access of 6 required N nodes visited and modified. • But now accessing 5 requires only N/2 nodes visited and modified. • Will also bring all nodes up to N/4 of root. (Try it!) • And all nodes are shallower. • Every access will tend to improve the tree for future operations.

  34. Operations on splay trees • Search – find the element and splay the resulting node • Insertion – insert the element and splay the node just inserted

  35. Delete Operation Deletion Splay the node we want to delete(say x) to the root x A B • find the rightmost node of A (say y) and splay y to the root • Attach the parts

  36. Splaying summary • Splaying has the effect of moving the accessed node to the root. • It also reduces the depth of almost all of the nodes along the access path.

  37. Properties of Splaying • Theorem: A sequence of M splay operations on a tree of N nodes takes time O(M log N). (Assuming M > N) • Proof: beyond the scope of this course. • Theorem: sequentially splaying all the nodes in the tree takes O(N) time. • Proof: beyond the scope of this course.

  38. Analysis of splay trees • The analysis of the running time of splay trees is quite difficult. • Any single find or insert might take O(N) time. • But any sequence of M operations, starting from an empty tree, will take only O(Mlog N) time. • In practice, splay trees work extremely well. • http://www.cs.technion.ac.il/~itai/ds2/framesplay/splay.html

  39. BST’s vs Hash Tables • Question: Hash tables have O(1) performance for lookups, inserts and deletes, what is the use of search trees that can be O(log N) at best? • Answer:

  40. Some nice things about BST’s • Find the maximum or minimum element • Find the successor (predecessor) of a given element in the set. • Searching and computing the "rank" in that ordering. • The RANK of an element in an ordered list is the number of elements before it in the list. • Splitting and joining (example from perl) • Prefix matching

  41. Thursday • Dynamic Programming • Start HW2

More Related