Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.

Slides:



Advertisements
Similar presentations
Splay Trees Binary search trees.
Advertisements

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.
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
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.
1 AVL Trees (10.2) CSE 2011 Winter April 2015.
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Trees Types and Operations
S. Sudarshan Based partly on material from Fawzi Emad & Chau-Wen Tseng
AVL Tree Smt Genap Outline AVL Tree ◦ Definition ◦ Properties ◦ Operations Smt Genap
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.
Red-Black Trees 4/16/2017 8:38 AM Splay Trees v z Splay Trees.
© 2004 Goodrich, Tamassia, Dickerson Splay Trees v z.
© 2004 Goodrich, Tamassia Binary Search Trees   
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 /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.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
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.
Chapter 4: Trees Binary Search Trees
AVL Trees v z. 2 AVL Tree Definition AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the.
2-3 Trees Professor Sin-Min Lee. Contents n Introduction n The 2-3 Trees Rules n The Advantage of 2-3 Trees n Searching For an Item in a 2-3 Tree n Inserting.
Binary Search Trees Chapter 7 Objectives
Advanced Data Structures and Algorithms COSC-600 Lecture presentation-6.
By : Budi Arifitama Pertemuan ke Objectives Upon completion you will be able to: Create and implement binary search trees Understand the operation.
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,
AVL Trees Amanuel Lemma CS252 Algoithms Dec. 14, 2000.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
Search Trees. Binary Search Tree (§10.1) A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying.
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.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
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.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
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.
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.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Question 4 Tutorial 8. Part A Insert 20, 10, 15, 5,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree
Binary Search Trees Chapter 7 Objectives
Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000
AA Trees.
G64ADS Advanced Data Structures
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
Binary search tree. Removing a node
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
AVL Tree.
Lecture 25 Splay Tree Chapter 10 of textbook
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
TCSS 342, Winter 2006 Lecture Notes
AVL Trees CENG 213 Data Structures.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees 11/26/2018 3:42 PM AVL Trees v z AVL Trees.
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
Lecture No.20 Data Structures Dr. Sohail Aslam
CMSC 341 Splay Trees.
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees 2/24/ :17 AM AVL Trees v z AVL Trees.
AVL-Trees (Part 1).
Chapter 20: Binary Trees.
CMSC 341 Splay Trees.
Red-black tree properties
CMSC 341 Splay Trees.
Splay Trees Binary search trees.
CS202 - Fundamental Structures of Computer Science II
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000

Contents  What is Splay Tree?  Splaying: zig-zig; zig-zag; zig.  Rules: search; insertion; deletion.  Complexity analysis.  Implementation.  Demos  Conclusion  References

What is Splay Tree?  A balanced search tree data structure  NIST Definition: A binary search tree in which operations that access nodes restructure the tree.  Goodrich: A splay tree is a binary search tree T. The only tool used to maintain balance in T is the splaying step done after every search, insertion, and deletion in T.  Kingston: A binary tee with splaying.

Splaying  Left and right rotation  Move-to-root operation  Zig-zig  Zig-zag  Zig  Comparing move-to-root and splaying  Example of splaying a node

Left and right rotation Adel’son-Vel’skii and Landis (1962), Kingston Left rotation: Y X X Y T1T2 T3 T2 T1

Right Rotation: X Y Y X T1T2 T3 T2 T1

Move-to-root operation (x=3) Allen and Munro (1978), Bitner(1979), Kingston

Move-to-root operation (x=3)

Zig-zig splaying  The node x and its parent y are both left children or both right children.  We replace z by x, making y a child of x and z a child of y, while maintaining the inorder relationships of the nodes in tree T.  Example: x is 30, y is 20, z is 10

Before splaying: T1 T3 T2 T4

After splaying: T1 T3 T2 T4

Zig-zag splaying  One of x and y is a left child and the other is a right child.  We replace z by x and make x have nodes y and z as its children, while maintaining the inorder relationships of the nodes in tree T.  Example: z is 10, y is 30, x is 20

Before splaying: T1 T3 T2 T4

After splaying: T1 T3 T2T4

Zig splaying  X doesn’t have a grand parent(or the grandparent is not considered)  We rotate x over y, making x’s children be the node y and one of x’s former children u, so as to maintain the relative inorder relationships of the nodes in tree T.  Example: x is 20, y is 10, u is 30

Before splaying: T1 T3 T2 T4

After splaying: T1 T3 T2T4

Move-to-root vs. splaying  Move-to-root should improve the performance of the BST when there is locality of references in the operation sequence, but it is not ideal. The example we use before, the result is not balanced even the original tree was.  Splaying also moves the subtrees of node x(which is being splayed) up 1 level, but move-to-root usually leaves one subtree at its original level.  Example:

Example1:original tree T1 T3 T2 T4

Example1:move-to-root T1 T3T2 T4

Example1:splaying T1 T3 T2 T4

Example2:original tree

Example2: move-to-root

Example2:splaying

Splaying a node: original tree

Splaying a node: cont

Splaying a node: cont

Rules of splaying: Search  When search for key I, if I is found at node x, we splay x.  If not successful, we splay the parent of the external node at which the search terminates unsuccessfully.(null node)  Example: see above slides, it can be for successfully searched I=35 or unsuccessfully searched say I=38.

Rules of splaying: insertion  When inserting a key I, we splay the newly created internal node where I was inserted.  Example:

10 Original tree

10 After insert key 15 15

10 After splaying 15

10 After insert key

10 After splaying 15 12

Rules of splaying: deletion  When deleting a key I, we splay the parent of the node x that gets removed. x is either the node storing I or one of its descendents(root, etc.).  If deleting from root, we move the key of the right-most node in the left subtree of root and delete that node and splay the parent. Etc.  Example:

Original tree

After delete key 30(root)

We are going to splay

After splaying

Complexity  Worst case: O(n), all nodes are on one side of the subtree. (In fact, it is  (n).)  Amortized Analysis: We will only consider the splaying time, since the time for perform search, insertion or deletion is proportional to the time for the splaying they are associated with.

Amortized analysis  Let n(v) = the number of nodes in the subtree rooted at v  Let r(v) = log(n(v)), rank.  Let r’(v) be the rank of node v after splaying.  If a>0, b>0, and c>a+b, then log a + log b <= 2 log c –2  *Search the key takes d time, d = depth of the node before splaying.

Before splaying: Z Y X T1 T3 T2 T4

After splaying: Z Y X T1 T3 T2 T4

Cont.  Zig-zig: variation of r(T) caused by a single splaying substep is: r’(x)+r’(y)+r’(z)-r(x)-r(y)-r(z) =r’(y)+r’(z)-r(x)-r(y) (r’(x)=r(z)) =r(x)) Also n(x)+n’(z)<=n’(x) We have r(x) + r’(z)<=2r’(x)-2(see previous slide)  r’(z)<=2r’(x)-r(x)-2 so we have variation of r(T) by a single splaying step is: <=3(r’(x)-r(x))-2 Since zig-zig takes 2 rotations, the amortized complexity will be 3(r’(x)-r(x))

Before splaying: Z X Y T1 T3 T2 T4

After splaying: Z X Y T1 T3 T2T4

Cont.  Zig-zag: variation of r(T) caused by a single splaying substep is: r’(x)+r’(y)+r’(z)-r(x)-r(y)-r(z) =r’(y)+r’(z)-r(x)-r(y) (r’(x)=r(z)) =r(x)) Also n’(y)+n’(z)<=n’(x) We have r’(y)+r’(z)<=2r’(x)-2 So we have variation of r(T0 by a single splaying substep is: <=2(r’(x)-r(x))-2 <=3(r’(x)-r(x))-2 Since zig-zag takes 2 rotations, the amortized complexity will be 3(r’(x)-r(x))

Before splaying: Y X Z T1 T3 T2 T4

After splaying: Y X Z T1 T3 T2T4

Cont.  Zig: variation of r(T) caused by a single splaying substep is: r’(x)+r’(y)-r(x)-r(y) =r(x)) <=3(r’(x)-r(x)) Since zig only takes 1 rotation, so the amortized complexity will be 3(r’(x)-r(x))+1

Cont.  Splaying node x consists of d/2 splaying substeps, recall d is the depth of x.  The total amortized complexity will be: <=  (3(r i (x)-r i-1 (x))) + 1(1<=i<=d/2) recall: the last step is a zig. =3(r d/2 (x)-r 0 (x)) +1 <=3(r(t)-r(x))+1(r(t): rank of root)

Cont.  So from before we have: 3(r(t)-r(x))+1 <=3r(t)+1 =3log n +1 thus, splaying takes O(log n).  So for m operations of search, insertion and deletion, we have O(mlog n)  This is better than the O(mn) worst-case complexity of BST

Implementation  LEDA ?  Java implementation: SplayTree.java and etc from previous files(?) Modified printTree() to track level and child identity, add a small testing part.

Demos  A Demo written in Perl, it has some small bugs bin/splay/splay-cgi.plhttp://bobo.link.cs.cmu.edu/cgi- bin/splay/splay-cgi.pl  An animation java applet: splay/splay.html splay/splay.html

Conclusion  A balanced binary search tree.  Doesn’t need any extra information to be stored in the node, ie color, level, etc.  Balanced in an amortized sense.  Running time is O(mlog n) for m operations  Can be adapted to the ways in which items are being accessed in a dictionary to achieve faster running times for the frequently accessed items.(O(1), AVL is about O(log n), etc.)

References  Data Structure and Algorithms in Java, Michael T. GoodRich.  Algorithms and Data Structures, Jeffrey H Kingston   NIST  Data Strucrues and Problem Solving with C++, Frank M. Carrano. Source code: 