15-211 Fundamental Data Structures and Algorithms Jeffrey Stylos February 8 th, 2005 Splay Trees Adapted from slides by Peter Lee October 4, 2001.

Slides:



Advertisements
Similar presentations
CS16: Introduction to Data Structures & Algorithms
Advertisements

Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
Solution of Assignment 3 and Midterm CSC2100B. AVL Tree A binary search tree is a binary tree in which every node has larger key than the nodes in its.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
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.
CS202 - Fundamental Structures of Computer Science II
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
CSE332: Data Abstractions Lecture 7: AVL Trees Tyler Robison Summer
AVL-Trees (Part 1) COMP171. AVL Trees / Slide 2 * Data, a set of elements * Data structure, a structured set of elements, linear, tree, graph, … * Linear:
Tirgul 5 AVL trees.
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Amortized Analysis Some of these lecture slides are adapted from CLRS.
TCSS 342 AVL Trees v1.01 AVL Trees Motivation: we want to guarantee O(log n) running time on the find/insert/remove operations. Idea: keep the tree balanced.
CSC 213 Lecture 7: Binary, AVL, and Splay Trees. Binary Search Trees (§ 9.1) Binary search tree (BST) is a binary tree storing key- value pairs (entries):
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
BST Data Structure A BST node contains: A BST contains
CSE 326: Data Structures AVL Trees
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
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.
Binary search trees Definition Binary search trees and dynamic set operations Balanced binary search trees –Tree rotations –Red-black trees Move to front.
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,
CS 61B Data Structures and Programming Methodology Aug 11, 2008 David Sun.
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.
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
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,
BINARY SEARCH TREE. Binary Trees A binary tree is a tree in which no node can have more than two children. In this case we can keep direct links to the.
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
CMSC420: Splay Trees Kinga Dobolyi Based off notes by Dave Mount.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
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.
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
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.
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.
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.
1 CSE 326: Data Structures Trees Lecture 6: Friday, Jan 17, 2003.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
(c) University of Washington20c-1 CSC 143 Binary Search Trees.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
BSTs, AVL Trees and Heaps Ezgi Shenqi Bran. What to know about Trees? Height of a tree Length of the longest path from root to a leaf Height of an empty.
Week 7 - Friday CS221.
UNIT III TREES.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Monday, April 16, 2018 Announcements… For Today…
AVL Trees CENG 213 Data Structures.
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
AVL Trees CSE 373 Data Structures.
CS202 - Fundamental Structures of Computer Science II
CSC 143 Binary Search Trees.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CSE 373 Data Structures Lecture 8
326 Lecture 9 Henry Kautz Winter Quarter 2002
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

Fundamental Data Structures and Algorithms Jeffrey Stylos February 8 th, 2005 Splay Trees Adapted from slides by Peter Lee October 4, 2001

Today  Review HW2  Review of binary trees  Splay trees

Announcements  HW2 is out!  Due next Thursday at midnight!  Get started now!  Quiz 2 next week, during recitation

Objects in calendar are closer than they appear. — Jim Duncan

And now, a few words about HW2  [Homework 2 write-up]

Let’s talk about BlackBoard  What you always wanted to know about the discussion board (but were afraid to ask)

Last Time…

Binary trees A binary tree is either empty (we'll write nil for clarity), or looks like (x,L,R) where x is an object, and L, R are binary subtrees

Binary search trees (BSTs) A binary tree T is a binary search tree iff flat(T) is an ordered sequence. Equivalently, in (x,L,R) all the nodes in L are less than x, and all the nodes in R are larger than x.

flat(T) = 2,3,4,5,6,7,9 Example

search(x,nil) = false search(x,(x,L,R)) = true search(x,(a,L,R)) = search(x,L) x<a search(x,(a,L,R)) = search(x,R) x>a Binary search How does one search in a BST? Inductively:

Log-time vs linear time  log k (N) = O(N) for any constant k.  I.e, logarithms grow very slowly.

flat(T) = 2,3,4,5,6,7,9 A bad tree

Balanced trees  Intuitively, one way to ensure good behavior is to make “balanced” search trees.  If always balanced, then operations such as search and insert will require only O(log(N)) comparisons.

flat(T) = 2,3,4,5,6,7,9 Example

Forcing good behavior It is clear (?) that for any n inputs, there always is a BST containing these elements of logarithmic depth. But if we just insert the standard way, we may build a very unbalanced, deep tree. Can we somehow force the tree to remain shallow? At low cost?

Today  Review HW2  Review of binary trees  Splay trees

Splay Trees

Binary search trees  Simple binary search trees can have bad behavior for some insertion sequences.  Average case O(log N), worst case O(N).  AVL trees maintain a balance invariant to prevent this bad behavior.  Accomplished via rotations during insert.  Splay trees achieve amortized running time of O(log N).  Accomplished via rotations during find.

Never do today…  If you are a reasonably lazy person…  Consider the problem of serving a six-course dinner.  You could spend 10 minutes washing the dishes right after each course.  Or you could let the dishes sit and spend an hour tomorrow (or the next day) washing them.  Either way, you will spend one hour doing dishes.

A better example  If you are a reasonably smart person…  Consider the problem of getting groceries.  You could spend 30 minutes going to the Giant Eagle every time you run out of something.  Or you could wait until you have run out of 10 items, and then make a single 2- hour trip to the Giant Eagle.  In this case, you would save 3 hours!

Amortized running time  The analysis that allows us to conclude that we spend the same (or less) amount of time over a sequence of operations is called amortized analysis.  If we say that the amortized running time of a sequence of operations is O(f(N)):  Some operations might be more than O(f(N)), other less.  But the average over the entire sequence is O(f(N)).

Splay trees  Splay trees provide a guarantee that any sequence of M operations (starting from an empty tree) will require O(Mlog N) time.  Hence, each operation has amortized cost of O(log N).  It is possible that a single operation requires O(N) time.  But there are no bad sequences of operations on a splay tree.

A basic observation  Let’s suppose that a node requires O(N) time to find.  If and when this happens, we must move it somewhere closer to the root so that if we access it again, it will definitely require less than O(N) time.  If we don’t do this, then O(log N) amortized behavior is not possible.

Danny Sleator  The inventor of splay trees.  Winner of Kannelakis award.  See his splay tree demo at /~sleator /~sleator  (And definitely a procrastinator.)

The basic idea  Every time a node is accessed, move it to the root (“splay” it)  The move can be accomplished by performing “AVL” rotations.  Practical benefits:  In practice, nodes are often accessed multiple times.  AVL rotations make trees more balanced.

Using rotations  Suppose we perform a find operation, which accesses node n.  Splay n, moving it up to the root.  Doing this requires O(d) time, where d is the depth of n. Hence O(N) (worst case)  But a subsequent access of n will require only O(1) time, and the tree will be better balanced, too.

Splaying, case 1  There are four cases to consider.  Case 1: The node is already the root.  Nothing to do.

Splaying, case 2  Case 2: Accessed node’s parent is the root.  Perform a single rotation. Z Y X ZY X

Splaying, cases 3 and 4  The next two cases cover the situation in which the accessed node has a grandparent.

Splaying, case 3  Case 3: Zig-zag (left).  Perform an AVL double rotation. a Z b X Y1Y1 Y2Y2 a Z b XY1Y1 Y2Y2

Splaying, case 4  Case 4: Zig-zig (left).  Special rotation. a Z b Y W X a Z b Y W X

Symmetry  And there are symmetric cases for zig-zag and zig-zig to the right.

Splay tree example Insert {0, 1, 2, 3, 4, 5, 6}, then find zig-zig right

Splay tree example, cont’d zig-zag right

Splaying example, cont’d zig-zag right

Result of splaying  Access of 6 required N nodes visited and modified.  But now accessing 5 requires only N/2 nodes visited and modified.  Will also bring all nodes up to N/4 of root. (Try it!)  And all nodes are shallower.  Every access will tend to improve the tree for future operations

Using splay operations  …to implement “find(x)”:  Splay x  Look at root (compare to x)  …to implement “insert(x)”:  Splay x  Look at root If it exists, do nothing Else, make root the child of x, call x the new root

Using splay operations (cont.)  …to implement “delete(x)”:  Splay x  Look at root (compare to x) If it doesn’t exist, do nothing  Delete root  If either child of root is null make the other child new root  Splay largest key of left-branch (we could also splay smallest of right) Can do this by splaying the non-existing element, because of the way splay works Everything in right-branch tree will be smaller than everything in left-branch tree  New left-branch tree will have empty right-branch (since we splayed the largest value) Set left-branch tree's right-branch to be the right-branch tree

Using splay operations (cont.)  …to implement “delete(x)”:  Splay x  Look at root (compare to x) If it doesn’t exist, do nothing  Delete root  Splay largest b f a c x de

Nice properties of splay trees  If you have a small working set, the operations’ costs are proportional to the size of the working set  Why?  If you splay the elements in order, the total cost is O(N)  Why?

Analysis of splay trees  The analysis of the running time of splay trees is quite difficult.  Any single find or insert might take O(N) time.  But any sequence of M operations, starting from an empty tree, will take only O(Mlog N) time.  In practice, splay trees work extremely well.

Splaying summary  Splaying has the effect of moving the accessed node to the root.  It also reduces the depth of almost all of the nodes along the access path.

All done for today!