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

Three trees balanced by partial rebuilding, from exercises 8.8 to 8.10 of Morin, beyond the book: binary search trees whose insertions and removals are those of the plain tree, followed by the rebuilding of the subtree of the highest node of the path that a rule selects into a perfectly balanced one [see ScapegoatTree.h for the rebuild]. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/dictionary/RandomHashing.h"
#include "ADS/numeric/RandomGenerators.h"
#include "ADS/tree/BinaryTreeAlgorithms.h"
#include "ADS/tree/ScapegoatTree.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/tree/impl/PartialRebuildingTree.tpp"

Go to the source code of this file.

Classes

class  ADS::PartialRebuildingTree< K, V, P >
 a DICTIONARY on a binary search tree balanced by partial rebuilding by the rule P More...
struct  ADS::PartialRebuildingTree< K, V, P >::Shape
 the shape of the tree, for PathIterator More...

Namespaces

namespace  ADS

Typedefs

template<std::totally_ordered K, class V>
using ADS::WeightRebuildTree
 the tree of exercise 8.8 of Morin, balanced by weight by rebuilds
template<std::totally_ordered K, class V>
using ADS::CountdownTree
 the CountdownTree of exercise 8.9 of Morin
template<std::totally_ordered K, class V>
using ADS::DynamiteTree = PartialRebuildingTree< K , V , RebuildPolicy::DYNAMITE >
 the DynamiteTree of exercise 8.10 of Morin

Enumerations

enum class  ADS::RebuildPolicy { ADS::RebuildPolicy::WEIGHT , ADS::RebuildPolicy::COUNTDOWN , ADS::RebuildPolicy::DYNAMITE }
 the rules that select the subtree to rebuild More...

Detailed description

Three trees balanced by partial rebuilding, from exercises 8.8 to 8.10 of Morin, beyond the book: binary search trees whose insertions and removals are those of the plain tree, followed by the rebuilding of the subtree of the highest node of the path that a rule selects into a perfectly balanced one [see ScapegoatTree.h for the rebuild].

Each node keeps the size of its subtree, updated along the path. They are DICTIONARY [see ADT.h] with the find(x) of an SSet (lowerBound()), and the policy is the parameter of PartialRebuildingTree:

  • WeightRebuildTree (exercise 8.8): every node but the root has \( s( u ) \le \frac23 s( u.\mathit{parent} ) \); a node whose child breaks it is rebuilt. Hence the sizes shrink by \( 3 / 2 \) per level and the height is at most \( \log_{3/2} n \). After a rebuild of a subtree of \( s \) nodes its children hold at most \( s / 2 \) of them, and they must reach \( 2 s' / 3 \), \( s' \) the size then, before the next rebuild of the same node, i.e., \( \Omega( s ) \) operations go through it in between; charging the rebuild to them, each operation pays \( O( 1 ) \) for each of the \( O( \log n ) \) nodes of its path, \( O( \log n ) \) amortized time. These are the trees of Overmars and van Leeuwen, balanced by weight and maintained by partial rebuilding rather than by rotations [see JoinTree.h for those by rotations].
  • CountdownTree (exercise 8.9): a node has a timer, decremented by each operation whose path goes through it and set to \( \lfloor s( u ) / 3 \rfloor \) when its subtree is rebuilt; the highest node whose timer runs out is rebuilt. A node rebuilt with \( s \) nodes sees at most \( s / 3 \) operations before its next rebuild, so that a child of it holds at most \( \frac34 \) of its subtree at any time and the height is at most \( \log_{4/3} n + O( 1 ) \); the rebuild costs \( O( s ) \), paid by those \( s / 3 \) operations, \( O( 1 ) \) per operation and per level, hence \( O( \log n ) \) amortized time.
  • DynamiteTree (exercise 8.10): each node of the path of an operation explodes with probability \( 1 / s( u ) \), its size after the operation, and the highest one that explodes is rebuilt. The expected cost of the explosions of a node is \( s \cdot 1 / s = O( 1 ) \) per operation through it; on a sequence of insertions fixed in advance, a node rebuilt with \( s \) nodes survives the more than \( s \) insertions it takes to unbalance it past \( \frac34 \) with probability below \( 1 / 2 \), also given the levels above it, which gives a path of at most \( 2 \lfloor \log_{4/3} n \rfloor + 2 \) nodes in expectation and \( O( \log n ) \) expected time per operation. With removals, the rebuilds still cost \( O( 1 ) \) expected per node of the path, but no bound on the depth is proved.

The analyses are in Trees of partial rebuilding. The random choices of DynamiteTree come from SplitMix64 [see RandomHashing.h] by uniformBelow() [see RandomGenerators.h].

Morin gives no code for them: the benchmarks compare them with ScapegoatTree, which pays its rebuilds by \( q \) and needs no field in the nodes, while these keep a size (and a timer) in each node.

The contracts [see Contract.h]: 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 sizes and, for WeightRebuildTree, the balance of the weights. The operations are defined in PartialRebuildingTree.tpp.

See also
M. H. Overmars, J. van Leeuwen, Dynamic multi-dimensional data structures based on quad- and k-d trees, Acta Informatica 17(3), 267-285, 1982, doi:10.1007/BF00264354
M. H. Overmars, The Design of Dynamic Data Structures, Lecture Notes in Computer Science 156, Springer, 1983, doi:10.1007/BFb0014927
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, chapter 8 and exercises 8.8 to 8.10
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa