<<

Self adjusting Trees Splay Trees • Ordinary binary search trees have no balance conditions CSE 373 › what you get from insertion order is it Data • Balanced trees like AVL trees enforce a Unit 8 balance condition when nodes change › is always balanced after an insert or delete • Self-adjusting trees get reorganized over time Reading: Sections 4.5-4.6 as nodes are accessed › Tree adjusts after insert, delete, or find

2

Splay Trees Terminology

• Splay trees are tree structures that: • Let X be a non-root with ≥ 2 ancestors. • P is its parent node. › Are not perfectly balanced all the time • G is its grandparent node. › Data most recently accessed is near the root. (principle of locality; 80-20 “rule”) G G G G • The procedure: › After node X is accessed, perform “splaying” P P P P operations to bring X to the root of the tree. › Do this in a way that leaves the tree more X X X X balanced as a whole

3 4 Zig-Zig and Zig-Zag Splay Tree Operations

Parent and grandparent Parent and grandparent 1. Helpful if nodes contain a parent pointer. in same direction. in different directions. parent element zig-zig 4 left right G G 5 2. When X is accessed, apply one of six rotation routines. P 5 • Single Rotations (X has a P (the root) but no G) zig-zag 1 P ZigFromLeft, ZigFromRight X 2 • Double Rotations (X has both a P and a G) X ZigZigFromLeft, ZigZigFromRight ZigZagFromLeft, ZigZagFromRight

5 6

Zig at depth 1 (root) Zig at depth 1 • “Zig” is just a single rotation, as in an AVL tree • Suppose Q is now accessed using Find • Let R be the node that was accessed (e.g. using Find) root root

ZigFromRight ZigFromLeft

• ZigFromLeft moves R to the top →faster access next time • ZigFromRight moves Q back to the top

7 8 Zig-Zag operation Zig-Zig operation

• “Zig-Zag” consists of two rotations of the • “Zig-Zig” consists of two single rotations opposite direction (assume R is the node that of the same direction (R is the node that was accessed) was accessed)

(ZigFromRight) (ZigFromLeft)

(ZigFromLeft) (ZigFromLeft)

ZigZagFromLeft ZigZigFromLeft

9 10

Decreasing depth - Splay Tree Insert and Delete "autobalance" • Insert x › Insert x as normal then splay x to root. • Delete x › Splay x to root and remove it. (note: the node does not have to be a leaf or single child node like in BST delete.) Two trees remain, right subtree and left subtree. › Splay the max in the left subtree to the root › Attach the right subtree to the new root of the left Find(T) Find(R) subtree.

11 12 Example Insert With Self-Adjustment

• Inserting in order 1,2,3,…,8 1 1 • Without self-adjustment

1 ZigFromRight 1 2 2 O(n2) time for n Insert 2 3 2 1 4 3 5 3 2 ZigFromRight 2 6 1 3 7 1 8 13 14

With Self-Adjustment Example Deletion

delete(8) 10 splay(Zig-Zag) 8 5 15 4 3 5 10 4 2 4 ZigFromRight 2 8 13 20 2 6 9 15 3 1 13 20 2 6 9 Splay (zig) remove 1 6 attach 5 10 5 10 Each Insert takes O(1) time therefore O(n) time for n Insert!! 15 2 9 15 2 6 9

13 20 13 20

15 16 Analysis of Splay Trees Summary of Search Trees

• Problem with Binary Search Trees: Must keep tree balanced to • Splay trees tend to be balanced allow fast access to stored items › M operations takes time O(M log N) for M > N operations on N items. (proof is difficult) • AVL trees: Insert/Delete operations keep tree balanced • Splay trees: Repeated Find operations produce balanced trees › Amortized O(log n) time. • Multi-way search trees (e.g. B-Trees): • Splay trees have good “locality” properties › More than two children per node allows shallow trees; all › Recently accessed items are near the root of leaves are at the same depth. the tree. › Keeping tree balanced at all times. › Excellent for indexes in database systems. › Items near an accessed one are pulled toward the root.

17 18