CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.

Slides:



Advertisements
Similar presentations
Splay Trees CSE 331 Section 2 James Daly. Reminder Homework 2 is out Due Thursday in class Project 2 is out Covers tree sets Due next Friday at midnight.
Advertisements

AVL Trees CS II – Fall /8/2010. Announcements HW#2 is posted – Uses AVL Trees, so you have to implement an AVL Tree class. Most of the code is provided.
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSE332: Data Abstractions Lecture 10: More B-Trees Tyler Robison Summer
CSE332: Data Abstractions Lecture 7: AVL Trees Dan Grossman Spring 2010.
CPSC 320: Intermediate Algorithm Design & Analysis Splay Trees (for Amortized Analysis) Steve Wolfman 1.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
CSE 326: Data Structures AVL Trees
CSE 326: Data Structures B-Trees Ben Lerner Summer 2007.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
CSC 2300 Data Structures & Algorithms February 16, 2007 Chapter 4. Trees.
Splay Trees Splay trees are binary search trees (BSTs) that:
0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Splay Trees and B-Trees
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Nicki Dell Spring 2014 CSE373: Data Structures & Algorithms1.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
CMSC 341 Splay Trees. 8/3/2007 UMBC CMSC 341 SplayTrees 2 Problems with BSTs Because the shape of a BST is determined by the order that data is inserted,
B-Trees and Red Black Trees. Binary Trees B Trees spread data all over – Fine for memory – Bad on disks.
Balanced Search Trees Problem: Efficiency of BST is related to tree’s height.  search, insert and remove follow a path from root to desired location 
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
CMSC420: Splay Trees Kinga Dobolyi Based off notes by Dave Mount.
CSE 326: Data Structures Lecture #12 Splay It Again, Sam Steve Wolfman Winter Quarter 2000.
Oct 26, 2001CSE 373, Autumn A Forest of Trees Binary search trees: simple. –good on average: O(log n) –bad in the worst case: O(n) AVL trees: more.
CSE 332 Data Abstractions: A Heterozygous Forest of AVL, Splay, and B Trees Kate Deibel Summer 2012 July 2, 2012CSE 332 Data Abstractions, Summer
Review for Exam 2 Topics covered (since exam 1): –Splay Tree –K-D Trees –RB Tree –Priority Queue and Binary Heap –B-Tree For each of these data structures.
CSE 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
AVL Trees / Slide 1 Height-balanced trees AVL trees height is no more than 2 log 2 n (n is the number of nodes) Proof based on a recurrence formula for.
1 CSE 326: Data Structures Trees. 2 Today: Splay Trees Fast both in worst-case amortized analysis and in practice Are used in the kernel of NT for keep.
Jim Anderson Comp 750, Fall 2009 Splay Trees - 1 Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees Linda Shapiro Winter 2015.
Fundamental Data Structures and Algorithms Jeffrey Stylos February 8 th, 2005 Splay Trees Adapted from slides by Peter Lee October 4, 2001.
Review for Exam 2 Topics covered: –Recursion and recursive functions –General rooted trees –Binary Search Trees (BST) –Splay Tree –RB Tree –K-D Trees For.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Catie Baker Spring 2015.
Splay trees Go&Ta How do you organize your world?
CSE332: Data Abstractions Lecture 7: AVL Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
Data Structures and Analysis (COMP 410)
Splay Trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red Black Trees.
Lecture 25 Splay Tree Chapter 10 of textbook
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Topics covered (since exam 1):
Instructor: Lilian de Greef Quarter: Summer 2017
AVL Trees: AVL Trees: Balanced binary search tree
David Kaplan Dept of Computer Science & Engineering Autumn 2001
CPSC 221: Algorithms and Data Structures Lecture #6 Balancing Act
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
SPLAY TREES.
Algorithms and Data Structures Lecture VIII
CSE 332: Data Abstractions AVL Trees
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
326 Lecture 9 Henry Kautz Winter Quarter 2002
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007

2 Administrivia Midterm July 16 during lecture –Topics posted, Review next week Project 2c posted early next week

3 Self adjustment for better living Ordinary binary search trees have no balance conditions –what you get from insertion order is it Balanced trees like AVL trees enforce a balance condition when nodes change –tree is always balanced after an insert or delete Self-adjusting trees get reorganized over time as nodes are accessed

4 Splay Trees Blind adjusting version of AVL trees –Why worry about balances? Just rotate anyway! Amortized time per operations is O(log n) Worst case time per operation is O(n) –But guaranteed to happen rarely Insert/Find always rotate node to the root!

5 Amortized Complexity If a sequence of M operations takes O(M f(n)) time, we say the amortized runtime is O(f(n)). Amortized complexity is worst-case guarantee over sequences of operations. Worst case time per operation can still be large, say O(n) Worst case time for any sequence of M operations is O(M f(n)) Average time per operation for any sequence is O(f(n))

6 Amortized Complexity Is amortized guarantee any weaker than worstcase? Is amortized guarantee any stronger than averagecase? Is average case guarantee good enough in practice? Is amortized guarantee good enough in practice?

7 The Splay Tree Idea If you’re forced to make a really deep access: Since you’re down there anyway, fix up a lot of deep nodes! 3

8 1.Find or insert a node k 2.Splay k to the root using: zig-zag, zig-zig, or plain old zig rotation Why could this be good?? 1.Helps the new root, k oGreat if k is accessed again 2.And helps many others! oGreat if many others on the path are accessed Find/Insert in Splay Trees

9 Splaying node k to the root: Need to be careful! One option (that we won’t use) is to repeatedly use AVL single rotation until k becomes the root: (see Section for details) s A k B C r D q E p F r D q E p F C s A B k

10 Splaying node k to the root: Need to be careful! What’s bad about this process? s A k B C r D q E p F r D q E p F C s A B k

11 Let X be a non-root node with  2 ancestors. P is its parent node. G is its grandparent node. P G X G P X G P X G P X Splay Tree Terminology

12 Zig-Zig and Zig-Zag 4 G5 1P Zig-zag G P5 X2 Zig-zig X Parent and grandparent in same direction. Parent and grandparent in different directions.

13 Zig-Zag operation “Zig-Zag” consists of two rotations of the opposite direction (assume R is the node that was accessed) (RotateFromRight) (RotateFromLeft) ZigZagFromLeft

14 Splay: Zig-Zag * g X p Y k Z W * Just like an… k Y g W p ZX Which nodes improve depth?

15 Zig-Zig operation “Zig-Zig” consists of two single rotations of the same direction (R is the node that was accessed) (RotateFromLeft) ZigZigFromLeft

16 Splay: Zig-Zig * k Z Y p X g W g W X p Y k Z *Is this just two AVL single rotations in a row? Why does this help?

17 Special Case for Root: Zig p X k Y Z root k Z p Y X Relative depth of p, Y, Z?Relative depth of everyone else? Why not drop zig-zig and just zig all the way?

18 Splaying Example: Find(6) Find(6) ? Think of as if created by inserting 6,5,4,3,2,1 – each took constant time – a LOT of savings so far to amortize those bad accesses over

19 Still Splaying ?

20 Finally… ?

21 Another Splay: Find(4) Find(4) ?

22 Example Splayed Out ?

23 Practice Finding Find 2… Then how long would it take to find 6? 4? Will the tree ever look like what we started with again?

24 But Wait… What happened here? Didn’t two find operations take linear time instead of logarithmic? What about the amortized O(log n) guarantee?

25 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 Overall, nodes which are low 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.)

26 Practical Benefit of Splaying No heights to maintain, no imbalance to check for –Less storage per node, easier to code Often data that is accessed once, is soon accessed again! –Splaying does implicit caching by bringing it to the root

27 Splay Operations: Find Find the node in normal BST manner Splay the node to the root –if node not found, splay what would have been its parent What if we didn’t splay?

28 Splay Operations: Insert Insert the node in normal BST manner Splay the node to the root What if we didn’t splay?

29 Example Insert Inserting in order 1,2,3,…,8 Without self-adjustment O(n 2 ) time

30 With Self-Adjustment ZigFromRight

31 With Self-Adjustment ZigFromRight O(n) time!!

32 Splay Operations: Remove find(k) LR k LR > k< k delete k Now what?

33 Example Deletion splay remove splay attach

34 Practice Delete

35 Join Join(L, R): given two trees such that (stuff in L) < (stuff in R), merge them: Splay on the maximum element in L, then attach R LR R L Does this work to join any two trees? splay max

36 Splay Tree Summary All operations are in amortized O(log n) time Splaying can be done top-down; this may be better because: –only one pass –no recursion or parent pointers necessary –we didn’t cover top-down in class Splay trees are very effective search trees –Relatively simple –No extra fields required –Excellent locality properties: frequently accessed keys are cheap to find