Splay Trees.

Slides:



Advertisements
Similar presentations
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Advertisements

Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
CPSC 320: Intermediate Algorithm Design & Analysis Splay Trees (for Amortized Analysis) Steve Wolfman 1.
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:
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
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):
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
AVL Trees / Slide 1 Balanced Binary Search Tree  Worst case height of binary search tree: N-1  Insertion, deletion can be O(N) in the worst case  We.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
Splay Trees Splay trees are binary search trees (BSTs) that:
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Splay Trees and B-Trees
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,
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
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.
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 326: Data Structures Lecture #11 AVL and Splay Trees Steve Wolfman Winter Quarter 2000.
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.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
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.
SPLAY TREE The basic idea of the splay tree is that every time a node is accessed, it is pushed to the root by a series of tree rotations. This series.
CSE332: Data Abstractions Lecture 7: AVL Trees
CSE 326: Data Structures Trees
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
File Organization and Processing Week 3
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Topics covered (since exam 1):
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
Data Structures and Analysis (COMP 410)
SPLAY TREE Features Binary Search Tree Self adjusting balanced tree
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
Red Black Trees.
Lecture 25 Splay Tree Chapter 10 of textbook
TCSS 342, Winter 2006 Lecture Notes
Topics covered (since exam 1):
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CSCI 104 Splay Trees Mark Redekopp.
Topics covered (since exam 1):
Tree Rotations & Splay Trees
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Splay Trees In balanced tree schemes, explicit rules are followed to ensure balance. In splay trees, there are no such rules. Search, insert, and delete.
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
SPLAY TREES.
Tree Rotations and AVL Trees
Balanced BSTs "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust CLRS, pages 333, 337.
CMSC 341 Splay Trees.
Balanced Binary Search Trees
AVL Trees CSE 373 Data Structures.
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
Topics covered (since exam 1, excluding PQ):
CMSC 341 Splay Trees.
AVL-Trees.
AVL-Trees (Part 1).
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CSE 326: Data Structures Lecture #9 AVL II
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CMSC 341 Splay Trees.
Topics covered (since exam 1):
CSE 373 Data Structures Lecture 8
CMSC 341 Splay Trees.
1 Lecture 13 CS2013.
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
Trees in Data Structures
326 Lecture 9 Henry Kautz Winter Quarter 2002
Splay Trees Binary search trees.
Presentation transcript:

Splay Trees

Motivation for Splay Trees Problems with AVL Trees extra storage/complexity for height fields complicated code Solution: splay trees blind adjusting version of AVL trees amortized time for all operations is O(log n) worst case time is O(n) insert/find always rotates node to the root!

Splay Tree Idea 10 You’re forced to make 17 a really deep access: Since you’re down there anyway, fix up a lot of deep nodes! Doorbell rings – clean up on the way to answer the door. 5 2 9 3

Self adjusting Trees Ordinary binary search trees have no balance conditions 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 Tree adjusts after insert, delete, or find

Splay Trees Splay trees are tree structures that: The procedure: Are not perfectly balanced all the time Data most recently accessed is near the root. (principle of locality; 80-20 “rule” – 80% of the accesses are to 20% of the data) The procedure: After node X is accessed, perform “splaying” operations to bring X to the root of the tree. Reorganize on the way out of the recursion Do this in a way that creates a more balanced as a whole

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

Zig-Zig and Zig-Zag Zig-zig 4 G G 5 P 5 Zig-zag 1 P X 2 X Parent and grandparent in same direction. Parent and grandparent in different directions. Zig-zig 4 G G 5 P 5 Zig-zag 1 P X 2 X

Splay Tree Operations 1. Helpful if nodes contain a parent pointer. element left right 2. When X is accessed, apply one of six rotation routines. Single Rotations (X has a P (the root) but no G) ZigFromLeft, ZigFromRight Double Rotations (X has both a P and a G) ZigZigFromLeft, ZigZigFromRight ZigZagFromLeft, ZigZagFromRight

Zig at depth 1 (no grandparent) “Zig” is just a single rotation, as in an AVL tree Let R be the node that was accessed (e.g. using Find) moves R to the top faster access next time root Right Rotation

Zig at depth 1 Suppose Q is now accessed using Find moves Q back to the top root Left rotation

Zig-Zag operation “Zig-Zag” consists of two rotations of the opposite direction (assume R is the node that was accessed) - same as for AVL double rotation. (RightRotation) (LeftRotation) Double right rotation

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

Zig-Zig operation I like to visualize it as follows (ZigFromLeft) RightZigZig

Decreasing depth - "autobalance" Find(T) Find(R)

Splay Tree Insert and Delete Insert x Insert x as normal then splay x to root. (Starting at x, look two levels up and do proper rotation. Continue until x is at 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 subtree. Your book doesn’t talk about deletion. There are LOTS of ways it could be done. We will take this method as “the way” to do it for this class.

Example Insert Inserting in order 1,2,3,…,8 Without self-adjustment 1 1+2+3+4+5+6+7+8 operations O(n2) time for n Insert 2 3 4 5 6 7 8

With Self-Adjustment 1 2 3 1 1 2 2 1 3 2 2 1 3 1 ZigFromRight

With Self-Adjustment 3 4 4 2 4 ZigFromRight 3 1 2 1 Each Insert takes O(1) time therefore O(n) time for n inserts!! Of course, a find of node 1 takes O(n) access, but the tree will be restructured on the way out of the recursion.

Example Deletion 10 splay 8 5 15 5 10 13 20 15 2 8 2 6 9 13 20 6 9 (Zig-Zag) 8 5 15 5 10 13 20 15 2 8 2 6 9 13 20 6 9 Splay on max of left tree attach remove 6 5 10 5 10 15 15 2 6 9 2 9 13 20 13 20

Analysis of Splay Trees Splay trees tend to be balanced m operations takes time O(m log n) for m > n operations on n items. (proof is difficult) Amortized O(log n) time. Webster: Amortize: gradually write off the initial cost of (an asset). Splay trees have good “locality” properties Recently accessed items are near the root of the tree. Items near an accessed one are pulled toward the root.