Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 25 Splay Tree Chapter 10 of textbook

Similar presentations


Presentation on theme: "Lecture 25 Splay Tree Chapter 10 of textbook"— Presentation transcript:

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

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

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

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

5 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

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

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

8 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

9 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

10 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

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

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

13 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


Download ppt "Lecture 25 Splay Tree Chapter 10 of textbook"

Similar presentations


Ads by Google