1 / 22

Splay Trees

Splay Trees. In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete operations are like in binary search trees, except at the end of each operation a special step called splaying is done.

csilla
Download Presentation

Splay Trees

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. Splay Trees • In balanced tree schemes, explicit rules are followed to ensure balance. • In splay trees, there are no such rules. • Search, insert, and delete operations are like in binary search trees, except at the end of each operation a special step called splaying is done. • Splaying ensures that all operations take O(lg n) amortized time. • First, a quick review of BST operations…

  2. BST: Search Search(25) Search(76) 44 17 88 32 65 97 28 54 82 29 76 80

  3. BST: Insert Insert(78) 44 17 88 32 65 97 28 54 82 29 76 80 78

  4. Has only one child: just splice out 32. BST: Delete 44 Delete(32) 17 88 32 65 97 28 54 82 29 76 80 78

  5. BST: Delete 44 Delete(32) 17 88 28 65 97 54 82 29 76 80 78

  6. Has two children: Replace 65 by successor, 76, and splice out successor. Note: Successor can have at most one child. (Why?) BST: Delete 44 Delete(65) 17 88 32 65 97 28 54 82 29 76 80 78

  7. BST: Delete 44 Delete(65) 17 88 32 76 97 28 54 82 80 29 78

  8. Splaying • In splay trees, after performing an ordinary BST Search, Insert, or Delete, a splay operation is performed on some node x (as described later). • The splay operation moves x to the root of the tree. • The splay operation consists of sub-operations called zig-zig, zig-zag, and zig.

  9. Zig-Zig x z x has a grandparent 30 10 y y 20 20 T4 T1 x z 10 30 T3 T2 T1 T2 T3 T4 (Symmetric case too) Note: x’s depth decreases by two.

  10. Zig-Zag x z x has a grandparent 20 10 y 30 T1 x z y 10 20 30 T4 T1 T2 T3 T4 T3 T2 (Symmetric case too) Note: x’s depth decreases by two.

  11. Zig x has no grandparent (so, y is the root) Note: w could be NIL x y 20 10 x 20 T1 w y w 10 30 30 T2 T1 T2 T3 T4 T3 T4 (Symmetric case too) Note: x’s depth decreases by one.

  12. zig-zag Complete Example 44 50 Splay(78) 17 88 32 65 97 28 54 82 z 29 76 y 80 x 78

  13. Complete Example 44 50 Splay(78) 17 88 32 65 97 zig-zag 28 54 82 x 29 78 y z 80 76

  14. zig-zag Complete Example 44 50 Splay(78) 17 88 z 32 65 97 y 28 54 82 x 29 78 80 76

  15. Complete Example 44 50 Splay(78) 17 88 x 78 32 97 zig-zag z y 28 82 65 76 54 80 29

  16. zig-zag Complete Example 44 z 50 Splay(78) y 17 88 x 78 32 97 28 82 65 76 54 80 29

  17. Complete Example 44 x 78 Splay(78) y 17 88 z 50 82 32 97 65 zig-zag 80 28 76 54 29

  18. zig Complete Example y 44 x 78 Splay(78) w 17 88 50 82 32 97 65 80 28 76 54 29

  19. Complete Example x 78 y 44 Splay(78) w 17 88 50 82 32 97 65 zig 80 28 76 54 29

  20. Result of splaying • The result is a binary tree, with the left subtree having all keys less than the root, and the right subtree having keys greater than the root. • Also, the final tree is “more balanced” than the original. • However, if an operation near the root is done, the tree can become less balanced.

  21. When to Splay • Search: • Successful: Splay node where key was found. • Unsuccessful: Splay last-visited internal node (i.e., last node with a key). • Insert: • Splay newly added node. • Delete: • Splay parent of removed node (which is either the node with the deleted key or its successor). • Note: All operations run in O(h) time, for a tree of height h.

  22. Unbalancing the Tree • In fact, a sequence of zig operations can result in a completely unbalanced linear tree. Then a search operation can take O(n) time, but this is OK because at least n operations have been performed up to this point.

More Related