CSE 326: Data Structures Lecture #12 Splay It Again, Sam Steve Wolfman Winter Quarter 2000 Today’s Outline • • • • Finish off splay trees Summary of the class so far Discuss Projects III & IV Free time Why Splaying Helps • If a node n on the access path is at depth d before the splay, it’s at about depth d/2 after the splay – Exceptions are the root, the child of the root, and the node splayed • Overall, nodes which are below nodes on the access path tend to move closer to the root • Splaying gets amortized O(log n) performance. (Maybe not now, but soon, and for the rest of the operations.) Splay Operations: Find • Find the node in normal BST manner • Splay the node to the root Splay Operations: Insert • Ideas? Can we just do BST insert? Digression: Splitting • Split(T, x) creates two BSTs L and R: – – – – all elements of T are in either L or R (T = L R) all elements in L are x all elements in R are x L and R share no elements (L R = ) Splitting in Splay Trees • How can we split? (SPOILERS below ^L) – – – – – We have the splay operation. We can find x or the parent of where x should be. We can splay it to the root. Now, what’s true about the left subtree of the root? And the right? Split split(x) splay T L void split(Node * root, Node *& left, Node *& right, Object x) { Node * target = root->find(x); splay(target); if (target < x) { left = target->left; target->left = NULL; right = target; L } x ... } R OR R >x L <x R x Back to Insert x split(x) L <x R >x void insert(Node *& root, Object x) { Node * left, * right; split(root, left, right, x); root = new Node(x, left, right); } L R Splay Operations: Delete x find(x) delete x L R Now what? L <x R >x Join • Join(L, R): given two trees such that L < R, merge them splay L R L R • Splay on the maximum element in L then attach R Delete Completed x find(x) T delete x L R L <x R >x Join(L,R) T-x Insert Example Insert(5) 6 4 1 9 4 2 7 split(5) 1 4 6 1 9 2 6 9 2 7 7 5 4 6 1 9 2 7 Delete Example Delete(4) 6 4 1 9 4 find(4) 6 1 7 6 1 9 2 2 2 1 7 Find max 7 2 9 2 6 1 6 9 7 9 7 Splay Tree Summary • All operations are in amortized O(log n) time • Splaying can be done top-down; better because: – only one pass – no recursion or parent pointers necessary • There are alternatives to split/insert and join/delete • Splay trees are very effective search trees – relatively simple – no extra fields required – excellent locality properties: frequently accessed keys are cheap to find Lists and Multi-Lists • Lists • Multi-lists – Array-based – List of lists: complete – Linked list-based – List of lists: sparse – Sparse – Cross-list Stacks and Queues • Stacks • Queues – array-based – array-based – linked list-based – linked list-based Priority Queues • Naïve • d-Heap • Binary heap • Leftist Heap • Skew Heap Dictionaries and Search Sets • Naïve • AVL Tree • Binary Search Tree • Splay Tree • B-Tree Asymptotic Analysis and Proof by Induction Asymptotic analysis – symbols: O, o, , , and – types: worst, best, average, common, amortized – objectives: time, space – quality: tight vs. loose – the fix sheet – analyzing code – interpretation of analysis • Proof techniques – by counterexample – by contradiction – by induction • base case • induction hypothesis • induction step Project III • Word frequency counting – Splay trees – AVL trees • • • • Writeup out on Friday Get your groups (of 2-3) together quickly Due a week from Monday Should be fairly easy Project IV • Writeup out two weeks from now. • Groups of 4-5 • Four options – – – – Maze generation: k-d trees and disjoint-sets Maze visualization: k-d trees Compression: adaptive heaps, dictionaries MUD/MOO: graphs, hash tables, etc. • More than meets the eye – Maze generation + visualization + MUD = automatically generated, walk-thru, 3-D MUD To Do • Get set for the midterm • Find a group for Project III • Relax over the weekend (at least as far as this class is concerned) Coming Up • • • • Midterm (February 4th) Hash Tables k-d Trees Disjoint-set union-find with up-trees and path compression (does that not sound rocking?)