SPLAY TREE Features Binary Search Tree Self adjusting balanced tree

Slides:



Advertisements
Similar presentations
AVL Trees binary tree for every node x, define its balance factor
Advertisements

Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
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.
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.
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 Lecture #13 Extendible Hashing and Splay Trees Alon Halevy Spring Quarter 2001.
1 BST Trees A binary search tree is a binary tree in which every node satisfies the following: the key of every node in the left subtree is.
Splay Trees Splay trees are binary search trees (BSTs) that:
David Kaplan Dept of Computer Science & Engineering Autumn 2001
Splay Trees and B-Trees
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
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.
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
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 Trees Chapter 7 Objectives
Lecture 23 Red Black Tree Chapter 10 of textbook
CSE 326: Data Structures Trees
AVL Trees CSE, POSTECH.
COMP261 Lecture 23 B Trees.
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
Lecture Efficient Binary Trees Chapter 10 of textbook
Red-Black Tree Neil Tang 02/04/2010
BCA-II Data Structure Using C
G64ADS Advanced Data Structures
Topics covered (since exam 1):
Splay Trees Binary search trees.
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.
Introduction Applications Balance Factor Rotations Deletion Example
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
Data Structures and Analysis (COMP 410)
Splay Trees.
AVL Tree.
Section 8.1 Trees.
Splay Trees Binary search trees.
Red Black Trees.
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
Data Structures Using C++ 2E
TCSS 342, Winter 2006 Lecture Notes
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
CSCI 104 Splay Trees Mark Redekopp.
Topics covered (since exam 1):
Tree Rotations & Splay Trees
CSE 373, Copyright S. Tanimoto, 2002 Binary Search Trees -
SPLAY TREES.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Data Structures & Algorithms
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.
CMSC 341 Splay Trees.
AVL Search Tree put(9)
Topics covered (since exam 1, excluding PQ):
CMSC 341 Splay Trees.
AVL Tree By Rajanikanth B.
AVL-Trees.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
CSE 326: Data Structures Splay Trees
CMSC 341 Splay Trees.
Topics covered (since exam 1):
Red-black tree properties
CMSC 341 Splay Trees.
CSE 326: Data Structures Lecture #10 Amazingly Vexing Letters
326 Lecture 9 Henry Kautz Winter Quarter 2002
Splay Trees Binary search trees.
Topic 10 Trees.
Presentation transcript:

SPLAY TREE Features Binary Search Tree Self adjusting balanced tree Most recently accessed item is always at the top of the tree Support all the operations of Binary Tree Uses rotations to attempt balance Provides fast access to recently used keys A single operation on a splay tree can take O(N) time. Not balanced at all time

BASIC IDEA OF SPLAY TREE 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 accessed, it is likely that it will be accessed again in the near future By pushing the accessed node to the root the tree: If the accessed node is accessed again, the future accesses will be much less costly. 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.

EXAMPLE Accessing node k1 When a node k is accessed, push it towards the root Do a singe rotation between node k’s parent and node k itself k5 k4 F k3 E k2 D k1 A B B access path

EXAMPLE But k3 is nearly as deep as k1 was. An access to k3 will push some other node nearly as deep as k3 is. So, this method does not work ... k1 k1 is now root k2 k5 k4 A B F k3 E C D

SPLAYING METHOD Restructuring the tree by applying rotations The method 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 splayed: Will be less deep, more wide.

Node (N) being accessed is: SPLAYING PROCEDURE Node (N) being accessed is: Root Child of root Has both parent (p) and grandparent (g) Zig-zig pattern: G  P  N is left-left or right-right (Fig A) Zig-zag pattern: G  P  N is left-right or right-left (Fig B) Fig A Fig B G G G G P P P P N N N N

SPLAYING PROCEDURE root root X Y X Y Case 1: N is a Root Node Steps: do nothing root root N N X Y X Y

SPLAYING PROCEDURE root root P Z X P X Y Y Z Case 2: N is a either a left or right child of Root steps: Apply single AVL rotation (Zig) root root P N N Z X P In the case where we’re just one step away from the root, we single rotate. Now, each of these helped and hurt an equal number of nodes at each step, but other than this final zig, our node always helped its children, right? Therefore, every single node except p, Y, and Z in this picture improves (some quite a lot). How bad are things for p, Y, and Z? each might be down one level from where it used to be. How good are things for n and many of its descendants and former ancestors? Very good. Why not zig all the way (no zig-zig)? This just helps _one_ child of n! X Y Y Z Splay Trees CSE 326 Autumn 2001 8

SPLAYING PROCEDURE G W P P Z X G Y Y Z W X Case 3a: N has both P & G, N is LL or RR child of G Steps: Apply Zig-zig rotation (LL /RR rotation) top down manner G N 1 W P P Z 2 X N G Y Can anyone tell me how to implement this with two rotations? There are two possibilities: Start with rotate n or rotate p? Rotate p! Rotate n makes p n’s left child and then we’re hosed. Then, rotate n. This helps all the nodes in blue and hurts the ones in red. So, in some sense, it helps and hurts the same number of nodes on one rotation. Question: what if we keep rotating? What happens to this whole subtree? It gets helped! Y Z W X Splay Trees CSE 326 Autumn 2001 9

SPLAYING PROCEDURE G X P G P W X Y Z W Y Z Case 3b: N has both P & G, N is either LR or RL child of G Steps: Apply AVL Double rotation (Zig-Zag) – Either LR or RL rotation G N X P G P N W X Y Z W Y Z Splay Trees CSE 326 Autumn 2001 10

OPERATIONS Insert(x) : Normal BST insert, then splay x Find(x) : Attempt normal BST find(x) and splay last node visited • If x is in the tree, then we splay x • If x is not in the tree we splay the leaf node where our search ended FindMin(), FindMax() Walk to far left or right of tree, return that node's value and then splay that node DeleteMin(), DeleteMax() Perform FindMin(), FindMax() [which splays the min/max to the root] then delete that node and set root to be the non-NULL child of the min/max • Remove(x) Find(x) splaying it to the top, then overwrite its value with is successor/predecessor, deleting the successor/predecessor node