# Bitwise trie with bitmap

A bitwise trie is a special form of trie where each node with its child-branches represents a bit sequence of one or more bits of a key. A bitwise trie with bitmap uses a bitmap to denote valid child branches.

## Tries and bitwise tries

A trie is a type of search tree where – unlike for example a B-tree – keys are not stored in the nodes but in the path to leaves. The key is distributed across the tree structure. In a "classic" trie, each node with its child-branches represents one symbol of the alphabet of one position (character) of a key.
In bitwise tries, keys are treated as bit-sequence of some binary representation and each node with its child-branches represents the value of a sub-sequence of this bit-sequence to form a binary tree (the sub-sequence contains only one bit) or n-ary tree (the sub-sequence contains multiple bits).
To give an example that explains the difference between "classic" tries and bitwise tries: For numbers as keys, the alphabet for a trie could consist of the symbols '0' .. '9' to represent digits of a number in the decimal system and the nodes would have up to 10 possible children.
There are multiple straight forward approaches to implement such a trie as physical data structure. To state two:
* A node can be represented having an array of child pointers for each symbol of the alphabet $\Sigma$ – an array of 10 pointers per node in the decimal number example. This gives a $O(|M|)$ lookup time with $|M|$ being the length of the key. But this isn't space efficient as each node preserves space for all possible child symbols even if there's no key that realizes that path.
* A node contains a list of (symbol, child pointer) tuples, ordered by symbol to allow a binary search through that list. This has a better space efficiency but the lookup time now is $O(|M|\cdot \log |\Sigma |)$. An ideal trie has an access time that is independent of the amount of keys stored.
These approaches get worse for larger alphabets, if, for example, the key is a string of Unicode characters. Treating the key as bit-sequence allows to have a fixed cardinality per node.

## Bitwise trie with bitmap

Bagwell presented a time and space efficient solution for tries named Array Mapped Tree (AMT). The Hash array mapped trie (HAMT) is based on AMT. The compact trie node representation uses a bitmap to mark every valid branch – a bitwise trie with bitmap. The AMT uses eight 32-bit bitmaps per node to represent a 256-ary trie that is able to represent an 8 bit sequence per node. With 64-Bit-CPUs (64-bit computing) a variation is to have a 64-ary trie with only one 64-bit bitmap per node that is able to represent a 6 bit sequence.
To determine the index of the child pointer of a node for such a given 6-bit value, the amount of preceding child pointers has to be calculated. It turns out that this can be implemented quite efficiently.

### Node traversal

The offset to find the index based on the current node index is the amount of least significant bits set in the bitmap before the target position plus one for the bitmap. The amount of least significant bits set can be calculated efficiently with constant time complexity using simple bit operations and a CTPOP (count population) operation that determines the number of set bits, which is available as Long.bitCount in Java. CTPOP itself can be implemented quite efficiently using a "bit-hack"
and many modern CPUs even provide CTPOP as a dedicated instruction treated by compilers as intrinsic function.

### CTPOP bit-hack implementation

How to use this principle for an universal index for database and Information retrieval applications is described in. A specialized and simplified solution demonstrating these concepts is shown below for an implementation of a 32-Bit-Integer set.

## Set implementation example
### Physical data structure

In this example implementation for a bitwise trie with bitmap, nodes are placed in an array of long (64-bit) integers. A node is identified by the position (index) in that array. The index of the root node marks the root of the trie.
Nodes are allocated from unused space in that array, extending the array if necessary. In addition, nodes, that are replaced, are collected in free lists and their space is recycled. Without this recycling, the data structure can be used to implement a persistent data structure by just keeping the previous root index and never overriding existing nodes but always creating a copy of a changed node.
Leaf nodes are inlined: Instead of having a child-pointer to a leaf node, the bitmap of the leaf node itself is stored.

### Set operations
#### Contains key

The get method tests, if a key is part of the set. The key is delivered as byte where each byte represents one 6-bit bit sequence of the key – so only 6 of the 8 bits per byte are used.

#### Clear (remove) key
### Set operators

Set operators for intersection (and), union (or) and difference (minus) are feasible using a flyweight pattern as shown below.
An interface represents physical nodes and "virtual" result nodes of an operator. Instances of this interface are created on demand during a trie traversal. Compound expressions, involving more than one operator, can be expressed directly by combining these operators as an operator can be used as argument (input) for another operator.

#### Intersection (AND)

The intersection operator is very efficient as it automatically performs pruning even over subexpressions. Nonrelevant child nodes don't have to be accessed because the bitmap and a bitwise AND operation allows to determine the result upfront. For example, calculating $\{1,2,3\}\cap (\{2,3,4\}\cup \{5,6,7\})=\{2,3\}$, the subexpression $\{2,3,4\}\cup \{5,6,7\}=\{2,3,4,5,6,7\}$ would not be materialized as intermediate result.

#### Ranges

Using the virtual node approach, range queries can be accomplished by intersecting a range generating virtual trie (see below) with another operator. So to determine which numbers of a set, say $\{10,20,30,40,50,60,61,62,63\}$, lay in certain range, say [10..50], instead of iterating through the set and checking each entry, this is performed by evaluating $\{10,20,30,40,50,60,61,62,63\}\cap \{10,..,50\}$.

### Usage example

The example shows the usage with 32-bit integers as keys.