This is the implementation of splay tree - self-balancing binary search tree.
The purpose of this project is to implement SplayTree class and it's basic operations.
- Splay tree qualities
Advantages | Disadvantages |
---|---|
Average-case performance is as efficient as other trees | In some cases the height of a splay tree can be linear |
Splay trees don't need to store any additional data in nodes |
-
rotate(child, parent) - rotates edge between child and parent node in splay tree
-
splay(x) - moves node x to splay tree's root due to following operations:
- zig(x) - runs if there is no grandparent of target node
- zig-zig(x) - runs if node and it's parent are both left or right childs of their parents
- zig-zag(x) - runs if node and it's parent are not both left or right childs of their parents
-
find(vertex, x) - trying to find node with value
x
beginning from given nodevertex
(as in simple BST).
Then runssplay
of it's result. If there is no node with valuex
in the tree then runssplay
from node with closest value tox
(leaf-node that was last during find process) -
split(vertex, x) - splits given by pointer
vertex
tree into two: in first all values are less thanx
, in second one all values are bigger or equal -
insert(vertex, x) - splits given by pointer
vertex
tree by valuex
. Then first subtree becomes left child and second subtree becomes right child of new node -
merge(root1, root2) - merges two trees into one. All values in first tree must be less than values in second tree
-
remove(vertex, x) - removes from given by pointer
vertex
tree node with valuex
. Runsfind(vertex, tree)
. Then runsmerge
left and right childs of new root.
-
Amortized time of
m
operations is actuallyO(m*log(n))
-
You can find proof here