Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

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.
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.
1 CSE 373 AVL trees, continued read: Weiss Ch. 4, section slides created by Marty Stepp
EECS 311: Chapter 4 Notes Chris Riesbeck EECS Northwestern.
Red-Black Trees 4/16/2017 8:38 AM Splay Trees v z Splay Trees.
CPSC 320: Intermediate Algorithm Design & Analysis Splay Trees (for Amortized Analysis) Steve Wolfman 1.
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:
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):
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
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 B-Trees Ben Lerner Summer 2007.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
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:
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Splay Trees and B-Trees
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
CSE373: Data Structures & Algorithms Optional Slides: AVL Delete Dan Grossman Fall 2013.
1 AVL-Trees: Motivation Recall our discussion on BSTs –The height of a BST depends on the order of insertion E.g., Insert keys 1, 2, 3, 4, 5, 6, 7 into.
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,
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
Balanced Search Trees Fundamental Data Structures and Algorithms Margaret Reid-Miller 3 February 2005.
B-Trees and Red Black Trees. Binary Trees B Trees spread data all over – Fine for memory – Bad on disks.
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.
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 
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 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.
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.
AVL Trees 1. Balancing a BST Goal – Keep the height small – For any node, left and right sub-tree have approximately the same height Ensures fast (O(lgn))
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
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.
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
CSE 326: Data Structures Trees
AVL Trees CSE, POSTECH.
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Splay Trees Binary search trees.
AVL Trees A BST in which, for any node, the number of levels in its two subtrees differ by at most 1 The height of an empty tree is -1. If this relationship.
CS 201 Data Structures and Algorithms
Data Structures and Analysis (COMP 410)
Splay Trees.
SPLAY TREE Features Binary Search Tree Self adjusting balanced tree
Splay Trees Binary search trees.
Lecture 25 Splay Tree Chapter 10 of textbook
AVL Search Tree put(9)
CMSC 341 Splay Trees.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CMSC 341 Splay Trees.
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
326 Lecture 9 Henry Kautz Winter Quarter 2002
Splay Trees Binary search trees.
Presentation transcript:

Splay Trees CSIT 402 Data Structures II

Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete code – Red-Black Complex coding Solution: splay trees – blind adjusting version of AVL trees – amortized time for all operations is O(log n) – worst case time is O(n) – Different idea: Trade a little balance for faster access of most recently searched for/inserted items insert/find always rotates node to the root! 2

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

Splaying Cases Node being accessed (n) is: – Root – Child of root – Has both parent (p) and grandparent (g) Zig-zig pattern: g  p  n is left-left or right-right Zig-zag pattern: g  p  n is left-right or right-left 4

Access root: Do nothing (that was easy!) X n Y root X n Y 5

Access child of root: Zig (AVL single rotation) p X n Y Z n Z p Y X root 6

Access (LR, RL) grandchild: Zig-Zag (AVL double rotation) g X p Y n Z W n Y g W p ZX 7

Access (LL, RR) grandchild: Zig-Zig n Z Y p X g W g W X p Y n Z Rotate top-down – why? 8

Splaying Example: Find(6) Find(6) zig-zig 9

… still splaying … zig-zig

… 6 splayed out! zig

Find (4) zig-zag

… 4 splayed out! zig-zag

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.) 14

Splay Operations: Find Find the node in normal BST manner Splay the node to the root 15

Splay Operations: Insert Ideas? Can we just do BST insert? 16

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 =  ) 17

Splitting in Splay Trees How can we split? – 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? 18

Split split(x) TLR splay OR LRLR  x  x > x< x 19

Back to Insert void insert(Node *& root, Object x) { Node * left, * right; split(root, left, right, x); root = new Node(x, left, right); } split(x) LR x LR > x< x 20

Splay Operations: Delete find(x) LR x LR > x< x delete x Now what? 21

Join Join(L, R): given two trees such that L < R, merge them Splay on the maximum element in L, then attach R LR R splay L 22

Delete Completed T find(x) LR x LR > x< x delete x T - x Join(L,R) 23

Insert Example Insert(5) split(5)

Delete Example Delete(4) find(4) Find max

Splay Tree Summary Can be shown that any M consecutive operations starting from an empty tree take at most O(M log(N))  All splay tree operations run in amortized O(log n) time O(N) operations can occur, but splaying makes them infrequent Implements most-recently used (MRU) logic – Splay tree structure is self-tuning 26

Splay Tree Summary (cont.) 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 (near top of tree) infrequently accessed keys stay out of the way (near bottom of tree) 27