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

The binary tries of the integers of \( w \) bits, beyond the book: a SORTED SET of keys \( x \in \{ 0 , \ldots , 2^w - 1 \} \), each with a value, in which lowerBound( x ) gives the smallest key \( \ge x \) (the successor, the find( x ) of the SSet of Morin's book), in time that depends on \( w \) and not on the number \( n \) of keys. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/Portable.h"
#include "ADS/dictionary/FlatHashTable.h"
#include "ADS/memory/ObjectPool.h"
#include <array>
#include <concepts>
#include <cstddef>
#include <iterator>
#include <limits>
#include <optional>
#include <type_traits>
#include <utility>
#include <variant>
#include "ADS/dictionary/impl/BinaryTrie.tpp"

Go to the source code of this file.

Classes

class  ADS::BinaryTrie< U, V, W >
 a binary trie of the integers of \( w \) bits with the list of the leaves and the jump pointers, a SORTED SET with a value per key More...
class  ADS::SimpleBinaryTrie< U, V, W >
 a binary trie of the integers of \( w \) bits with no list and no jump pointers (exercise 13.1 of Morin's book) More...

Namespaces

namespace  ADS

Concepts

concept  ADS::TrieWidth
 the number of bits of the keys of a trie on U: \( w \), if it is between 1 and the bits of U

Detailed description

The binary tries of the integers of \( w \) bits, beyond the book: a SORTED SET of keys \( x \in \{ 0 , \ldots , 2^w - 1 \} \), each with a value, in which lowerBound( x ) gives the smallest key \( \ge x \) (the successor, the find( x ) of the SSet of Morin's book), in time that depends on \( w \) and not on the number \( n \) of keys.

  • BinaryTrie is the digital search tree of section 13.1 of Morin's book: the node at depth \( i \) stands for a prefix of \( i \) bits, its child 0 and 1 for the prefixes one bit longer, and the leaves, all at depth \( w \), for the keys; the leaves are also a doubly linked list in increasing order, and each internal node with one child keeps a jump pointer to the smallest leaf below it, if its child 0 is missing, or to the largest, if its child 1 is. A search that falls off the trie at depth \( i \) thus reaches the successor through the jump pointer and at most one step of the list; lowerBound(), insert() and remove() take \( O( w ) \) time, and the trie has at most \( n w + 1 \) nodes. The leaves are the nodes themselves, their two children used as the links of the list, and the nodes come from an ObjectPool [see ObjectPool.h].
  • SimpleBinaryTrie is the trie of exercise 13.1 of Morin's book, with neither the list nor the jump pointers: a search that falls off the trie takes the leftmost path from the deepest node of its path where a larger branch departs, \( O( w ) \) time as well; its nodes are two pointers each, a leaf also a key and a value.

Against the code of Morin's book: the nodes of BinaryTrie come from a pool instead of one call to the allocator each, and those made one after another lie next to each other, so that the \( w \) nodes of a new path seldom cost an allocation and the walk from the root misses the cache less (where it is meant to be faster); the keys have any width up to 64 bits, where the book fixes \( w = 32 \); and remove() reads the links of the leaf before it frees it, while the code of the book reads them from the freed leaf. SimpleBinaryTrie, with two pointers per internal node instead of five, takes less memory, at the price of the walk down to the leftmost leaf.

The x-fast trie of section 13.2 shares the code of BinaryTrie [see XFastTrie.h]. The contracts [see Contract.h]: the keys are below \( 2^w \), insert() ensures that the key is there and remove() that it is not; the invariant (the depth of the leaves, the order of the list, the jump pointers and, for the x-fast trie, the tables of the prefixes) is checked in \( O( n w ) \) time at the level 3 while \( n \le 256 \), and at the leaf of the operation beyond. The operations are defined in BinaryTrie.tpp.

See also
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 13.1 and exercise 13.1
E. Fredkin, Trie memory, Communications of the ACM 3(9), 490-499, 1960, doi:10.1145/367390.367400
D. E. Willard, Log-logarithmic worst-case range queries are possible in space Theta(N), Information Processing Letters 17(2), 81-84, 1983, doi:10.1016/0020-0190(83)90075-3
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa