130 likes | 162 Views
Learn about splay trees - binary search trees where accessed nodes move to root, balancing the structure for faster operations like insertion, search, and deletion in O(log n) time. Discover how splaying maintains efficiency even with unknown operation sequences.
E N D
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 N G P T1 P P T4 N G T2 T3 G N T1 T2 T4 T3 T4 T1 T2 • Move a node up to root by a sequence of left, right rotations • Zig- zig operation: (right-right-rotations, or left-left-rotations) T3
Splaying N G G P G P T4 N T4 T1 T2 T3 T3 T4 T1 N P T2 T3 T1 T2 • Zig-zag operation: (left-right-rotations, or right-left-rotations)
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 72 54 54 90 39 63 39 63 99 9 45 90 9 45 27 72 99 27 18 18 • Search 72. Found on the right subtree, splay the tree make 72 root of the tree.
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 54 39 63 9 45 90 72 99 81 18 54 90 63 39 63 54 72 99 9 45 90 39 27 72 99 9 45 18 81 27 18 • Consider the splay tree given on the left. • Observe the change in structure of the tree when 81 is added to it 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 81 81 63 90 63 90 54 72 99 P 54 72 99 39 18 9 45 9 45 27 17 54 18 18 63 81 9 45 72 90 27 99 Example: delete node 39 from it, the new structure of the tree can be given as shown below.