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,

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

CS16: Introduction to Data Structures & Algorithms
Splay Trees Binary search trees.
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.
Treaps.  Good (logarithmic) performance with random data  Linear performance if the data is sorted ◦ Solutions – Splay Trees Amortized O( lg n) performance.
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Chapter 4: Trees Part II - AVL Tree
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
AA Trees another 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 balanced.
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.
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 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
CSE 326: Data Structures Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
DART! Time Assigner By: Matt Ivers and Ray Kilway.
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
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
Tree.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
CSC 213 – Large Scale Programming. Implementing Map with a Tree  Accessing root much faster than going to leaves  In real-world, should place important.
CSIT 402 Data Structures II
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
B-Trees and Red Black Trees. Binary Trees B Trees spread data all over – Fine for memory – Bad on disks.
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
Advanced Data Structures and Implementation Top-Down Splay Trees Top-Down Splay Trees Red-Black Trees Red-Black Trees Top-Down Red Black Trees Top-Down.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Search Trees Chapter   . Outline  Binary Search Trees  AVL Trees  Splay 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.
CMSC 341 Amortized Analysis.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
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.
Red-Black Trees Definitions and Bottom-Up Insertion.
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
1 More Trees Trees, Red-Black Trees, B Trees.
Fundamental Data Structures and Algorithms Jeffrey Stylos February 8 th, 2005 Splay Trees Adapted from slides by Peter Lee October 4, 2001.
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)
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
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.
Binary search tree. Removing a node
Data Structures and Analysis (COMP 410)
Splay Trees.
SPLAY TREE Features Binary Search Tree Self adjusting balanced tree
Advanced Associative Structures
B- Trees D. Frey with apologies to Tom Anastasio
SPLAY TREES.
CMSC 341 Splay Trees.
CMSC 341 Splay Trees.
CMSC 341 Lecture 12.
CMSC 341 Splay Trees.
CMSC 341 Splay Trees.
326 Lecture 9 Henry Kautz Winter Quarter 2002
Splay Trees Binary search trees.
Presentation transcript:

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, we run the risk of trees that are essentially lists

8/3/2007 UMBC CMSC 341 SplayTrees 3 BST Sequence of Operations Worst case for a single BST operation is O(N) Not so bad if this happens only occasionally BUT...its not uncommon for an entire sequence of “bad” operations to occur. In this case, a sequence of M operations take O(M * N) time and the time for the sequence of operations becomes noticeable.

8/3/2007 UMBC CMSC 341 SplayTrees 4 Splay Tree Sequence of Operations Splay trees guarantee that a sequence of M operations takes at most O( M * lg N ) time. We say that the splay tree has amortized running time of O( lg N ) cost per operation. Over a long sequence of operations, some may take more than lg N time, some will take less.

8/3/2007 UMBC CMSC 341 SplayTrees 5 Splay Tree Sequence of Operations (cont.) Does not preclude the possibility that any particular operation is still O( N ) in the worst case.  Therefore, amortized O( lg N ) not as good as worst case O( lg N)  But, the effect is the same – there is no “bad” sequence of operations or bad input sequences. If any particular operation is O( N ) and we still want amortized O( lg N ) performance, then whenever a node is accessed, it must be moved. Otherwise its access time is always O( N ).

8/3/2007 UMBC CMSC 341 SplayTrees 6 Splay Trees 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 of tree rotations is knowing as “splaying”. If the node being “splayed” is deep, many nodes on the path to that node are also deep and by restructuring the tree, we make access to all of those nodes cheaper in the future.

8/3/2007 UMBC CMSC 341 SplayTrees 7 Basic “Single” Rotation in a BST Assuming that the tree on the left is a BST, how can we verify that the tree on the right is still a valid BST? Note that the rotation can be performed in either direction. Rotating k 1 around k 2

Under the Hood how rotation really works In the previous slide, rotating k 1 around k 2 is really nothing more than performing these 2 relinking statements: k 2.left = k 1.right; and k 1.right = k 2 ; Now, k 2 is the parent of k 1, and the diagram on the right just shows the nodes in their proper perspective. You should work out the code to do all of the double rotations in the splay tree section (zig-zig, zig-zag) of the text and the slides that follow 8/3/2007 UMBC CMSC 341 SplayTrees 8

8/3/2007 UMBC CMSC 341 SplayTrees 9 Splay Operation To “splay node x”, traverse up the tree from node x to root, rotating along the way until x is the root. For each rotation:  If x is the root, do nothing.  If x has no grandparent, rotate x about its parent.  If x has a grandparent, if x and its parent are both left children or both right children, rotate the parent about the grandparent, then rotate x about its parent. if x and its parent are opposite type children (one left and the other right), rotate x about its parent, then rotate x about its new parent (former grandparent).

8/3/2007 UMBC CMSC 341 SplayTrees 10 Node has no grandparent - Zig

8/3/2007 UMBC CMSC 341 SplayTrees 11 Node and Parent are Same Side (both left/right children) -- Zig-Zig Rotate P around G, then X around P

8/3/2007 UMBC CMSC 341 SplayTrees 12 Node and Parent are Different Sides (one is left, one is right child) -- Zig-Zag Rotate X around P, then X around G

8/3/2007 UMBC CMSC 341 SplayTrees 13 Operations in Splay Trees insert  first insert as in binary search tree  then splay inserted node  if there is a duplicate, the node holding the duplicate element is splayed find/contains  search for node  if found, splay it; otherwise splay last node accessed on the search path

8/3/2007 UMBC CMSC 341 SplayTrees 14 Operations on Splay Trees (cont) remove  splay element to be removed if the element to be deleted is not in the tree, the node last visited on the search path is splayed  disconnect left and right subtrees from root  do one or both of: splay max item in T L (then T L has no right child) splay min item in T R (then T R has no left child)  connect other subtree to empty child of root

8/3/2007 UMBC CMSC 341 SplayTrees 15 Exercise - find( 65 )

8/3/2007 UMBC CMSC 341 SplayTrees 16 Exercise - remove( 25 )

8/3/2007 UMBC CMSC 341 SplayTrees 17 Insertion in order into a Splay Tree In a BST, building a tree from N sorted elements was O( N 2 ). What is the performance of building a splay tree from N sorted elements?

8/3/2007 UMBC CMSC 341 SplayTrees 18 An extreme example of splaying

8/3/2007 UMBC CMSC 341 SplayTrees 19 Splay Tree Code The splaying operation is performed “up the tree” from the node to the root. How do we traverse “up” the tree? How do we know if X and P are both left/right children or are different sided children? How do we know if X has a grandparent? What disadvantages are there to this technique?

8/3/2007 UMBC CMSC 341 SplayTrees 20 Top-Down Splay Trees Rather than write code that traverses both up and down the tree, “top-down” splay trees only traverse down the tree. On the way down, rotations are performed and the tree is split into three parts depending on the access path (zig, zig-zig, zig-zag) taken  X, the node currently being accessed  Left – all nodes less than X  Right – all nodes greater than X As we traverse down the tree, X, Left, and Right are reassembled This method is faster in practice, uses only O( 1 ) extra space and still retains O( lg N ) amortized running time.