|
ADS
Algorithms and Data Structures
|
The binary search tree of Morin whose nodes keep the size, the depth and the height of their subtree (exercise 6.19 of Morin), beyond the book: a DICTIONARY [see ADT.h] with the find(x) of an SSet (lowerBound()) and the getLE(x) of exercise 6.14, whose insertions and removals keep the three measures of every node exact for a constant factor on the plain tree, \( O( h ) \) time. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/tree/BinaryTreeAlgorithms.h"#include <algorithm>#include <concepts>#include <cstddef>#include <optional>#include <utility>#include <vector>#include "ADS/tree/impl/MeasuredSearchTree.tpp"Go to the source code of this file.
Classes | |
| class | ADS::MeasuredSearchTree< K, V > |
| a DICTIONARY on a binary search tree whose nodes keep the size, the depth and the height of their subtree More... | |
| struct | ADS::MeasuredSearchTree< K, V >::Measures |
| the measures of a node More... | |
| struct | ADS::MeasuredSearchTree< K, V >::Shape |
| the shape of the tree, for PathIterator More... | |
Namespaces | |
| namespace | ADS |
The binary search tree of Morin whose nodes keep the size, the depth and the height of their subtree (exercise 6.19 of Morin), beyond the book: a DICTIONARY [see ADT.h] with the find(x) of an SSet (lowerBound()) and the getLE(x) of exercise 6.14, whose insertions and removals keep the three measures of every node exact for a constant factor on the plain tree, \( O( h ) \) time.
An insertion adds a leaf of depth one more than its parent, and only the nodes on its path change their size (by 1) and possibly their height, which is recomputed from the children going up. The removal is the one point that needs care: the splice of the book lifts a whole subtree by one level, which would change the depth of all its nodes. Here the node that goes is always a leaf: the key removed is replaced by its successor (or its predecessor, if there is no right subtree), whose node is then emptied in the same way, and each step goes down, so that the chain ends at a leaf after at most \( h \) steps; no node changes its depth, and the sizes and the heights change only on the path of the leaf. A rotation could keep the size and the height but not the depth, which changes in a whole subtree (exercise 7.7 of Morin), and that is why the tree has none.
Morin gives no code for it; against his BinarySearchTree, which it extends, an insertion and a removal walk their path back up once, and a removal moves at most \( h \) pairs instead of one, the price of depths that never change.
The contracts [see Contract.h]: after insert() the key is there and after remove() it is not; the invariant, an \( O( n ) \) check at the level 3, is the order of the keys, the parents and the three measures of every node. The operations are defined in MeasuredSearchTree.tpp.