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

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

Detailed description

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.

See also
G. M. Adelson-Velsky, E. M. Landis, An algorithm for the organization of information, Soviet Mathematics Doklady 3, 1259-1263, 1962
D. E. Knuth, The Art of Computer Programming, vol. 3, 2nd ed., Addison-Wesley, 1998, section 6.2.3
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
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa