220 likes | 443 Views
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.
E N D
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…
BST: Search Search(25) Search(76) 44 17 88 32 65 97 28 54 82 29 76 80
BST: Insert Insert(78) 44 17 88 32 65 97 28 54 82 29 76 80 78
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
BST: Delete 44 Delete(32) 17 88 28 65 97 54 82 29 76 80 78
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
BST: Delete 44 Delete(65) 17 88 32 76 97 28 54 82 80 29 78
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.
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.
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.
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.
zig-zag Complete Example 44 50 Splay(78) 17 88 32 65 97 28 54 82 z 29 76 y 80 x 78
Complete Example 44 50 Splay(78) 17 88 32 65 97 zig-zag 28 54 82 x 29 78 y z 80 76
zig-zag Complete Example 44 50 Splay(78) 17 88 z 32 65 97 y 28 54 82 x 29 78 80 76
Complete Example 44 50 Splay(78) 17 88 x 78 32 97 zig-zag z y 28 82 65 76 54 80 29
zig-zag Complete Example 44 z 50 Splay(78) y 17 88 x 78 32 97 28 82 65 76 54 80 29
Complete Example 44 x 78 Splay(78) y 17 88 z 50 82 32 97 65 zig-zag 80 28 76 54 29
zig Complete Example y 44 x 78 Splay(78) w 17 88 50 82 32 97 65 80 28 76 54 29
Complete Example x 78 y 44 Splay(78) w 17 88 50 82 32 97 65 zig 80 28 76 54 29
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.
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.
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.