# Splay Trees

Splay Trees are kind of special when it comes to how they work performance wise. They just as Red-Black trees have a performance of O(log(n)) but it accomplishes this by looking at m operations and making these O(m log(n)).

## Bottom Up

The bottom up method uses a term called _splaying_, this will set a specific element as the root element through rotations. This splaying method can be explained with three specific cases:

> Tip: A good way to implement going towards the node is by pushing the nodes that are used in the path on a stack. That way we can go back to the parent and the grandparent.

### Operations

* **Search:** Search as in a BST, and make the last node the root by using the splay operation.
* **Insert:** Insert as in a BST, and make this inserted node the root.
* **Remove:** Remove as in a BST, and make the parent of the removed node the root. If the element was not found, make the last node in the search path the root.

### Cases

| Zig Case                                                                                                                                                                                                                                                                                          | Zig Zag Case                                                                                                                                                                                                                                                                                             |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Rotate Right over P ![](https://1983113773-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lc5VIoifZmhHibC2KSG%2F-Lc5VL9WESreusC3YQxb%2F-Lc5VT3tMfWuF9YEGudu%2Fsplay_bottom_up_zig_case.png?generation=1554887341860097\&alt=media)                                                | First rotate C left over P and then rotate this solution right over G ![](https://1983113773-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lc5VIoifZmhHibC2KSG%2F-Lc5VL9WESreusC3YQxb%2F-Lc5VT3v0keU-mc3OZc8%2Fsplay_bottom_up_zig_zag_case.png?generation=1554887342579365\&alt=media) |
| Zig Zig Case                                                                                                                                                                                                                                                                                      |                                                                                                                                                                                                                                                                                                          |
| First rotate C over P right and then again rotate right over G ![](https://1983113773-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lc5VIoifZmhHibC2KSG%2F-Lc5VL9WESreusC3YQxb%2F-Lc5VT3xCBW9q4nbLdL3%2Fsplay_bottom_up_zig_zig_case.png?generation=1554887342982733\&alt=media) |                                                                                                                                                                                                                                                                                                          |

## Top Down

The top down method has been recommended by the creators of this algorithm, because it is faster then the bottom-up version. When going down we will put all the nodes on this path out of the way, and we will perform rotations to keep our atomaire operations.

How it works is that we have 3 subtrees called L, R and M. L contains the keys that are < than the keys in M and R contains the keys that are > than the keys in M. We start with L and R empty and M with our whole subtree. Now we can split our trees as shown in the cases below.

### Operations

* **Search:** Make the with the searched key root, if it is not found, make the successor root.
* **Insert:** The new node gets the left subtree as its left child and the right subtree as its right child (see cases and merge)
* **Remove:** Find the key and remove it when its found. Afterwards merge the 2 subtrees again.

### Cases

| Zig Case                                                                                                                                                                                                                          | Zig Zig Case                                                                                                                                                                                                                      |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| ![](https://1983113773-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lc5VIoifZmhHibC2KSG%2F-Lc5VL9WESreusC3YQxb%2F-Lc5VT3zUN6-3ZIIOqkN%2Fsplay_top_down_zig_case.png?generation=1554887342105959\&alt=media)     | ![](https://1983113773-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lc5VIoifZmhHibC2KSG%2F-Lc5VL9WESreusC3YQxb%2F-Lc5VT40xcNwm1tpN4uX%2Fsplay_top_down_zig_zig_case.png?generation=1554887337364447\&alt=media) |
| Zig Zag Case                                                                                                                                                                                                                      |                                                                                                                                                                                                                                   |
| ![](https://1983113773-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lc5VIoifZmhHibC2KSG%2F-Lc5VL9WESreusC3YQxb%2F-Lc5VT42sk9vqhZ4TSMN%2Fsplay_top_down_zig_zag_case.png?generation=1554887342208794\&alt=media) |                                                                                                                                                                                                                                   |

Once we are down, the only thing left to do is to merge the M, L and R subtrees back together with root C.

![](https://1983113773-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lc5VIoifZmhHibC2KSG%2F-Lc5VL9WESreusC3YQxb%2F-Lc5VT44l4TD6VF7ZVYb%2Fsplay_top_down_merge.png?generation=1554887342580874\&alt=media)
