Splay trees in Data Structure

play tree is defined as a self-balancing binary search tree with the extra property that recently accessed elements are quick to access again. Basic operations such as insertion, look-up and removal are performed by splay tree in O(log n) amortized time. For many sequences of non-random operations, splay trees work better than other search trees, even when the specific pattern of the sequence is not known. All normal operations on a binary search tree are joined with one basic operation, called splaying.

Let us assume that for each node a, we store a real number key(a).

In any binary search tree left subtree of any node a contains items having “key” values smaller than the value of key(a) and right subtree of the node a contains items with “key” values higher than the value of key(a).

In splay trees, we first search the query item, say a as in the usual binary search trees— compare the query item with the value in the root, if less then recursively search in the left subtree else if higher then, recursively search in the right subtree, and if it is equal then we are done. Then, informally speaking, we look at every disjoint pair of consecutive ancestors f a, say b =parent(a) and c =parent(b), and accomplish certain pair of rotations. As a result of these rotations, a comes in place of c.

In case a has an odd number of proper ancestors, then the ancestor of a (which is child of the root), will also have to be dealt in separate way, in terminal case— we rotate the edge between a and the root. This step is denoted as zig step.

If a and b are both left or are both right children of their respective parents, then we first rotate the edge between b and its parent c and then the edge between a and its parent b. This step is denoted as zig-zig step.

If a is a left (respectively right) child and b is a right (respectively left) child, then we first rotate the edge between a and b and then between a and c, this step is denoted as zig-zag step.