CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II.

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 Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Chapter 4: Trees Part II - AVL Tree
D. D. Sleator and R. E. Tarjan | AT&T Bell Laboratories Journal of the ACM | Volume 32 | Issue 3 | Pages | 1985 Presented By: James A. Fowler,
Binary Search Tree AVL Trees and Splay Trees
CS202 - Fundamental Structures of Computer Science II
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.
Data Structures Lecture 11 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
CSE 326: Data Structures Splay Trees Ben Lerner Summer 2007.
© 2004 Goodrich, Tamassia, Dickerson Splay Trees v z.
CS 206 Introduction to Computer Science II 11 / 19 / 2008 Instructor: Michael Eckmann.
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):
1 Theory I Algorithm Design and Analysis (3 - Balanced trees, AVL trees) Prof. Th. Ottmann.
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.
E.G.M. PetrakisTrees in Main Memory1 Balanced BST  Balanced BSTs guarantee O(logN) performance at all times  the height or left and right sub-trees are.
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
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CS 61B Data Structures and Programming Methodology Aug 11, 2008 David Sun.
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,
CSC 213 – Large Scale Programming. Implementing Map with a Tree  Accessing root much faster than going to leaves  In real-world, should place important.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
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.
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.
D. ChristozovCOS 221 Intro to CS II AVL Trees 1 AVL Trees: Balanced BST Binary Search Trees Performance Height Balanced Trees Rotation AVL: insert, delete.
Tree Rotations & Splay Trees. BST Structure BST's only perform well when balanced But common cases lead to unbalanced trees.
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.
Fundamental Data Structures and Algorithms Jeffrey Stylos February 8 th, 2005 Splay Trees Adapted from slides by Peter Lee October 4, 2001.
1Computer Sciences. 2 HEAP SORT TUTORIAL 4 Objective O(n lg n) worst case like merge sort. Sorts in place like insertion sort. A heap can be stored as.
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.
AVL Trees. AVL Tree In computer science, an AVL tree is the first-invented self-balancing binary search tree. In an AVL tree the heights of the two child.
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.
Splay trees Go&Ta How do you organize your world?
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.
AA Trees.
Red-Black Tree Neil Tang 02/04/2010
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
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 TREE Features Binary Search Tree Self adjusting balanced tree
Lecture 25 Splay Tree Chapter 10 of textbook
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Tree Rotations & Splay Trees
Copyright © Aiman Hanna All rights reserved
CS223 Advanced Data Structures and Algorithms
CMSC 341 Splay Trees.
CS202 - Fundamental Structures of Computer Science II
Fundamental Structures of Computer Science II
CMSC 341 Splay Trees.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Splay trees CS 202 – Fundamental Structures of Computer Science II Bilkent University Computer Engineering Department

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 2 Spay Trees So far we have seen: BST: binary search trees  Worst-case running time per operation = O(N)  Worst case average running time = O(N)  Think about inserting a sorted item list AVL tree:  Worst-case running time per operation = O(logN)  Worst case average running time = O(logN) We will now wee a new data structure, called splay trees, for which:  Worst-case running time of one operation = O(N)  Worst case running time of M operations = O(MlogN)  O(logN) amortized running time. A spay tree is a binary search tree.

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 3 Splay trees A splay tree guarantees that, for M consecutive operations, the total running time is O(MlogN). A single operation on a splay tree can take O(N) time.  So the bound is not as strong as O(logN) worst- case bound in AVL trees.

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 4 Amortized running time Definition: For a series of M consecutive operations:  If the total running time is O(M*f(N)), we say that the amortized running time (per operation) is O(f(N)). Using this definition:  A splay tree has O(logN) amortized cost (running time) per operation.

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 5 Splay tree idea: Try to make the worst-case situation occur less frequently. In a Binary search tree, the worst case situation can occur with every operation. (while inserting a sorted item list). In a splay tree, when a worst-case situation occurs for an operation:  The tree is re-structured (during or after the operation), so that the subsequent operations do not cause the worst-case situation to occur again. The basic idea of splay tree is: After a node is accessed, it is pushed to the root by a series of AVL tree-like operations (rotations). For most applications, when a node is access, it is likely that it will be accessed again in the near future.

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 6 Splay tree idea: By pushing the accessed node to the root the tree:  1) If the accessed node is accessed again, the future accesses will be much less costly.  2) During the push to the root operation, the tree might be more balanced than the previous tree.  Accesses to other nodes can also be less costly.

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 7 Splay tree: a bad method for pushing to the root A simple idea, which is not working, is:  When a node K is accessed, push it towards the root by the following algorithm: On the path from k to root:  Do a singe rotation between node k’s parent and node k itself.  Every single rotation will push k to one level higher.  Finally, it will reach to the root.

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 8 Splay tree: a bad method for pushing to the root F k4k4 E D A k1k1 B B k5k5 k3k3 k2k2 access path Accessing node k 1

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 9 F k4k4 E D B k5k5 k3k3 After rotation between k 2 and k 1 A k2k2 Pushing k 1 to the root C k1k1

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 10 F k4k4 E B k5k5 k1k1 After rotation between k 3 and k 1 A Pushing k 1 to the root k2k2 DC k3k3

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 11 F k1k1 B k5k5 After rotation between k 4 and k 1 A Pushing k 1 to the root k2k2 DC E k4k4 k3k3

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 12 k1k1 B After rotation between k 5 and k 1 A Pushing k 1 to the root k2k2 DC F k3k3 k5k5 E k4k4 k 1 is now root But k 3 is nearly as deep as k 1 was. An access to k 3 will push some other node nearly as deep as k 3 is. So, this method does not work!

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 13 Splaying – A method that works Now, we will give a method that works.  It will push the accessed node to the root.  With this pushing operation it will also balance the tree somewhat. So that further operations on the new will be less costly compared to operations that would be done on the original tree. A deep tree will be spayed: Will be less deep, more wide.

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 14 Splaying - algorithm Assume we access a node. We will splay along the path from access node to the root. At every splay step:  We will selectively rotate the tree.  Selective operation will depend on the structure of the tree around the node around which rotation will be performed

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 15 Splaying - algorithm Let X be a non-root node on the access path at which we are rotating.  If parent of X is root, We rotate between X and root. X becomes root. And we are done!  Otherwise, X has a parent P which is non-root. X certainly has also a grand-parent G.  There two cases to consider Zig-zag case: P is left child of G and X is right child of P Zig-zig case: P is left child of G and X is left child of P  We will have also symmetric cases of the two cases above. The operations for these symmetric cases will be very similar to the operations for the two cases above. Symetric cases are: P is right child of G and X is left child of P P is right child of G and X is right child of P.

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 16 Splaying - algorithm Depending on the case that is determined from the interconnection of X, P, and G, we will perform one of the two operations we will describe below:  Zig-zag case: Perform double rotation (an AVL tree operation).  Zig-zig case Transform the tree to an other one as described below.

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 17 Case 1: Zig-zag case operation CB X A P G D P X G A BCD double-rotation between G, P, and X

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 18 Case 2: Zig-zig case operation P G D X C B A X D A P C G B

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 19 A general example: F k4k4 E D A k1k1 B B k5k5 k3k3 k2k2 node k 1 is accessed and it will be pushed to root. X P G This is case 1: Zig-zag case Double rotate Original tree

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 20 A general example: F k4k4 E AB k5k5 k1k1 After double rotation k2k2 CD k3k3 X P G This is case 2: Requires zig-zig case operation

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 21 A general example – final tree k1k1 After zig-zig case operation EF k5k5 k4k4 AB k2k2 CD k3k3 k 1 is root The tree is more balanced than the original tree The depth of the new tree is nearly half of the depth of the original tree New tree

CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University 22 A specific example Assume 7,6,5,4,3,2,1 are inserted into an empty splay tree Splaying at node with item 1 X P G X P G X P G original tree new tree after splaying