Presentation is loading. Please wait.

Presentation is loading. Please wait.

Oct 26, 2001CSE 373, Autumn 20011 A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.

Similar presentations


Presentation on theme: "Oct 26, 2001CSE 373, Autumn 20011 A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more."— Presentation transcript:

1 Oct 26, 2001CSE 373, Autumn 20011 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).

2 Oct 26, 2001CSE 373, Autumn 20012 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.

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

4 Oct 26, 2001CSE 373, Autumn 20014 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.

5 Oct 26, 2001CSE 373, Autumn 20015 Case 0: X is the root Do nothing! X A

6 Oct 26, 2001CSE 373, Autumn 20016 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 C B X A X C B P A

7 Oct 26, 2001CSE 373, Autumn 20017 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. P C B X A G D P C B G A X D

8 Oct 26, 2001CSE 373, Autumn 20018 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. P C B X A G D P C B G A X D

9 Oct 26, 2001CSE 373, Autumn 20019 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?)

10 Oct 26, 2001CSE 373, Autumn 200110 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. T2T2 J T1T1 K T2T2 J T1T1 T

11 Oct 26, 2001CSE 373, Autumn 200111 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. T2T2 K T1T1 T T2T2 T1T1 Splay(findMax(T 1 )) T2T2 M


Download ppt "Oct 26, 2001CSE 373, Autumn 20011 A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more."

Similar presentations


Ads by Google