0% found this document useful (0 votes)
37 views12 pages

Introduction To Splay Tree Data Structure

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views12 pages

Introduction To Splay Tree Data Structure

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to Splay tree data structure

Splay tree is a self-adjusting binary search tree data structure, which means that the tree structure
is adjusted dynamically based on the accessed or inserted elements. In other words, the tree
automatically reorganizes itself so that frequently accessed or inserted elements become closer to
the root node.

1. The splay tree was first introduced by Daniel Dominic Sleator and Robert Endre Tarjan in
1985. It has a simple and efficient implementation that allows it to perform search,
insertion, and deletion operations in O(log n) amortized time complexity, where n is the
number of elements in the tree.

2. The basic idea behind splay trees is to bring the most recently accessed or inserted
element to the root of the tree by performing a sequence of tree rotations, called splaying.
Splaying is a process of restructuring the tree by making the most recently accessed or
inserted element the new root and gradually moving the remaining nodes closer to the
root.

3. Splay trees are highly efficient in practice due to their self-adjusting nature, which reduces
the overall access time for frequently accessed elements. This makes them a good choice
for applications that require fast and dynamic data structures, such as caching systems,
data compression, and network routing algorithms.

4. However, the main disadvantage of splay trees is that they do not guarantee a balanced
tree structure, which may lead to performance degradation in worst-case scenarios. Also,
splay trees are not suitable for applications that require guaranteed worst-case
performance, such as real-time systems or safety-critical systems.

Overall, splay trees are a powerful and versatile data structure that offers fast and efficient access
to frequently accessed or inserted elements. They are widely used in various applications and
provide an excellent tradeoff between performance and simplicity.

A splay tree is a self-balancing binary search tree, designed for efficient access to data elements
based on their key values.

 The key feature of a splay tree is that each time an element is accessed, it is moved to the
root of the tree, creating a more balanced structure for subsequent accesses.

 Splay trees are characterized by their use of rotations, which are local transformations of
the tree that change its shape but preserve the order of the elements.

 Rotations are used to bring the accessed element to the root of the tree, and also to
rebalance the tree if it becomes unbalanced after multiple accesses.

Operations in a splay tree:

 Insertion: To insert a new element into the tree, start by performing a regular binary
search tree insertion. Then, apply rotations to bring the newly inserted element to the root
of the tree.

 Deletion: To delete an element from the tree, first locate it using a binary search tree
search. Then, if the element has no children, simply remove it. If it has one child, promote
that child to its position in the tree. If it has two children, find the successor of the element
(the smallest element in its right subtree), swap its key with the element to be deleted,
and delete the successor instead.

 Search: To search for an element in the tree, start by performing a binary search tree
search. If the element is found, apply rotations to bring it to the root of the tree. If it is not
found, apply rotations to the last node visited in the search, which becomes the new root.

 Rotation: The rotations used in a splay tree are either a Zig or a Zig-Zig rotation. A Zig
rotation is used to bring a node to the root, while a Zig-Zig rotation is used to balance the
tree after multiple accesses to elements in the same subtree.

Here’s a step-by-step explanation of the rotation operations:

 Zig Rotation: If a node has a right child, perform a right rotation to bring it to the root. If it
has a left child, perform a left rotation.

 Zig-Zig Rotation: If a node has a grandchild that is also its child’s right or left child, perform
a double rotation to balance the tree. For example, if the node has a right child and the
right child has a left child, perform a right-left rotation. If the node has a left child and the
left child has a right child, perform a left-right rotation.

 Note: The specific implementation details, including the exact rotations used, may vary
depending on the exact form of the splay tree.

Rotations in Splay Tree

 Zig Rotation

 Zag Rotation

 Zig – Zig Rotation

 Zag – Zag Rotation

 Zig – Zag Rotation

 Zag – Zig Rotation

1) Zig Rotation:

The Zig Rotation in splay trees operates in a manner similar to the single right rotation in AVL Tree
rotations. This rotation results in nodes moving one position to the right from their current
location. For example, consider the following scenario:
Zig Rotation (Single Rotation)

2) Zag Rotation:

The Zag Rotation in splay trees operates in a similar fashion to the single left rotation in AVL Tree
rotations. During this rotation, nodes shift one position to the left from their current location. For
instance, consider the following illustration:

Zag Rotation (Single left Rotation)

3) Zig-Zig Rotation:

The Zig-Zig Rotation in splay trees is a double zig rotation. This rotation results in nodes shifting
two positions to the right from their current location. Take a look at the following example for a
better understanding:
Zig-Zig Rotation (Double Right Rotation)

4) Zag-Zag Rotation:

In splay trees, the Zag-Zag Rotation is a double zag rotation. This rotation causes nodes to move
two positions to the left from their present position. For example:

Zag-Zag Rotation (Double left rotation)

5) Zig-Zag Rotation:

The Zig-Zag Rotation in splay trees is a combination of a zig rotation followed by a zag rotation. As
a result of this rotation, nodes shift one position to the right and then one position to the left from
their current location. The following illustration provides a visual representation of this concept:
Zig- Zag rotation

6) Zag-Zig Rotation:

The Zag-Zig Rotation in splay trees is a series of zag rotations followed by a zig rotation. This results
in nodes moving one position to the left, followed by a shift one position to the right from their
current location. The following illustration offers a visual representation of this concept:

Zag-Zig Rotation

Searching in Splay Tree

Splay Tree-

Splay tree is a binary search tree. In a splay tree, M consecutive operations can be performed in O (M
log N) time.

A single operation may require O(N) time but average time to perform M operations will need O (M
Log N) time.
When a node is accessed, it is moved to the top through a set of operations known as splaying.
Splaying technique is similar to rotation in an AVL tree. This will make the future access of the node
cheaper.

Unlike AVL tree, splay trees do not have the requirement of storing Balance Factor of every node.
This saves the space and simplifies algorithm to a great extent.

There are two standard techniques of splaying.

1. Bottom up Splaying

2. Top Down Splaying

1. Bottom up Splaying:-

Idea behind bottom up splaying is explained below: Rotation is performed bottom up along the
access path.

Let X be a (non root) node on the access path at which we are rotating.

a) If the parent of X is the root of the tree, rotate X and the parent of X. This will be the last rotation
required.

b) If X has both a parent (P) and Grand parent (G) then like an AVL tree there could be four cases.

These four cases are:

1. X is a left child and P is a left child.

2. X is a left child and P is right child

3. X is a right child and P is a left child

4. X is a right child and P is a right child.

2.Top Down Splaying:-

When an item X is inserted as a leaf, a series of tree rotations brings X at the root. These rotations
are known as splaying. A splay is also performed during searches, and if an item is not found, a splay
is performed on the last node on the access path.

A top down traversal is performed to locate the leaf node.

Splaying is done using bottom up traversal.

This can be done by storing the access path, during top down traversal on a stack.

Top down splaying is based on splaying on the initial traversal path. A stack is not required to save
the traversal path.

At any point in the access:

1. A current node X that is the root of its sub tree and represented as the “middle” tree.

2. Tree L stores nodes in the tree T that are less than X, but not in the X’s sub tree.

3. Tree R stores nodes in the tree T that are larger than X, but not in X’s sub tree.

4. Initially, X is the root of T, and L and R are empty.


The worst case time complexity of Binary Search Tree (BST) operations like search, delete, insert is
O(n). The worst case occurs when the tree is skewed. We can get the worst case time complexity as
O(Logn) with AVL and Red-Black Trees.
Can we do better than AVL or Red-Black trees in practical situations?
Like AVL and Red-Black Trees, Splay tree is also self-balancing BST. The main idea of splay tree is to
bring the recently accessed item to root of the tree, this makes the recently searched item to be
accessible in O(1) time if accessed again. The idea is to use locality of reference (In a typical
application, 80% of the access are to 20% of the items). Imagine a situation where we have millions
or billions of keys and only few of them are accessed frequently, which is very likely in many practical
applications.
All splay tree operations run in O(log n) time on average, where n is the number of entries in the
tree. Any single operation can take Theta(n) time in the worst case.
Search Operation
The search operation in Splay tree does the standard BST search, in addition to search, it also splays
(move a node to the root). If the search is successful, then the node that is found is splayed and
becomes the new root. Else the last node accessed prior to reaching the NULL is splayed and
becomes the new root.
There are following cases for the node being accessed.
1) Node is root We simply return the root, don’t do anything else as the accessed node is already
root.
2) Zig: Node is child of root (the node has no grandparent). Node is either a left child of root (we do
a right rotation) or node is a right child of its parent (we do a left rotation).
T1, T2 and T3 are subtrees of the tree rooted with y (on left side) or x (on right side)

y x

/\ Zig (Right Rotation) / \

x T3 – - – - – - – - - -> T1 y

/\ <--------- /\

T1 T2 Zag (Left Rotation) T2 T3

3) Node has both parent and grandparent. There can be following subcases.
……..3.a) Zig-Zig and Zag-Zag Node is left child of parent and parent is also left child of grand parent
(Two right rotations) OR node is right child of its parent and parent is also right child of grand parent
(Two Left Rotations).

Zig-Zig (Left Left Case):

G P X

/\ / \ /\

P T4 rightRotate(G) X G rightRotate(P) T1 P

/\ ============> / \ / \ ============> /\

X T3 T1 T2 T3 T4 T2 G

/\ /\
T1 T2 T3 T4

Zag-Zag (Right Right Case):

G P X

/ \ / \ /\

T1 P leftRotate(G) G X leftRotate(P) P T4

/ \ ============> / \ / \ ============> / \

T2 X T1 T2 T3 T4 G T3

/\ /\

T3 T4 T1 T2

……..3.b) Zig-Zag and Zag-Zig Node is right child of parent and parent is left child of grand parent (Left
Rotation followed by right rotation) OR node is left child of its parent and parent is right child of
grand parent (Right Rotation followed by left rotation).

Zag-Zig (Left Right Case):

G G X

/\ / \ / \

P T4 leftRotate(P) X T4 rightRotate(G) P G

/ \ ============> / \ ============> / \ / \

T1 X P T3 T1 T2 T3 T4

/\ /\

T2 T3 T1 T2

Zig-Zag (Right Left Case):

G G X

/ \ / \ / \

T1 P rightRotate(P) T1 X leftRotate(G) G P

/ \ =============> / \ ============> / \ / \

X T4 T2 P T1 T2 T3 T4

/\ /\

T2 T3 T3 T4
Example:

100 100 [20]

/ \ / \ \

50 200 50 200 50

/ search(20) / search(20) / \

40 ======> [20] ========> 30 100

/ 1. Zig-Zig \ 2. Zig-Zig \ \

30 at 40 30 at 100 40 200

/ \

[20] 40

class Node:

def __init__(self, key):

self.key = key

self.left = None

self.right = None

def new_node(key):

return Node(key)

def right_rotate(x):

y = x.left

x.left = y.right

y.right = x

return y

def left_rotate(x):

y = x.right

x.right = y.left
y.left = x

return y

def splay(root, key):

if root is None :

return new_node(key)

if root.key == key:

return root

if root.key > key:

if root.left is None:

return root

if root.left.key > key:

root.left.left = splay(root.left.left, key)

root = right_rotate(root)

elif root.left.key < key:

root.left.right = splay(root.left.right, key)

if root.left.right:

root.left = left_rotate(root.left)

return root.left if root.left is None else right_rotate(root)

else:

if root.right is None:

return root

if root.right.key > key:

root.right.left = splay(root.right.left, key)

if root.right.left:

root.right = right_rotate(root.right)

elif root.right.key < key:

root.right.right = splay(root.right.right, key)

root = left_rotate(root)

return root.right if root.right is None else left_rotate(root)


def insert(root, key):

if root is None:

return new_node(key)

root = splay(root, key)

if root.key == key:

return root

node = new_node(key)

if root.key > key:

node.right = root

node.left = root.left

root.left = None

else:

node.left = root

node.right = root.right

root.right = None

return node

def pre_order(node):

if node:

print(node.key, end=" ")

pre_order(node.left)

pre_order(node.right)

if __name__ == "__main__":

root = None

root = insert(root, 100)

root = insert(root, 50)


root = insert(root, 200)

root = insert(root, 40)

root = insert(root, 60)

print("Preorder traversal of the modified Splay tree:")

pre_order(root)

You might also like