ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
JoinAlgorithms.h File Reference

The balanced binary search trees whose only balancing operation is join, beyond the book, and the operations on ordered sets and maps built on it, sequential and parallel. More...

#include "ADS/Contract.h"
#include "ADS/parallel/Executor.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <span>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/tree/impl/JoinAlgorithms.tpp"

Go to the source code of this file.

Classes

struct  ADS::AVLJoin< A >
 the join of the AVL trees; A gives also height( t ), -1 for nil, which connect() keeps More...
struct  ADS::RedBlackJoin< A >
 the join of the Red-Black trees, whose ranks are the black heights; A gives also red( t ), false for nil, and paint( t , red ) More...
struct  ADS::WeightBalancedJoin< A >
 the join of the trees balanced by weight, with \( \alpha = 0.29 \) More...
struct  ADS::TreapJoin< A >
 the join of the treaps; A gives also priority( t ), of a node More...
struct  ADS::JoinAlgorithms< A >
 the algorithms on the trees whose only balancing is the join of A; each of them consumes the trees it is given and returns the trees it makes More...
struct  ADS::JoinAlgorithms< A >::Tree
 a tree: its root and its rank More...
struct  ADS::JoinAlgorithms< A >::Exposed
 a tree taken apart at its root More...
struct  ADS::TakeSecond
 the value of the second tree, or of the batch, on a key in both More...
struct  ADS::TakeFirst
 the value of the first tree on a key in both More...
class  ADS::Joinable< T, A, Key, Value >
 the operations based on join of the tree T of keys Key and values Value, whose nodes A adapts [see JoinAdaptor], each the function of JoinAlgorithms of the same name: a base of T (which makes A its friend); A is read only in the bodies, as T is incomplete where its bases are named, and gives take( t ), the root of \( t \) taken out of it, which is left empty; put( t , r ), \( r \) the new root of the empty \( t \), which updates its size; peek( t ), its root; fresh( t ), an empty tree like \( t \); valid( t ), the invariant of \( t \); salt( t ) and make( k , v , s , i ), a node alone of the pair \( ( k , v ) \), the \( i \)-th of a batch of salt \( s \), which may be called in parallel More...

Namespaces

namespace  ADS

Concepts

concept  ADS::JoinAdaptor
 the nodes of a binary search tree as the algorithms based on join see them: nil(), the empty tree; left(), right(), key(), value() and size(), the reading of a node (size() of nil is 0); expose( t , l , r ), which takes off the children of \( t \) into \( l \) and \( r \), so that \( t \) is a node alone; connect( m , l , r ), which hangs \( l \) and \( r \) under the node alone \( m \) and updates what \( m \) keeps of its subtree (its size, its height, its augmented value, the parents of \( l \) and \( r \)); dispose( t ), which frees the subtree \( t \) (nothing for nil); rank( t ), the rank of a tree, and childRank( t , r , c ), that of the child \( c \) of \( t \) of rank \( r \), in \( O( 1 ) \); join( l , rl , m , r , rr ), the join of the balancing scheme, which returns the root and its rank

Variables

constexpr std::size_t ADS::kJoinGrain = 2048
 the sum of the sizes of two trees below which the algorithms run on the calling thread alone

Detailed description

The balanced binary search trees whose only balancing operation is join, beyond the book, and the operations on ordered sets and maps built on it, sequential and parallel.

join( L , k , R ), for two trees \( L \) and \( R \) whose keys are smaller and larger than \( k \), returns a balanced tree of the keys of \( L \), \( k \) and those of \( R \); it is the one operation that knows the balancing scheme, and split, the removals, the insertions, the union, the intersection, the difference, the batches and the filters are written once, on join alone, for every scheme. Adams wrote the union of the trees balanced by weight in this way; Blelloch, Ferizovic and Sun showed that the joins of four schemes (the AVL trees, the Red-Black trees, the trees balanced by weight and the treaps) are enough for the set operations to take the optimal work, and that the two recursive calls of each of them can run in parallel.

The cost of join is bounded through a rank \( r( T ) \) of each tree, which is \( \Theta( \log n ) \) for a tree of \( n \) keys: the height \( h \) for the AVL trees; the black height \( \hat{h} \) (the black nodes on a path from the root to a leaf, the root included if it is black, 0 for nil) for the Red-Black trees; \( \log_2 w( T ) \), \( w \) the weight (the keys plus 1), for the trees balanced by weight and, in expectation, for the treaps. Each join below goes down the right spine of the higher tree (the left one of the lower, symmetrically) until it meets a subtree \( c \) of the rank of the other tree, hangs \( k \) there with \( c \) and the other tree as its children, and fixes the balance on the way back by at most a double rotation per level; hence it costs \( O( | r( L ) - r( R ) | + 1 ) \), and the rank of its result is between \( \max( r( L ) , r( R ) ) \) and that plus 1. The trees balanced by weight take the parameter \( \alpha \le 1 - 1 / \sqrt{2} \), here 0.29; for the treaps, join is the merge of the two spines by the priorities, and its expected cost is that of an insertion, \( O( \log n ) \).

split( T , k ) exposes the root \( ( L , m , R ) \) of \( T \), recurs on the side of \( k \) and joins the other side with \( m \) and the part of the recursion on its side, in \( O( \log n ) \), since the costs of its joins telescope; join2( L , R ), the join with no key in between, takes the last key of \( L \) off it by splitLast() and joins it with \( R \), again in \( O( \log n ) \). The union of \( A \) and \( B \) exposes the root of the smaller tree, splits the larger one at its key, takes the unions of the two left and of the two right sides, in parallel, and joins them with the key; the intersection and the difference differ only in whether the key survives, and use join2 when it does not. For sizes \( m \le n \) the work is \( \Theta( m \log( n / m + 1 ) ) \), the lower bound of the comparisons for merging two sorted sequences, and the span \( O( \log m \log n ) \). A batch of \( m \) sorted pairs, multiInsert(), is the union with the tree built from the batch, and multiRemove() the difference, both done without building it: the batch is split by a binary search on the key of the root, in the same work; filter() and mapReduce() visit every node, \( O( n ) \) work and \( O( \log^2 n ) \) span. The arguments are on the theory page of the area.

A node may keep a value of its subtree, computed from those of its two children and of its own pair (its size, its height, the sum of the values of an augmented map [see JoinTree.h]); since every node that changes is passed to connect(), which recomputes that value, and the rotations are two exposes and two connects, join keeps it, and so does every algorithm above, at the cost of a combination per node that join touches.

A tree gives its nodes to the algorithms through an adaptor [see JoinAdaptor]: the joins of the four schemes (AVLJoin, RedBlackJoin, WeightBalancedJoin and TreapJoin) are written on it, JoinAlgorithms writes on it split(), join2() and the bulk operations, and Joinable gives them to the class of the tree as methods: AVLTree, Treap and the Red-Black tree of SearchTree, whose nodes keep the sizes of their subtrees for it, and JoinTree [see JoinTree.h], whose four schemes are augmented by a template parameter. All the operations are destructive: they consume their arguments and reuse their nodes, and a tree is not shared between versions (the persistent trees of PAM copy the path that changes, and count the references to the nodes).

An operation given an executor [see Executor.h] runs its two recursive calls by parallelInvoke(), until the two trees have fewer than kJoinGrain keys in all, or the executor one thread, where it goes on sequentially; the shape of the result does not depend on the executor, nor on the number of its threads, since join is deterministic and the priorities of a batch of a treap are drawn by the place of each pair in the batch. A key is compared, and a node read or written, by one thread at a time.

In the pseudocode of the functions below, a tree is nil or a node \( ( L , k , R ) \) of children \( L \) and \( R \) and key \( k \); \( \mathrm{expose}( T ) \) gives the three, \( \mathrm{node}( L , k , R ) \) makes a node and recomputes what it keeps (connect()), and \( r( T ) \) is the rank. Each join is given for the case of the left tree higher, the other being its mirror.

Among the libraries, PAM is that of the paper on augmented maps, persistent and on ParlayLib; PaC-trees (the library CPAM) keep blocks of up to \( B \) keys, possibly compressed, in the leaves of the same trees, and reach the same bounds with less space and fewer cache misses. De Man and Dhulipala give batch-parallel treaps (and link-cut and Euler tour trees) on a join of the same kind. oneTBB (concurrent_map) and folly (ConcurrentSkipList) keep concurrent skip lists, without bulk set operations or split; Boost.Intrusive has AVL, Red-Black, scapegoat, splay and treap trees with no split nor join of trees.

The contracts [see Contract.h]: the preconditions of join() and join2() on the order of the keys, the batches sorted with no repeated key (an audit), and after every operation the invariant of the tree, which the class checks at the level 3: the order of the keys, the sizes, the augmented values and the balance of its scheme (the heights of AVL, the colors and the black heights of Red-Black, the weights, the heap order of the priorities). The operations are defined in JoinAlgorithms.tpp.

See also
S. Adams, Efficient sets: a balancing act, Journal of Functional Programming 3(4), 553-561, 1993, doi:10.1017/S0956796800000885
G. E. Blelloch, D. Ferizovic, Y. Sun, Just join for parallel ordered sets, Proceedings of the 28th ACM Symposium on Parallelism in Algorithms and Architectures (SPAA), 253-264, 2016, doi:10.1145/2935764.2935768
G. E. Blelloch, D. Ferizovic, Y. Sun, Joinable parallel balanced binary trees, ACM Transactions on Parallel Computing 9(2), 1-41, 2022, doi:10.1145/3512769
G. E. Blelloch, M. Reid-Miller, Fast set operations using treaps, Proceedings of the 10th ACM Symposium on Parallel Algorithms and Architectures (SPAA), 16-26, 1998, doi:10.1145/277651.277660
M. R. Brown, R. E. Tarjan, A fast merging algorithm, Journal of the ACM 26(2), 211-226, 1979, doi:10.1145/322123.322127
J. Nievergelt, E. M. Reingold, Binary search trees of bounded balance, SIAM Journal on Computing 2(1), 33-43, 1973, doi:10.1137/0202005
F. K. Hwang, S. Lin, A simple algorithm for merging two disjoint linearly ordered sets, SIAM Journal on Computing 1(1), 31-39, 1972, doi:10.1137/0201004
Y. Hirai, K. Yamamoto, Balancing weight-balanced trees, Journal of Functional Programming 21(3), 287-307, 2011, doi:10.1017/S0956796811000104
R. Seidel, C. R. Aragon, Randomized search trees, Algorithmica 16(4-5), 464-497, 1996, doi:10.1007/BF01940876
R. E. Tarjan, Data Structures and Network Algorithms, SIAM, 1983, chapter 4 (the join of the Red-Black trees), doi:10.1137/1.9781611970265
Y. Sun, D. Ferizovic, G. E. Blelloch, PAM: parallel augmented maps, Proceedings of the 23rd ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming (PPoPP), 290-304, 2018, doi:10.1145/3178487.3178509
L. Dhulipala, G. E. Blelloch, Y. Gu, Y. Sun, PaC-trees: supporting parallel and compressed purely-functional collections, Proceedings of the 43rd ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI), 108-121, 2022, doi:10.1145/3519939.3523733
Q. De Man, L. Dhulipala, Fast and theoretically efficient batch-parallel link-cut trees, Euler tour trees, and treaps, Proceedings of the 38th ACM Symposium on Parallelism in Algorithms and Architectures (SPAA), 234-246, 2026, doi:10.1145/3816782.3819216
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa