1 / 22

Balanced Search Trees

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.

marinel
Download Presentation

Balanced Search 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. 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.

  2. 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.

  3. 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.

  4. S B A B a b C D c d E e m f g Split A Binary Search Tree

  5. S B Split A Binary Search Tree B A b C a D c d E e m f g

  6. S B Split A Binary Search Tree A B C a b D c d E e m f g

  7. S B Split A Binary Search Tree A B a b C D c d E e m f g

  8. S B Split A Binary Search Tree A B a b C c D E d e m f g

  9. S B Split A Binary Search Tree A B E a b C e c D d m f g

  10. S B Split A Binary Search Tree A B E a b C e g c D f d m

  11. 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.

  12. S B A B a b C D c d E e m f g RL Move

  13. S B C D c d E e m f g RL Move A B a b Same outcome as in split.

  14. S B C D c d E e m f g RR Move A B a b

  15. S B RR Move A B a D b C E c d e m Rotation performed. f g Outcome is different from split.

  16. S B L Move A B a D b C E c d e m f g

  17. S B L Move A B E a D b e C c d m f g

  18. S B Wrap Up m f g A B E a D b e C c d

  19. S B Wrap Up m A B E a D b g e C f c d

  20. Wrap Up m A B E a D b g e C f c d

  21. YouTube lecture • CS 61B Lecture 34: Splay Trees

  22. Java Applets • http://www.ibr.cs.tu-bs.de/courses/ss98/audii/applets/BST/SplayTree-Example.html • http://techunix.technion.ac.il/~itai/

More Related