|
ADS
Algorithms and Data Structures
|
The scapegoat tree of Galperin and Rivest, the general balanced tree of Andersson (section 8.1 of Morin), beyond the book: a binary search tree with no balance information in its nodes, whose height stays at most \( \log_b q \), \( q \) an upper bound on the number of keys that satisfies \( q / 2 \le n \le q \) and \( 1 < b < 2 \) a parameter (\( b = 3 / 2 \) in Morin, \( b = 1 / \alpha \) in Galperin and Rivest), by rebuilding subtrees into perfectly balanced ones. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include <algorithm>#include <cmath>#include <concepts>#include <cstddef>#include <optional>#include <utility>#include <vector>#include "ADS/tree/BinaryTreeAlgorithms.h"#include "ADS/tree/impl/ScapegoatTree.tpp"Go to the source code of this file.
Classes | |
| class | ADS::ScapegoatTree< K, V, StoredSizes > |
| a DICTIONARY on a scapegoat tree of height at most \( \log_b q \), whose nodes keep the size of their subtree if StoredSizes More... | |
| struct | ADS::ScapegoatTree< K, V, StoredSizes >::Shape |
| the shape of the tree, for PathIterator More... | |
Namespaces | |
| namespace | ADS |
The scapegoat tree of Galperin and Rivest, the general balanced tree of Andersson (section 8.1 of Morin), beyond the book: a binary search tree with no balance information in its nodes, whose height stays at most \( \log_b q \), \( q \) an upper bound on the number of keys that satisfies \( q / 2 \le n \le q \) and \( 1 < b < 2 \) a parameter (\( b = 3 / 2 \) in Morin, \( b = 1 / \alpha \) in Galperin and Rivest), by rebuilding subtrees into perfectly balanced ones.
An insertion that makes a leaf deeper than \( \log_b q \) climbs from it to the first ancestor \( w \) below which it is deeper than \( \log_b s( w ) \), the scapegoat, and rebuilds the subtree of \( w \), whose height becomes \( \lfloor \log_2 s( w ) \rfloor < d - \mathrm{depth}( w ) \), so that no node is left deeper than the parent of the new leaf was. The root qualifies, since \( \log_b n \le \log_b q < d \), and the scapegoat has a child \( u \) on the path with \( s( u ) > s( w ) / b \), since otherwise the sizes would shrink by \( b \) at each level below it. This is the rule of Galperin and Rivest (HEIGHT, the default); the rule of Morin (WEIGHT), the first ancestor with \( s( u ) > s( w ) / b \), rebuilds the smallest unbalanced subtree. The argument of section 8.1 of Morin for it, that the subtree of a scapegoat is not complete and so loses a level, leaves one step unstated: the new leaf is the only node at its depth \( d \), since the tree was at most \( \log_b q < d \) high before, so that the subtree of \( w \) had height at most \( d - \mathrm{depth}( w ) - 1 \) and, not being complete, fewer than \( 2^{d - \mathrm{depth}( w )} - 1 \) nodes. "Not complete" alone does not suffice: a node \( w \) with one child, whose two children are leaves, has \( s( w ) = 4 \), a child of \( 3 > \frac23 \cdot 4 \), and 4 nodes perfectly balanced still have height 2; but this subtree cannot arise, since its other leaf would already be at the depth of the new one. The class keeps the rule as an option, its height checked by the invariant; the test \( i = 0 \) of its climb, which takes the root when no node qualifies, is never met (Lemma 8.1 of Morin), and guards only against the rounding of \( b \cdot s( u ) \). A removal rebuilds the whole tree when \( q > 2 n \). A search takes \( O( \log n ) \) in the worst case, and an insertion or a removal \( O( \log n ) \) amortized (Lemma 8.3 and Theorem 8.1 of Morin, whose argument with \( b \) in place of \( 3 / 2 \) charges \( 1 / ( 2 / b - 1 ) \) credits per node of a path, so that the constant grows as \( b \) nears 2 and the height grows as it nears 1: exercise 4 of chapter 8).
The variants of the exercises of chapter 8 of Morin are options of the class, so that the benchmarks compare them on the same code. The sizes are computed when the climb needs them, with no field in the nodes, as in Morin; RECOMPUTE counts the whole subtree of each ancestor, as his code does, REUSE (the default, exercise 8.5) adds to the size of the child on the path, known from the step before, the size of the other child alone, which makes the climb cost \( O( s( w ) ) \) in all instead of a sum of sizes; ScapegoatTree< K , V , true > keeps the sizes in the nodes (exercise 8.6), updated along the path of each insertion and removal, so that the climb reads them. The rebuild lays the nodes of the subtree in a buffer and links the middle one at the root of each part (ARRAY, as in Morin, but in a buffer kept from one rebuild to the next instead of an array allocated each time), or threads them into a list by their right links and builds the tree from the list by recursion with no buffer (LIST, exercise 8.7), the two giving the same tree. The counters rebuilds() and rebuiltNodes() measure the work of the rebuilds, e.g., the \( \Omega( n \log n ) \) of the insertions in increasing order (exercise 8.3).
Where it is meant to be faster than the ScapegoatTree of Morin, for the benchmarks to check: his nodes have a parent, here the climb follows the path of the descent kept in a vector, so that a node has 2 links instead of 3; his test of the depth computes \( \log_{3/2} q \) by a logarithm at each insertion, here a comparison with a table of the powers of \( b \); his climb recomputes \( s( w ) \) and \( s( w.\mathit{parent} ) \) from scratch at each step, REUSE counts each node at most once; and the buffer of the rebuild is allocated once.
The contracts [see Contract.h]: \( 1 < b < 2 \); insert() ensures that the key is there and remove() that it is not; the invariant, an \( O( n ) \) check at the level 3, is the order of the keys, the bound \( h \le \log_b q \), \( q / 2 \le n \le q \) and the stored sizes. The operations are defined in ScapegoatTree.tpp.