Here is the code for a splay operation at node x.
splay(x) {
while x is not the root do {
if x.p is the root then {
rotate x.p so x becomes the root
} else if x and x.p are both left children,
or both right children then {
rearrange zig-zig path to opposite zig-zig path
} else {
rearrange zig-zag path to sibling path
}
}
}
search(k)
— do a search like in the binary search tree. Let x
be the last node accessed. Then either x.key
= k
, or k
is not in the tree but k
’s parent would be x
if k
were in the tree. Splay x
.insert(k)
— use the bst algorithm to insert k
, say in the new node x
. Splay x
.delete(z)
— execute the BST algorithm. It sets y
to z
or its successor, and replaces y
by child x
. Splay y.p
.