1 / 11

A Forest of Trees

A Forest of Trees. Binary search trees : simple. good on average: O(log n ) bad in the worst case: O( n ) AVL trees : more complicated. good worst case bound: O(log n ) Splay trees : “medium” complexity of code.

rwheeler
Download Presentation

A Forest of 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. A Forest of Trees • Binary search trees: simple. • good on average: O(log n) • bad in the worst case: O(n) • AVL trees: more complicated. • good worst case bound: O(log n) • Splay trees: “medium” complexity of code. • guaranteed good performance over a long sequence of operations: O(m log n) over m operations (amortized running time). CSE 373, Autumn 2001

  2. Self-adjusting structures • To avoid repeated accesses to deep nodes, the (tree) structure needs to alter itself after each operation. • Idea: Since AVL rotation seems to reduce the depth of nodes, do an AVL rotate (from node to root) any time we do an operation. CSE 373, Autumn 2001

  3. Idea does not work 7 1 6 7 5 6 find(1) 4 5 3 4 2 3 1 2 find(2), find(3), find(4), … CSE 373, Autumn 2001

  4. Splay operation • Splay(K): • Search for K in the usual way. • Let X be the last node inspected. • If K is in the tree, then K is in X. • If K is not in the tree, then P has an empty child where the search for K terminated. • Follow the path from X to the root, carrying out rotations along the way. CSE 373, Autumn 2001

  5. Case 0: X is the root • Do nothing! X A CSE 373, Autumn 2001

  6. Case 1: no grandparent • X has no grandparent (i.e., X’s parent is the root). Perform a single rotation on X and X’s parent. P X P X C A C B B A CSE 373, Autumn 2001

  7. Case 2: zig-zig • X and X’s parent are both left children or both right children. Perform two single rotations: • X’s grandparent and X’s parent. • X and X’s parent. G X P P A D G B X C C D A B CSE 373, Autumn 2001

  8. Case 3: zig-zag • One of X and X’s parent is a left child and the other is a right child. Perform a double rotation. X G P G P D B D X A C A B C CSE 373, Autumn 2001

  9. Properties of Splay(K) • If K is in the tree, then the resulting BST will have K at the root. • If K is not in the tree, then the root contains a key that would be the successor or predecessor of K, if K were in the tree. (What determines whether the successor predecessor is at the root?) CSE 373, Autumn 2001

  10. Splay trees: find, insert • Find(K): Do Splay(K). Examine the root to see if it has K. • Insert(I, K): Do Splay(K). If K is at the root, replace its element with I. Otherwise, create a new node from K and I and break one link to make this node the new root. J K T T2 T1 J T2 T1 CSE 373, Autumn 2001

  11. Splay tree: remove • Remove(K): Do Splay(K). If the root does not contain K, do nothing. Otherwise, delete the root and Concat the two subtrees of the root. K T T1 T2 T2 T1 M Splay(findMax(T1)) T2 CSE 373, Autumn 2001

More Related