Oct 26, 2001CSE 373, Autumn 20011 A Forest of Trees Binary search trees: simple. –good on average:...

11
Oct 26, 200 1 CSE 373, Autumn 20 01 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).

Transcript of Oct 26, 2001CSE 373, Autumn 20011 A Forest of Trees Binary search trees: simple. –good on average:...

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

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

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

Oct 26, 2001 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.

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

Oct 26, 2001 CSE 373, Autumn 2001 3

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), …

Page 4: 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.

Oct 26, 2001 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.

Page 5: 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.

Oct 26, 2001 CSE 373, Autumn 2001 5

Case 0: X is the root

• Do nothing!

X

A

Page 6: 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.

Oct 26, 2001 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

CB

X

A

X

CB

P

A

Page 7: 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.

Oct 26, 2001 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.

P

CB

X

A

G

D

P

C

B

GA

X

D

Page 8: 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.

Oct 26, 2001 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.

P

CB

X

A

G

D

P

CB

G

A

X

D

Page 9: 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.

Oct 26, 2001 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?)

Page 10: 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.

Oct 26, 2001 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.

T2

J

T1

K

T2

J

T1

T

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

Oct 26, 2001 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.

T2

K

T1TT2T1

Splay(findMax(T1))

T2

M