Splay Tree.pptx
Splay Tree.pptx
Introduction
• 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.
• 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) .
• 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.
Introduction
• 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.
• 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.
Introduction
• 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.
Operations in a splay tree
• 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.
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.
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.
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.
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
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
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
Drawbacks of splay tree data structure
• Unbalanced Trees: Splay trees can become unbalanced and inefficient if
the tree is repeatedly rotated in the same direction.
• Memory Usage: Splay trees can use a lot of memory compared to other
data structures because each node contains additional information.
• Complexity: Splay trees can have a high time complexity for basic
operations such as insertion and deletion because the trees need to be
reorganized after every operation.
• Reorganization Overhead: The splaying operation required in every
operation can be time-consuming and result in a high overhead.
• Limited Use Cases: Splay trees are not suitable for all data structures and
have limited use cases because they don’t handle duplicate keys
efficiently.
Applications of the splay tree
• Caching: Splay trees can be used to implement cache memory management, where the most
frequently accessed items are moved to the top of the tree for quicker access.
• Database Indexing: Splay trees can be used to index databases for faster searching and retrieval of
data.
• File Systems: Splay trees can be used to store file system metadata, such as the allocation table,
directory structure, and file attributes.
• Data Compression: Splay trees can be used to compress data by identifying and encoding
repeating patterns.
• Text Processing: Splay trees can be used in text processing applications, such as spell-checkers,
where words are stored in a splay tree for quick searching and retrieval.
• Graph Algorithms: Splay trees can be used to implement graph algorithms, such as finding the
shortest path in a weighted graph.
• Online Gaming: Splay trees can be used in online gaming to store and manage high scores,
leaderboards, and player statistics.
Advantages of Splay Trees
• Splay trees have amortized time complexity of
O(log n) for many operations, making them
faster than many other balanced tree data
structures in some cases.
• Splay trees are self-adjusting, meaning that
they automatically balance themselves as
items are inserted and removed. This can help
to avoid the performance degradation that
can occur when a tree becomes unbalanced.
Disadvantages of Splay Trees
• Splay trees can have worst-case time
complexity of O(n) for some operations,
making them less predictable than other
balanced tree data structures like AVL trees or
red-black trees.
• Splay trees may not be suitable for certain
applications where predictable performance is
required