Splay Tree
Splay Tree
Splay Tree
A splay tree is a self-adjusting binary search tree with the additional property that recently
accessed elements are quick to access again. It performs basic operations such as insertion,
look-up and removal in O(log n) amortized time. For many sequences of operations, splay
trees perform better than other search trees, even when the specific pattern of the sequence is
unknown. The splay tree was invented by Daniel Dominic Sleator and Robert Endre Tarjan in
1985.[1]
All normal operations on a binary search tree are combined with one basic operation, called
splaying. Splaying the tree for a certain element rearranges the tree so that the element is
placed at the root of the tree. One way to do this is to first perform a standard binary tree
search for the element in question, and then use tree rotations in a specific fashion to bring
the element to the top. Alternatively, a top-down algorithm can combine the search and the
tree reorganization into a single phase.
Contents
[hide]
• 1 Advantages
• 2 Disadvantages
• 3 Operations
o 3.1 Splaying
o 3.2 Insertion
o 3.3 Deletion
o 3.4 Deletion Code in C language
• 4 Code in C language
o 4.1 Splay operation in BST
• 5 Analysis
• 6 Performance theorems
• 7 Dynamic optimality conjecture
• 8 See also
• 9 References
• 10 External links
[edit] Advantages
Good performance for a splay tree depends on the fact that it is self-optimizing, in that
frequently accessed nodes will move nearer to the root where they can be accessed more
quickly. The height - though unlikely - is O(n), with the average being O(log n). This is an
advantage for nearly all practical applications,[citation needed] and is particularly useful for
implementing caches and garbage collection algorithms.
Advantages include:
• Small memory footprint—splay trees do not need to store any bookkeeping data.
• Possibility of creating a persistent data structure version of splay trees—which allows
access to both the previous and new versions after an update. This can be useful in
functional programming, and requires amortized O(log n) space per update.
• Working well with nodes containing identical keys—contrary to other types of self-
balancing trees. Even with identical keys, performance remains amortized O(log n).
All tree operations preserve the order of the identical nodes within the tree,[citation needed]
which is a property similar to stable sorting algorithms. A carefully designed find
operation can return the left most or right most node of a given key.
[edit] Disadvantages
Perhaps the most significant disadvantage of splay trees is that the height of a splay tree can
be linear. For example, this will be the case after accessing all n elements in non-decreasing
order. Since the height of a tree corresponds to the worst-case access time, this means that the
actual cost of an operation can be slow. However the amortized access cost of this worse case
is logarithmic, O(log n).
A splay tree can be worse than a static tree by at most a constant factor.
Splay trees can change even when they are accessed in a 'read-only' manner (i.e. by find
operations). This complicates the use of splay trees in a multi-threaded environment.
Specifically, extra management is needed if multiple threads are allowed to perform find
operations concurrently.
[edit] Operations
[edit] Splaying
Zig-zig Step: This step is done when p is not the root and x and p are either both right
children or are both left children. The picture below shows the case where x and p are both
left children. The tree is rotated on the edge joining p with its parent g, then rotated on the
edge joining x with p. Note that zig-zig steps are the only thing that differentiate splay trees
from the rotate to root method introduced by Allen and Munro[2] prior to the introduction of
splay trees.
Zig-zag Step: This step is done when p is not the root and x is a right child and p is a left
child or vice versa. The tree is rotated on the edge between x and p, then rotated on the edge
between x and its new parent g.
[edit] Insertion
To insert a node x into a splay tree, we first insert it as with a normal binary search tree. Then
we splay the new node x to the top of the tree...
[edit] Deletion
To delete a node x, we use the same method as with a binary search tree. If x has two
children, we replace its value with either the rightmost node of its left sub tree (its in-order
predecessor) or the leftmost node of its right subtree (its in-order successor). Then we remove
that node instead. In this way, deletion is reduced to the problem of removing a node with 0
or 1 children. After deletion, we splay the parent of the removed node to the top of the tree.
OR The node to be deleted is first splayed, i.e. brought to the root of the tree and then
deleted. This leaves the tree with two sub trees. The maximum element of the left sub tree (:
METHOD 1), or minimum of the right sub tree (: METHOD 2) is then splayed to the root.
The right sub tree is made the right child of the resultant left sub tree (for METHOD 1). The
root of left sub tree is the root of melded tree.
Here x is the node on which the splay operation is performed and root is the root node of the
tree.
void splay (struct node *x, struct node *root)
{
node *p,*g;
/*check if node x is the root node*/
if(x==root);
/*Performs Zig step*/
else if(x->parent==root)
{
if(x==(x->parent)->left)
rightrotation(root);
else
leftrotation(root);
}
else
{
p=x->parent; /*now points to parent of x*/
g=p->parent; /*now points to parent of x's parent*/
/*Performs the Zig-zig step when x is left and x's parent is left*/
if(x==p->left&&p==g->left)
{
rightrotation(g);
rightrotation(p);
}
/*Performs the Zig-zig step when x is right and x's parent is right*/
else if(x==p->right&&p==g->right)
{
leftrotation(g);
leftrotation(p);
}
/*Performs the Zig-zag step when x's is right and x's parent is left*/
else if(x==p->right&&p==g->left)
{
leftrotation(p);
rightrotation(g);
}
/*Performs the Zig-zag step when x's is left and x's parent is right*/
else if(x==p->left&&p==g->right)
{
rightrotation(p);
leftrotation(g);
}
splay(x, root);
}
}
[edit] Analysis
A simple amortized analysis of static splay trees can be carried out using the potential
method. Suppose that size(r) is the number of nodes in the subtree rooted at r (including r)
and rank(r) = log2(size(r)). Then the potential function P(t) for a splay tree t is the sum of the
ranks of all the nodes in the tree. This will tend to be high for poorly-balanced trees, and low
for well-balanced trees. We can bound the amortized cost of any zig-zig or zig-zag operation
by:
Balance Theorem[1]
The cost of performing the sequence S is O(m(1 + logn) + nlogn). In other words,
splay trees perform as well as static balanced binary search trees on sequences of at
least n accesses.
Static Optimality Theorem[1]
Let qi be the number of times element i is accessed in S. The cost of performing S is
In other words, splay trees perform as well as optimum static binary search trees
on sequences of at least n accesses.
Static Finger Theorem[1]
Let ij be the element accessed in the jth access of S and let f be any fixed element (the
Dynamic Optimality Conjecture:[1] Let A be any binary search tree algorithm that
accesses an element x by traversing the path from the root to x at a cost of d(x) + 1,
and that between accesses can make any rotations in the tree at a cost of 1 per
rotation. Let A(S) be the cost for A to perform the sequence S of accesses. Then the
cost for a splay tree to perform the same accesses is O(n + A(S)).
There are several corollaries of the dynamic optimality conjecture that remain unproven:
Traversal Conjecture:[1] Let T1 and T2 be two splay trees containing the same
elements. Let S be the sequence obtained by visiting the elements in T2 in preorder
(i.e. depth first search order). The total cost of performing the sequence S of accesses
on T1 is O(n).
Deque Conjecture:[7][5][8] Let S be a sequence of m double-ended queue operations
(push, pop, inject, eject). Then the cost of performing S on a splay tree is O(m + n).
Split Conjecture:[9] Let S be any permutation of the elements of the splay tree. Then
the cost of deleting the elements in the order S is O(n).