|
ADS
Algorithms and Data Structures
|
The AVL tree of Adelson-Velsky and Landis, beyond the book [see SearchTree.h for the Red-Black tree of chapter 6]: a binary search tree whose every node has subtrees of heights differing by at most 1, so that its height is below \( 1.4405 \log_2( n + 2 ) \), a little more balanced than a Red-Black tree (whose bound is \( 2 \log_2( n + 1 ) \)), at the price of more rotations on the updates. More...
#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/tree/BinaryTreeAlgorithms.h"#include "ADS/tree/JoinAlgorithms.h"#include <algorithm>#include <concepts>#include <cstddef>#include <memory>#include <optional>#include <utility>#include "ADS/tree/impl/AVLTree.tpp"Go to the source code of this file.
Classes | |
| class | ADS::AVLTree< K, V > |
| a DICTIONARY on an AVL tree, with the operations based on join [see Joinable] More... | |
| struct | ADS::AVLTree< K, V >::Shape |
| the shape of the tree, for PathIterator More... | |
Namespaces | |
| namespace | ADS |
The AVL tree of Adelson-Velsky and Landis, beyond the book [see SearchTree.h for the Red-Black tree of chapter 6]: a binary search tree whose every node has subtrees of heights differing by at most 1, so that its height is below \( 1.4405 \log_2( n + 2 ) \), a little more balanced than a Red-Black tree (whose bound is \( 2 \log_2( n + 1 ) \)), at the price of more rotations on the updates.
Each node keeps its height; an insertion or a removal goes down recursively and, on the way back, restores the balance of each node by a single or a double rotation, in \( O( \log n ) \) time. It is a DICTIONARY [see ADT.h] with lookup(), insert() and remove(), and forEach() visits the pairs by increasing keys.
Each node keeps also the size of its subtree, so that the AVL tree has the operations based on join [see JoinAlgorithms.h]: split(), join(), join2(), extractRange(), the union, the intersection and the difference with another tree, the batches of insertions and of removals, filter() and mapReduce(), sequential or on the threads of an executor; its join goes down the spine of the higher tree to a subtree of the height of the other, in \( O( | h_l - h_r | + 1 ) \) time.
The contracts [see Contract.h]: insert() ensures that the key is there and remove() that it is not; the invariant is the order of the keys, the heights and the sizes stored and the balance of every node, an \( O( n ) \) check at the level 3. The operations are defined in AVLTree.tpp.