CMSC 341 Splay Trees.

Slides:



Advertisements
Similar presentations
Definitions and Bottom-Up Insertion
Advertisements

Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
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.
Splay Trees CSIT 402 Data Structures II. Motivation Problems with other balanced trees – AVL: extra storage/complexity for height fields Periulous delete.
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.
Trees and Red-Black Trees Gordon College Prof. Brinton.
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.
Tree.
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,
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
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.
Binary Tree. Some Terminologies Short review on binary tree Tree traversals Binary Search Tree (BST)‏ Questions.
Red-Black Trees Definitions and Bottom-Up Insertion.
1 More Trees Trees, Red-Black Trees, B Trees.
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.
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.
AVL Trees CSE, POSTECH.
Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000
Definitions and Bottom-Up Insertion
AA Trees.
G64ADS Advanced Data Structures
Week 7 - Friday CS221.
Search Trees.
Binary search tree. Removing a node
Splay Trees Binary search trees.
Binary Search Tree (BST)
Balanced Binary Search Trees
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
CMSC 341 Lecture 13 Leftist Heaps
Splay Trees Binary search trees.
Red Black Trees.
Lecture 25 Splay Tree Chapter 10 of textbook
Red-Black Trees Bottom-Up Deletion.
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
Advanced Associative Structures
CSCI 104 Splay Trees Mark Redekopp.
Tree Rotations & Splay Trees
B- Trees D. Frey with apologies to Tom Anastasio
B- Trees D. Frey with apologies to Tom Anastasio
Red-Black Trees.
Red Black Trees Top-Down Deletion.
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.
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
2-3-4 Trees Red-Black Trees
B- Trees D. Frey with apologies to Tom Anastasio
CMSC 341 Splay Trees.
Podcast Ch18a Title: Overview of Binary Search Trees
Richard Anderson Spring 2016
CSE 326: Data Structures Splay Trees
CMSC 341 Lecture 12.
CMSC 341 Splay Trees.
CMSC 341 Splay Trees.
Red-Black Tree Rotations
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
326 Lecture 9 Henry Kautz Winter Quarter 2002
Splay Trees Binary search trees.
Presentation transcript:

CMSC 341 Splay Trees

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 21 12 20 15 32 24 37 40 55 56 77 8/3/2007 UMBC CMSC 341 SplayTrees

BST Sequence of Operations Worst case for a single BST operation can be 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

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

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

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

Basic “Single” Rotation in a BST Rotating k1 around k2 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. 8/3/2007 UMBC CMSC 341 SplayTrees

Under the Hood how rotation really works In the previous slide, rotating k1 around k2 is really nothing more than performing these 2 relinking statements: K2.left = k1.right; and k1.right = k2; Now, k2 is the parent of k1, 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). Instructor note: draw the diagram and mark the links that are changed. 8/3/2007 UMBC CMSC 341 SplayTrees

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 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

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

Node and Parent are Same Side Zig-Zig Rotate P around G, then X around P 8/3/2007 UMBC CMSC 341 SplayTrees

Node and Parent are Different Sides Zig-Zag Rotate X around P, then X around G 8/3/2007 UMBC CMSC 341 SplayTrees

Operations in Splay Trees insert first insert as in normal 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

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 TL (then TL has no right child) splay min item in TR (then TR has no left child) connect other subtree to empty child of root 8/3/2007 UMBC CMSC 341 SplayTrees

Exercise - find( 65 ) 50 40 60 20 43 70 65 16 66 63 8/3/2007 UMBC CMSC 341 SplayTrees

Exercise - remove( 25 ) 50 40 60 20 43 70 65 16 25 66 63 8/3/2007 UMBC CMSC 341 SplayTrees

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

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

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 children? How do we know if X has a grandparent? What disadvantages are there to this technique? 8/3/2007 UMBC CMSC 341 SplayTrees

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. 8/3/2007 UMBC CMSC 341 SplayTrees