Splay Trees Splay trees are binary search trees (BSTs) that:

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
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 Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
Red-Black Trees 4/16/2017 8:38 AM Splay Trees v z Splay Trees.
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.
© 2004 Goodrich, Tamassia, Dickerson Splay Trees v z.
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 Red-Black Trees. 2 Black-Height of the tree = 4.
AVL trees. AVL Trees We have seen that all operations depend on the depth of the tree. We don’t want trees with nodes which have large height This can.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 45 AVL Trees and Splay.
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
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
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.
§4 AVL Trees Target : Speed up searching (with insertion and deletion) Tool : Binary search trees root smalllarge Problem : Although T p = O( height ),
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,
1 Joe Meehean.  BST efficiency relies on height lookup, insert, delete: O(height) a balanced tree has the smallest height  We can balance an unbalanced.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
1 Splay trees (Sleator, Tarjan 1983). 2 Goal Support the same operations as previous search trees.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay Trees.
CMSC420: Splay Trees Kinga Dobolyi Based off notes by Dave Mount.
CS 253: Algorithms Chapter 13 Balanced Binary Search Trees (Balanced BST) AVL 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.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
1 Chapter 7 Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation of the binary search tree.
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.
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.
Fundamental Data Structures and Algorithms Jeffrey Stylos February 8 th, 2005 Splay Trees Adapted from slides by Peter Lee October 4, 2001.
CS 5243: Algorithms Balanced Trees AVL : Adelson-Velskii and Landis(1962)
AVL TREES By Asami Enomoto CS 146 AVL Tree is… named after Adelson-Velskii and Landis the first dynamically balanced trees to be propose Binary search.
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.
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.
G64ADS Advanced Data Structures
AVL Trees binary tree for every node x, define its balance factor
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
Data Structures and Analysis (COMP 410)
Splay Trees.
SPLAY TREE Features Binary Search Tree Self adjusting balanced tree
AVL Tree.
AVL Trees "The voyage of discovery is not in seeking new landscapes but in having new eyes. " - Marcel Proust.
Lecture 25 Splay Tree Chapter 10 of textbook
TCSS 342, Winter 2006 Lecture Notes
Tree Rotations & Splay Trees
SPLAY TREES.
Data Structures & Algorithms
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.
AVL Search Tree put(9)
CMSC 341 Splay Trees.
AVL-Trees (Part 1).
CMSC 341 Splay Trees.
CMSC 341 Splay Trees.
Splay Trees Binary search trees.
Presentation transcript:

Splay Trees Splay trees are binary search trees (BSTs) that: Are not perfectly balanced all the time Allow search and insertion operations to try to balance the tree so that future operations may run faster Based on the heuristic: If X is accessed once, it is likely to be accessed again. After node X is accessed, perform “splaying” operations to bring X up to the root of the tree. Do this in a way that leaves the tree more or less balanced as a whole.

Motivating Example Root 14 Root 12 6 15 3 9 18 After splaying with 12 15 After Search(12) 2 6 18 Splay idea: Get 12 up to the root using rotations 1 3 12 9 14 Initial tree Not only splaying with 12 makes the tree balanced, subsequent accesses for 12 will take O(1) time. Active (recently accessed) nodes will move towards the root and inactive nodes will slowly move further from the root

Splay Tree Terminology Let X be a non-root node, i.e., has at least 1 ancestor. Let P be its parent node. Let G be its grandparent node (if it exists) Consider a path from G to X: Each time we go left, we say that we “zig” Each time we go right, we say that we “zag” There are 6 possible cases: G G G G P P P P P P X X X X X X 1. zig 2. zig-zig 3. zig-zag 4. zag-zig 5. zag-zag 6. zag

Splay Tree Operations When node X is accessed, apply one of six rotation operations: Single Rotations (X has a P but no G) zig, zag Double Rotations (X has both a P and a G) zig-zig, zig-zag zag-zig, zag-zag

Splay Trees: Zig Operation “Zig” is just a single rotation, as in an AVL tree Suppose 6 was the node that was accessed (e.g. using Search) 15 6 Zig-Right 6 18 3 15 3 12 12 18 “Zig-Right” moves 6 to the root. Can access 6 faster next time: O(1) Notice that this is simply a right rotation in AVL tree terminology.

Splay Trees: Zig-Zig Operation “Zig-Zig” consists of two single rotations of the same type Suppose 3 was the node that was accessed (e.g., using Search) 15 Zig-Right 6 3 Zig-Right 1 6 6 18 3 15 4 15 3 12 1 4 12 18 12 18 1 4 Due to “zig-zig” splaying, 3 has bubbled to the top! Note: Parent-Grandparent is rotated first.

Splay Trees: Zig-Zag Operation “Zig-Zag” consists of two rotations of the opposite type Suppose 12 was the node that was accessed (e.g., using Search) 15 Zag-Left 15 Zig-Right 12 6 18 12 18 6 15 3 10 14 18 3 12 6 14 3 10 10 14 Due to “zig-zag” splaying, 12 has bubbled to the top! Notice that this is simply an LR imbalance correction in AVL tree terminology (first a left rotation, then a right rotation)

Splay Trees: Zag-Zig Operation “Zag-Zig” consists of two rotations of the opposite type Suppose 17 was the node that was accessed (e.g., using Search) 15 15 Zag-Left 17 Zig-Right 15 20 6 20 6 17 6 16 18 30 17 30 16 20 18 30 16 18 Due to “zag-zig” splaying, 17 has bubbled to the top! Notice that this is simply an RL imbalance correction in AVL tree terminology (first a right rotation, then a left rotation)

Splay Trees: Zag-Zag Operation “Zag-Zag” consists of two single rotations of the same type Suppose 30 was the node that was accessed (e.g., using Search) 15 Zag-Left 20 Zag-Left 30 15 30 6 20 20 40 6 17 25 40 15 25 17 30 6 17 25 40 Due to “zag-zag” splaying, 30 has bubbled to the top! Note: Parent-Grandparent is rotated first.

Splay Trees: Zag Operation “Zag” is just a single rotation, as in an AVL tree Suppose 15 was the node that was accessed (e.g., using Search) 15 6 Zag-Left 6 18 3 15 12 18 3 12 “Zag-Left”moves 15 to the root. Can access 15 faster next time: O(1) Notice that this is simply a left rotation in AVL tree terminology

Splay Trees: Example – 40 is accessed 80 80 40 70 85 30 70 70 85 60 75 50 80 40 75 50 65 45 60 75 85 30 50 55 65 40 55 45 60 55 65 30 45 After Zig-zig After Zig-zig (a) (b) (c)

Splay Trees: Example – 60 is accessed 40 40 60 30 30 70 60 40 70 70 80 50 50 30 50 65 80 75 85 45 45 60 55 65 80 45 55 75 85 75 85 55 65 After Zig-zag After zag (a) (b) (c)

Do it yourself exercise Insert the keys 1, 2, …, 7 in that order into an empty splay tree. What happens when you access “7”?

Splaying during other operations Splaying can be done not just after Search, but also after other operations such as Insert/Delete. Insert X: After inserting X at a leaf node (as in a regular BST), splay X up to the root Delete X: Do a Search on X and get X up to the root. Delete X at the root and move the largest item in its left sub-tree, i.e, its predecessor, to the root using splaying. Note on Search X: If X was not found, splay the leaf node that the Search ended up with to the root.

Summary of Splay Trees Examples suggest that splaying causes tree to get balanced. The actual analysis is rather advanced and is in Chapter 11. Such analysis is called “amortized analysis” Result of Analysis: Any sequence of M operations on a splay tree of size N takes O(M log N) time. So, the amortized running time for one operation is O(log N). This guarantees that even if the depths of some nodes get very large, you cannot get a long sequence of O(N) searches because each search operation causes a rebalance. Without splaying, total time could be O(MN).