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(mlog(1+nm)) for two trees of size m and n(≥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(t1,k,t2) 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 t1, root k and right subtree t2. Suppose that t1 is heavier (this "heavier" depends on the balancing scheme) than t2 (the other case is symmetric). Join follows the right spine of t1 until a node c which is balanced with t2. At this point a new node with left child c, root k and right child t2 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:
function joinRightAVL(TL, k, TR) (l, k', c) := expose(TL) if h(c) ≤ h(TR) + 1 T' := Node(c, k, TR) if h(T') ≤ h(l) + 1 return Node(l, k', T') else return rotateLeft(Node(l, k', rotateRight(T'))) else T' := joinRightAVL(c, k, TR) T := Node(l, k', T') if h(T') ≤ h(l) + 1 return T else return rotateLeft(T)
function joinLeftAVL(TL, k, TR) /* symmetric to joinRightAVL */
function join(TL, k, TR) if h(TL) > h(TR) + 1 return joinRightAVL(TL, k, TR) else if h(TR) > h(TL) + 1 return joinLeftAVL(TL, k, TR) else return Node(TL, k, TR)
Where:
h(v) is the height of node v. expose(v) extracts the left child l, key k, and right child r of node v into a tuple (l,k,r). Node(l,k,r) creates a node with left child l, key k, and right child r. The join algorithm for red–black trees:
function joinRightRB(TL, k, TR) if TL.color = black and ĥ(TL) = ĥ(TR) return Node(TL, ⟨k, red⟩, TR) else (L', ⟨k', c'⟩, R') := expose(TL) T' := Node(L', ⟨k', c'⟩, joinRightRB(R', k, TR)) if c' = black and T'.right.color = T'.right.right.color = red T'.right.right.color := black return rotateLeft(T') else return T'
function joinLeftRB(TL, k, TR) /* symmetric to joinRightRB */
function join(TL, k, TR) if ĥ(TL) > ĥ(TR) T' := joinRightRB(TL, k, TR) if (T'.color = red) and (T'.right.color = red) T'.color := black return T' else if ĥ(TR) > ĥ(TL) /* symmetric */ else if TL.color = black and TR = black return Node(TL, ⟨k, red⟩, TR) else return Node(TL, ⟨k, black⟩, TR)
Where:
ĥ(v) is the black height of node v. expose(v) extracts the left child l, key k, color c, and right child r of node v into a tuple (l,⟨k,c⟩,r). Node(l,⟨k,c⟩,r) creates a node with left child l, key k, color c, and right child r. The join algorithm for weight-balanced trees:
function joinRightWB(TL, k, TR) (l, k', c) := expose(TL) if w(TL) =α w(TR) return Node(TL, k, TR) else T' := joinRightWB(c, k, TR) (l1, k1, r1) := expose(T') if w(l) =α w(T') return Node(l, k', T') else if w(l) =α w(l1) and w(l)+w(l1) =α w(r1) return rotateLeft(Node(l, k', T')) else return rotateLeft(Node(l, k', rotateRight(T'))
function joinLeftWB(TL, k, TR) /* symmetric to joinRightWB */
function join(TL, k, TR) if w(TL) >α w(TR) return joinRightWB(TL, k, TR) else if w(TR) >α w(TL) return joinLeftWB(TL, k, TR) else return Node(TL, k, TR)
Where:
w(v) is the weight of node v. w1=αw2 means weights w1 and w2 are α-weight-balanced. w1>αw2 means weight w1 is heavier than weight w2 with respect to the α-weight-balance. expose(v) extracts the left child l, key k, and right child r of node v into a tuple (l,k,r). Node(l,k,r) creates a node with left child l, key k and right child r.
Join-based algorithms In the following, expose(v) extracts the left child l, key k, and right child r of node v into a tuple (l,k,r). Node(l,k,r) creates a node with left child l, key k and right child r. "s1||s2" means that two statements s1 and s2 can run in parallel.
… excerpt ends here. Continue reading the full article.