User:Ofek Gila/sandbox

= Zip tree =

In computer science, a zip tree is a randomized self-balancing binary search tree. Zip trees are named after how they zip or unzip in order to balance, rather than using rotations like other similar binary search trees. Zip trees are very closely related to both treaps and skip lists, which are both probabilistic data structures that allow $$\mathcal{O}(\log n)$$ search, insertion, and deletion complexity, with high probability.

Zip trees were designed to improve upon treaps by allowing rank ties, reducing the number of random bits used per node to $$\mathcal{O}(\log \log n)$$ rather than $$\mathcal{O}(\log n)$$, and by using zipping and unzipping to merge paths rather than tree rotations which reduces pointer changes. Allowing rank ties comes at a cost of an average height increase of about 8%. Due to the fact that zip trees make a majority of their changes near the leaves, rarely modifying nodes near the root, they may be a good candidate for concurrent implementation.

Description
The zip tree was first described by Tarjan et al. in 2018, zip meaning "to move very fast", referring to to how it efficiently merges paths. It reduces pointer changes by a constant factor of at least three when compared to tree rotations. Other than the different insertion and deletion algorithms, the zip tree is similar to a treap with ranks following a geometric distribution with a mean of 1, as opposed to a uniform distribution over a large set. For this reason, zip tree ranks are can be much smaller than treap ranks. As with any binary search tree, the inorder traversal order of the nodes is the same as the sorted order of the keys. The structure of the tree is uniquely determined by the requirement that it be heap-ordered, with ties broken in favor of smaller keys: that is, the priority of of non-leaf node must be greater than its left child and greater than or equal to its right child. The choice of favoring smaller keys for rank ties was chosen to increase the natural isomorphism between zip trees and skip lists.

Operations
The following operation descriptions assume that all nodes have distinct keys.

Searching
Searching for a key k in a zip tree is much the same as in any binary search tree data structure. A standard binary search algorithm is used, ignoring the node ranks.

Insertion
Inserting a new node x with key x.key into a zip tree requires generating a rank for the new node, finding the node y that x will replace, and unzipping the rest of the search path for x. First, a rank x.rank is generated from a geometric distribution with a mean of 1. Then, a normal binary search is used to find a node y such that either y.rank < x.rank, or y.rank = x.rank and x.key < y.key (favoring the smaller key in case of ties). If y is the root, then set x to the new root, otherwise update y's parent to have x as its new child. From there, the rest of the path is unzipped by splitting it into a path P containing nodes with keys less than x.key, and Q containing nodes with keys greater than x.key. Then the left and right subtrees of node x are set to the top nodes in P and in Q, respectively.

Deletion
Deleting a node x from a zip tree directly inverses the effects of inserting that same node. First, the node x is found using a standard binary search algorithm. From there, zip together the right spine of the left subtree of node x, and the left spine of the right subtree of node x, merging them in such a way that the zip tree definition is preserved. Specifically, such that they are merged in non-increasing rank order, with ties broken in favor of smaller keys. Let node y be the top node of this new merged path. If x was the root, then y as the new root, otherwise update x 's parent to have y as its new child.