Red-Black 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
- ✓Red-Black Tree runs in O(log n) time and O(n) space, in TypeScript as in every other language.
- ✓Nothing at run time. The type system can describe the colour and the shape of a node, and that is genuinely all it can do here.
- ✓Type the colour as a literal union of red and black rather than a boolean, so the invariants read as prose and a negated condition cannot quietly mean the opposite of what it says.
- ✓The TypeScript trap: It is worth being explicit about how little help you get. The properties that make a red-black tree correct are that no red node has a red child and that every root-to-leaf path contains the same number of black nodes.
- ✓Erased at compile time; the emitted code is the JavaScript version. The literal union costs nothing at run time because it compiles to plain string comparison.
What Red-Black Tree actually is
Red-Black Tree in TypeScript: what you actually reach for
TypeScript implementation
type Color = 'red' | 'black'; // a union, not a boolean: !isRed lies
interface RbNode<K, V> {
key: K;
value: V;
color: Color;
left: RbNode<K, V> | null;
right: RbNode<K, V> | null;
parent: RbNode<K, V> | null;
}
/**
* The type system CANNOT express either red-black invariant:
* 1. no red node has a red child
* 2. every root-to-leaf path has the same black-height
* Both are statements about the whole structure at run time. So this is
* the actual check, run it in tests, and do not mistake strict typing
* for a guarantee about the property that makes the tree correct.
*/
export function assertInvariants<K, V>(root: RbNode<K, V> | null): void {
if (root === null) return;
if (root.color !== 'black') throw new Error('root must be black');
const blackHeight = (node: RbNode<K, V> | null): number => {
if (node === null) return 1; // null leaves count as black
if (node.color === 'red') {
if (node.left?.color === 'red' || node.right?.color === 'red') {
throw new Error('red node ' + String(node.key) + ' has a red child');
}
}
const left = blackHeight(node.left);
const right = blackHeight(node.right);
if (left !== right) {
throw new Error('black-height mismatch at ' + String(node.key));
}
return left + (node.color === 'black' ? 1 : 0);
};
blackHeight(root);
}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 Red-Black Tree?
Nothing at run time. The type system can describe the colour and the shape of a node, and that is genuinely all it can do here.
What is the most common TypeScript mistake when implementing Red-Black Tree?
It is worth being explicit about how little help you get. The properties that make a red-black tree correct are that no red node has a red child and that every root-to-leaf path contains the same number of black nodes. Neither is expressible in TypeScript, because both are statements about a whole structure at run time rather than about the shape of a value. So the type system will happily accept a tree that violates both, and a codebase that feels safe because it is strictly typed has zero protection on the only thing that matters. Write the invariants as an assertion function and run it over the tree in tests instead: that is the check, and pretending the types are doing it is how the bugs survive.
How does Red-Black Tree actually perform in TypeScript?
Erased at compile time; the emitted code is the JavaScript version. The literal union costs nothing at run time because it compiles to plain string comparison. 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 trialBefore paying for Devin, check its student and free-tier options 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.