ArticleslgStudy

computer science

Bitwise trie with bitmap

Bitwise trie with bitmap is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Bitwise trie with bitmap rather than just read about it. In short: 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.

Bitwise trie with bitmap — main illustration
Bitwise trie with bitmap — illustration

Key takeaways

  • Bitwise trie with bitmap belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Bitwise trie with bitmap to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Bitwise trie with bitmap from memory before moving on to harder problems.

Reference excerpt

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 Σ – 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 binary tree of (symbol, child pointer) tuples, ordered by symbol. This has a better space efficiency but the lookup time now is O(|M|⋅log⁡|Σ|). 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 bitCount() in java.lang.Long 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.

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.

Set (add) key

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.

Flyweight interface

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}∩({2,3,4}∪{5,6,7})={2,3}, the subexpression {2,3,4}∪{5,6,7}={2,3,4,5,6,7} would not be materialized as intermediate result.

Union (OR)

Difference (MINUS)

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}∩{10,..,50}.

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

References

Illustrations

Bitwise trie with bitmap: Trie node with bitmap that marks valid child branches.
Trie node with bitmap that marks valid child branches.

Worked examples

Example 1 — a first encounter with Bitwise trie with bitmap

Start with the simplest possible case. Write down what Bitwise trie with bitmap claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Bitwise trie with bitmap before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Bitwise trie with bitmap ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Bitwise trie with bitmap

In research
Bitwise trie with bitmap appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Bitwise trie with bitmap in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Bitwise trie with bitmap is common in secondary-school and first-year university syllabi. It links to neighbouring topics Trees (data structures), so understanding it makes those chapters shorter.
In everyday life
Look for Bitwise trie with bitmap outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Bitwise trie with bitmap” →

How to study Bitwise trie with bitmap in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Bitwise trie with bitmap means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Bitwise trie with bitmap out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Bitwise trie with bitmap in simple terms?

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.

Why does Bitwise trie with bitmap matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Bitwise trie with bitmap?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Bitwise trie with bitmap.

Tags

  • Trees (data structures)

Keep exploring