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 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 or n-ary tree.
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 – an array of 10 pointers per node in the decimal number example. This gives a lookup time with 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 binary tree of tuples, ordered by symbol. This has a better space efficiency but the lookup time now is. 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. The Hash array mapped trie 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 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


long bitMap = mem;
long bitPos = 1L << value; // 6-bit-value
if
return false; // not found
long childNodeIdx = mem;

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 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.

int CTPOP

Set implementation example

Physical data structure

In this example implementation for a bitwise trie with bitmap, nodes are placed in an array of long integers. A node is identified by the position 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.

public class BBTrieSet

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.

public boolean get

Set (add) key


public boolean set
private long set

Clear (remove) key


public boolean clear
public long clear

Set operators

Set operators for intersection, union and difference 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 for another operator.

Flyweight interface


public interface BBTrieNode

public static class BBTrieNodeMem implements BBTrieNode

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
, the subexpression would not be materialized as intermediate result.


public static class BBTrieAnd implements BBTrieNode

Union (OR)


public static class BBTrieOr implements BBTrieNode

Difference (MINUS)


public static class BBTrieMinus implements BBTrieNode

Ranges

Using the virtual node approach, range queries can be accomplished by intersecting a range generating virtual trie with another operator. So to determine which numbers of a set, say, lay in certain range, say , instead of iterating through the set and checking each entry, this is performed by evaluating.

public static class BBTrieIntRange implements BBTrieNode

Usage example

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

public class BBTrieSetSample