# Join-based tree algorithms

In computer science, join-based tree algorithms are a class of algorithms for self-balancing binary search trees. This framework aims at designing highly-parallelized algorithms for various balanced binary search trees. The algorithmic framework is based on a single operation join. Under this framework, the join operation captures all balancing criteria of different balancing schemes, and all other functions join have generic implementation across different balancing schemes. The join-based algorithms can be applied to at least four balancing schemes: AVL trees, red–black trees, weight-balanced trees and treaps.
The join$(L,k,R)$ operation takes as input two binary balanced trees $L$ and $R$ of the same balancing scheme, and a key $k$, and outputs a new balanced binary tree $t$ whose in-order traversal is the in-order traversal of $L$, then $k$ then the in-order traversal of $R$. In particular, if the trees are search trees, which means that the in-order of the trees maintain a total ordering on keys, it must satisfy the condition that all keys in $L$ are smaller than $k$ and all keys in $R$ are greater than $k$.

## History

The join operation was first defined by Tarjan on red–black trees, which runs in worst-case logarithmic time. Later Sleator and Tarjan described a join algorithm for splay trees which runs in amortized logarithmic time. Later Adams extended join to weight-balanced trees and used it for fast set–set functions including union, intersection and set difference. In 1998, Blelloch and Reid-Miller extended join on treaps, and proved the bound of the set functions to be $O(m\log(1+{\tfrac {n}{m}}))$ for two trees of size $m$ and $n(\geq m)$, which is optimal in the comparison model. They also brought up parallelism in Adams' algorithm by using a divide-and-conquer scheme. In 2016, Blelloch et al. formally proposed the join-based algorithms, and formalized the join algorithm for four different balancing schemes: AVL trees, red–black trees, weight-balanced trees and treaps. In the same work they proved that Adams' algorithms on union, intersection and difference are work-optimal on all the four balancing schemes.

## Join algorithms

The function join$(t_{1},k,t_{2})$ considers rebalancing the tree, and thus depends on the input balancing scheme. If the two trees are balanced, join simply creates a new node with left subtree t$_1$, root k and right subtree t$_2$. Suppose that t$_1$ is heavier (this "heavier" depends on the balancing scheme) than t$_2$ (the other case is symmetric). Join follows the right spine of t$_1$ until a node c which is balanced with t$_2$. At this point a new node with left child c, root k and right child t$_2$ is created to replace c. The new node may invalidate the balancing invariant. This can be fixed with rotations.
The following is the join algorithms on different balancing schemes.
The join algorithm for AVL trees:
Where:
* $h(v)$ is the height of node $v$.
* ${\text{expose}}(v)$ extracts the left child $l$, key $k$, and right child $r$ of node $v$ into a tuple $(l,k,r)$.
* ${\text{Node}}(l,k,r)$ creates a node with left child $l$, key $k$, and right child $r$.
The join algorithm for red–black trees:
Where:
* ⁠${\hat {h}}(v)$⁠ is the black height of node $v$.
* ${\text{expose}}(v)$ extracts the left child $l$, key $k$, color $c$, and right child $r$ of node $v$ into a tuple $(l,\langle k,c\rangle ,r)$.
* ⁠${\text{Node}}(l,\langle k,c\rangle ,r)$⁠ creates a node with left child $l$, key $k$, color $c$, and right child $r$.
The join algorithm for weight-balanced trees:
Where:
* $w(v)$ is the weight of node $v$.
* $w_{1}=_{\alpha }w_{2}$ means weights $w_{1}$ and $w_{2}$ are α-weight-balanced.
* $w_{1}>_{\alpha }w_{2}$ means weight $w_{1}$ is heavier than weight $w_{2}$ with respect to the α-weight-balance.
* ${\text{expose}}(v)$ extracts the left child $l$, key $k$, and right child $r$ of node $v$ into a tuple $(l,k,r)$.
* ${\text{Node}}(l,k,r)$ creates a node with left child $l$, key $k$ and right child $r$.

## Join-based algorithms

In the following, ${\text{expose}}(v)$ extracts the left child $l$, key $k$, and right child $r$ of node $v$ into a tuple $(l,k,r)$. ${\text{Node}}(l,k,r)$ creates a node with left child $l$, key $k$ and right child $r$. "$s_{1}||s_{2}$" means that two statements $s_{1}$ and $s_{2}$ can run in parallel.

### Split

To split a tree into two trees, those smaller than key x, and those larger than key x, we first draw a path from the root by inserting x into the tree. After this insertion, all values less than x will be found on the left of the path, and all values greater than x will be found on the right. By applying Join, all the subtrees on the left side are merged bottom-up using keys on the path as intermediate nodes from bottom to top to form the left tree, and the right part is asymmetric. For some applications, Split also returns a boolean value denoting if x appears in the tree. The cost of Split is $O(\log n)$, order of the height of the tree.
The split algorithm is as follows:

### Join2

This function is defined similarly as join but without the middle key. It first splits out the last key $k$ of the left tree, and then join the rest part of the left tree with the right tree with $k$. The algorithm is as follows:
The cost is $O(\log n)$ for a tree of size $n$.

### Insert and delete

The insertion and deletion algorithms, when making use of join can be independent of balancing schemes. For an insertion, the algorithm compares the key to be inserted with the key in the root, inserts it to the left/right subtree if the key is smaller/greater than the key in the root, and joins the two subtrees back with the root. A deletion compares the key to be deleted with the key in the root. If they are equal, return join2 on the two subtrees. Otherwise, delete the key from the corresponding subtree, and join the two subtrees back with the root. The algorithms are as follows:
Both insertion and deletion requires $O(\log n)$ time if $|T|=n$.

### Set–set functions

Several set operations have been defined on weight-balanced trees: union, intersection and set difference. The union of two weight-balanced trees t$_1$ and t$_2$ representing sets A and B, is a tree t that represents A ∪ B. The following recursive function computes this union:
Similarly, the algorithms of intersection and set-difference are as follows:
The complexity of each of union, intersection and difference is $O\left(m\log \left({\tfrac {n}{m}}+1\right)\right)$ for two weight-balanced trees of sizes $m$ and $n(\geq m)$. This complexity is optimal in terms of the number of comparisons. More importantly, since the recursive calls to union, intersection or difference are independent of each other, they can be executed in parallel with a parallel depth $O(\log m\log n)$. When $m=1$, the join-based implementation applies the same computation as in a single-element insertion or deletion if the root of the larger tree is used to split the smaller tree.

### Build

The algorithm for building a tree can make use of the union algorithm, and use the divide-and-conquer scheme:
This algorithm costs $O(n\log n)$ work and has $O(\log ^{3}n)$ depth. A more-efficient algorithm makes use of a parallel sorting algorithm.
This algorithm costs $O(n\log n)$ work and has $O(\log n)$ depth assuming the sorting algorithm has $O(n\log n)$ work and $O(\log n)$ depth.

### Filter

This function selects all entries in a tree satisfying a predicate $p$, and return a tree containing all selected entries. It recursively filters the two subtrees, and join them with the root if the root satisfies $p$, otherwise join2 the two subtrees.
This algorithm costs work $O(n)$ and depth $O(\log ^{2}n)$ on a tree of size $n$, assuming $p$ has constant cost.

## Used in libraries

The join-based algorithms are applied to support interface for sets, maps, and augmented maps in libraries such as Hackage, SML/NJ, and PAM.