Lecture 25 Splay Tree Chapter 10 of textbook

Slides:



Advertisements
Similar presentations
Rizwan Rehman Centre for Computer Studies Dibrugarh University
Advertisements

Splay Tree Algorithm Mingda Zhao CSC 252 Algorithms Smith College Fall, 2000.
Trees Types and Operations
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.
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.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Splay Trees Splay trees are binary search trees (BSTs) that:
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.
Chapter 7 Trees_Part3 1 SEARCH TREE. Search Trees 2  Two standard search trees:  Binary Search Trees (non-balanced) All items in left sub-tree are less.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
IT 60101: Lecture #121 Foundation of Computing Systems Lecture 12 Trees: Part VII.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
Red-Black Trees an 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.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
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
COMP261 Lecture 23 B Trees.
AA Trees.
File Organization and Processing Week 3
Binary search trees Definition
Lecture Efficient Binary Trees Chapter 10 of textbook
BCA-II Data Structure Using C
G64ADS Advanced Data Structures
Week 7 - Friday CS221.
Red Black Trees
Splay Trees Binary search trees.
Lecture 17 Red-Black Trees
Balanced Trees AVL : Adelson-Velskii and Landis(1962)
Splay Trees.
SPLAY TREE Features Binary Search Tree Self adjusting balanced tree
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
CO4301 – Advanced Games Development Week 10 Red-Black Trees continued
AVL Tree Mohammad Asad Abbasi Lecture 12
AVL Tree.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Splay Trees Binary search trees.
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Monday, April 16, 2018 Announcements… For Today…
Data Structures Balanced Trees CSCI
AVL Tree A Balanced Binary Search Tree
Red-Black Trees 11/13/2018 2:07 AM AVL Trees v z AVL Trees.
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Red-Black Trees Bottom-Up Deletion.
TCSS 342, Winter 2006 Lecture Notes
Topics covered (since exam 1):
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.
AVL Trees: AVL Trees: Balanced binary search tree
Tree Rotations & Splay Trees
v z Chapter 10 AVL Trees Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
Red-Black Trees.
Red Black Trees Top-Down Deletion.
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.
Red-Black Trees 1/16/2019 1:56 PM Splay Trees v z Splay Trees.
Topics covered (since exam 1, excluding PQ):
CMSC 341 Splay Trees.
AVL-Trees.
Richard Anderson Spring 2016
CMSC 341 Splay Trees.
Red-Black Implementation of 2-3 Trees
Red-black tree properties
CMSC 341 Splay Trees.
Red Black Trees Top-Down Deletion.
326 Lecture 9 Henry Kautz Winter Quarter 2002
Splay Trees Binary search trees.
AVL Trees: AVL Trees: Balanced binary search tree
Presentation transcript:

Lecture 25 Splay Tree Chapter 10 of textbook 1. Binary Search Tree (BST) Threaded Tree AVL Tree Red Black Tree Splay Tree

5. Splay Trees A splay tree is a binary search tree with the property that recently accessed elements splaying to root. It is said to be an efficient binary tree because it performs basic operations such as insertion, search and deletion operations in O(log n) amortized time. For many non-uniform sequences of operations, splay trees perform better than other search trees, even when the specific pattern of the sequence is unknown.

Splay Tree When a node in a splay tree is accessed, it is rotated or "splayed" to the root. In a splay tree, search, insert and deletion are first done as BST, then followed by combined splaying to make the relevant node root of the tree Why ? Since the most frequently accessed node is always moved closer to the starting point of the search (or the root node), those nodes are therefore located faster. A simple idea behind it is that if an element is accessed, it is likely that it will be accessed again.

Splaying When we access a node N, splaying is performed on N to move it to the root. To perform a splay operation, certain splay steps are performed where each step moves N closer to the root. Splaying a particular node of interest after every access ensures that the recently accessed nodes are kept closer to the root and the tree remains roughly balanced, so that the desired amortized time bounds can be achieved. Each splay step depends on three factors: Whether N is the left or right child of its parent P Whether P is the root or not, and if not Whether P is the left or right child of its parent G (N’s grandparent)

Splaying operation Move a node up to root by a sequence of left, right rotations Zig- zig operation: (right-right-rotations, or left-left-rotations) G P T4 N T1 T2 T3 T3

Splaying Zig-zag operation: (left-right-rotations, or right-left-rotations) G P T4 T1 N T2 T3

Searching for a Node in a Splay Tree If a particular node N is present in the splay tree then a pointer to the N is returned; otherwise a pointer to the null node is returned. The steps performed to search a node N in a splay tree includes: Search down the root of the splay tree looking for N If the search is successful, and we reach N then splay the tree at N and return a pointer to N If the search is unsuccessful, i.e., the splay tree does not contain N, then we reach the null node. Splay the tree at the last non-null node reached during the search and return a pointer to null.

Searching for a Node in a Splay Tree Search 72. Found on the right subtree, splay the tree make 72 root of the tree. 54 39 63 9 45 27 18 90 72 99

Inserting a Node in a Splay Tree Although the process of inserting a new node N into a splay tree begins in the same way as we insert a node in a binary search tree, but after the insertion, N is made the new root of the splay tree. The steps performed to insert a new node N in a splay tree can be given as: Step 1: Search N in the splay tree. If the search is successful, splay at the node N. Step 2: If the search is unsuccessful, add the new node N in such a way that it replaces the NULL pointer reached during the search by a pointer to a new node N. Splay the tree at N

Inserting a Node in a Splay Tree Consider the splay tree given on the left. Observe the change in structure of the tree when 81 is added to it 54 39 63 9 45 18 90 72 99 54 39 63 9 45 27 18 90 72 99 81 81 63 54 72 39 9 45 27 18 90 99 27

Deleting a Node from a Splay Tree To delete a node N from a splay tree, perform the steps given below. Search for the node N that has to be deleted. If the search is unsuccessful, splay the tree at the last non-null node encountered during search. If the search is successful, and N is not the root node then let P be the parent of N. Replace N by an appropriate descendent of P (as we do in binary search tree). Finally splay the tree at P.

Deleting a Node from a Splay Tree Search for N that has to be deleted. If the search is unsuccessful, splay the tree at the last non-null node encountered during the search. If the search is successful and N is not the root node, then let P be the parent of N. Replace N by an appropriate descendent of P (as we do in binary search tree). Finally splay the tree at P.

Deleting a Node from a Splay Tree Example: delete node 39 from it, the new structure of the tree can be given as shown below. 81 63 90 54 72 99 39 9 45 27 18 17 P 54 18 63 81 72 90 99 9 45 27