User:Cwang0/sandbox

= Zip Tree = In computer science, zip trees are a type of randomized binary search tree which each node in a zip tree is assigned a numeric rank, with the tree maintaining a max-heap order based on these ranks.

This structure allows for efficient insertions and deletions without the need for rotations, using a process called zipping and unzipping. The rank of each node is randomly chosen from a geometric distribution, allowing for a compact representation of the tree.

Zip
The zip operation combines two zip trees into one, ensuring the resulting tree maintains the binary search tree property and the max-heap property based on ranks. It's used primarily during deletion to merge two subtrees that were previously separated by a removed node.

Given two trees, x and y, the goal is to merge them into a single tree. If either x or y is null, the result is the non-null tree (or null if both are null).

Compare the ranks of the roots of x and y. The tree with the larger rank becomes the new root of the merged tree. If x's root has the smaller rank, its right child and y are zipped and the result becomes the new right child of x's root. If y's root has the smaller rank, its left child and x are zipped and the result becomes the new left child of y's root. function zip(x,y) if x = null do return y    if y = null do return x    if x.rank < y.rank do y.left ← zip(x, y.left) return y    else do x.right ← zip(x.right, y)       return x end function

Unzip
Unzipping is the inverse process of zipping and is implied during the insertion operation. When inserting a new node, if the node's rank is higher than the rank of the current node at a given position, the tree is "unzipped" to make space for the new node, ensuring it is placed at the correct position in the tree according to its rank and key.