230 likes | 344 Views
Balanced Search Trees. Problem: Efficiency of BST is related to tree’s height. search, insert and remove follow a path from root to desired location range in value for ceil(log (N+1)) to N What affects height of BST? sensitive to order of data in which you insert or delete items.
E N D
Balanced Search Trees • Problem: Efficiency of BST is related to tree’s height. • search, insert and remove follow a path from root to desired location • range in value for ceil(log (N+1)) to N • What affects height of BST? • sensitive to order of data in which you insert or delete items. • Solution: Maintain balance by using variation of BST.
Splay Trees • Type of balanced binary search trees. • Search, insert, delete, and split have amortized complexity O(log n) & actual complexity O(n). • Actual and amortized complexity of join is O(1). • Priority queue and double-ended priority queue versions outperform heaps over a sequence of operations. • Two varieties. • Bottom up. • Top down.
Top-Down Splay Trees • On the way down the tree, split the tree into the binary search trees S (smallelements)and B (bigelements). • Similar to split operation in an unbalanced binary search tree. • However, a rotation is done whenever an LL or RR move is made. • Move down 2 levels at a time, except (possibly) in the end when a one level move is made. • When the splay node is reached, S, B,and the subtree rooted at the splay node are combined into a single binary search tree.
S B A B a b C D c d E e m f g Split A Binary Search Tree
S B Split A Binary Search Tree B A b C a D c d E e m f g
S B Split A Binary Search Tree A B C a b D c d E e m f g
S B Split A Binary Search Tree A B a b C D c d E e m f g
S B Split A Binary Search Tree A B a b C c D E d e m f g
S B Split A Binary Search Tree A B E a b C e c D d m f g
S B Split A Binary Search Tree A B E a b C e g c D f d m
A B a b C D c d E e m f g Two-Level Moves • RL move from A to C. • RR move from C to E. • L move from E to m. • Let m be the splay node.
S B A B a b C D c d E e m f g RL Move
S B C D c d E e m f g RL Move A B a b Same outcome as in split.
S B C D c d E e m f g RR Move A B a b
S B RR Move A B a D b C E c d e m Rotation performed. f g Outcome is different from split.
S B L Move A B a D b C E c d e m f g
S B L Move A B E a D b e C c d m f g
S B Wrap Up m f g A B E a D b e C c d
S B Wrap Up m A B E a D b g e C f c d
Wrap Up m A B E a D b g e C f c d
YouTube lecture • CS 61B Lecture 34: Splay Trees
Java Applets • http://www.ibr.cs.tu-bs.de/courses/ss98/audii/applets/BST/SplayTree-Example.html • http://techunix.technion.ac.il/~itai/