B-Tree Implementation in TypeScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓B-Tree runs in O(log n) time and O(n) space, in TypeScript as in every other language.
- ✓The same absence as JavaScript. What TypeScript adds is the ability to make the node invariant visible in the type, since a B-tree node is either a leaf holding only keys or an internal node holding keys and exactly one more child than it has keys.
- ✓Model the node as a discriminated union on a leaf boolean, so every function that walks the tree must handle both shapes and the compiler refuses to let you read children off a leaf.
- ✓The TypeScript trap: TypeScript cannot express the relationship children.length === keys.length + 1, because that is an arithmetic constraint between two runtime values and the type system has no dependent types.
- ✓Erased at compile time. The discriminated union costs one property read per node visit at run time, which is the same check the untyped version would have written by hand.
What B-Tree actually is
B-Tree in TypeScript: what you actually reach for
TypeScript implementation
type BTreeNode<K> =
| { readonly leaf: true; keys: K[] }
| { readonly leaf: false; keys: K[]; children: BTreeNode<K>[] };
// INVARIANT the type system cannot express:
// for an internal node, children.length === keys.length + 1
export class BTree<K> {
private root: BTreeNode<K> = { leaf: true, keys: [] };
constructor(
private readonly order: number,
private readonly cmp: (a: K, b: K) => number,
) {
if (order < 3) throw new RangeError('order must be at least 3');
}
has(key: K): boolean {
let node = this.root;
for (;;) {
const i = this.lowerBound(node.keys, key);
if (i < node.keys.length && this.cmp(node.keys[i]!, key) === 0) return true;
if (node.leaf) return false;
// The ONE place the unprovable invariant is relied on. Everything
// else reads through this method rather than indexing children.
node = node.children[i]!;
}
}
private lowerBound(keys: readonly K[], key: K): number {
let lo = 0;
let hi = keys.length;
while (lo < hi) {
const mid = (lo + hi) >>> 1;
if (this.cmp(keys[mid]!, key) < 0) lo = mid + 1;
else hi = mid;
}
return lo;
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe TypeScript-specific trap
Performance in TypeScript, beyond the O(log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which TypeScript type or library should I use for B-Tree?
The same absence as JavaScript. What TypeScript adds is the ability to make the node invariant visible in the type, since a B-tree node is either a leaf holding only keys or an internal node holding keys and exactly one more child than it has keys.
What is the most common TypeScript mistake when implementing B-Tree?
TypeScript cannot express the relationship children.length === keys.length + 1, because that is an arithmetic constraint between two runtime values and the type system has no dependent types. The core invariant of a B-tree is therefore entirely unchecked at compile time, and scattering children[i + 1]! across the traversal turns each access into an unverified promise. Concentrate the arithmetic in one insert and one split helper and keep the rest of the code reading through them.
How does B-Tree actually perform in TypeScript?
Erased at compile time. The discriminated union costs one property read per node visit at run time, which is the same check the untyped version would have written by hand. Time complexity is O(log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? GitHub Copilot free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.