AVL 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
- ✓AVL Tree runs in O(log n) time and O(n) space, in TypeScript as in every other language.
- ✓Nothing at run time beyond JavaScript. The type-level contribution is the comparator signature, which lets the tree accept any key type instead of being limited to the primitives the relational operators happen to handle.
- ✓Take a compare function of type (a: K, b: K) => number in the constructor and never use the relational operators on K.
- ✓The TypeScript trap: The type system can require that a comparator exists and has the right signature, but it cannot check that it is a valid ordering.
- ✓Erased. The generated code is the JavaScript version, and the comparator call is a normal function call that V8 will inline once the call site is monomorphic.
What AVL Tree actually is
AVL Tree in TypeScript: what you actually reach for
TypeScript implementation
interface AvlNode<K> {
key: K;
left: AvlNode<K> | null;
right: AvlNode<K> | null;
height: number;
}
export class AvlTree<K> {
private root: AvlNode<K> | null = null;
// The comparator is required. TypeScript can check its SHAPE but not
// that it is transitive, antisymmetric, or consistent with equality.
constructor(private readonly cmp: (a: K, b: K) => number) {}
insert(key: K): void {
this.root = this.insertAt(this.root, key);
}
has(key: K): boolean {
let node = this.root;
while (node !== null) {
const c = this.cmp(key, node.key);
if (c === 0) return true;
node = c < 0 ? node.left : node.right;
}
return false;
}
private height(n: AvlNode<K> | null): number {
return n === null ? 0 : n.height;
}
private update(n: AvlNode<K>): void {
n.height = 1 + Math.max(this.height(n.left), this.height(n.right));
}
private rotateRight(y: AvlNode<K>): AvlNode<K> {
const x = y.left!;
y.left = x.right;
x.right = y;
this.update(y);
this.update(x);
return x;
}
private rotateLeft(x: AvlNode<K>): AvlNode<K> {
const y = x.right!;
x.right = y.left;
y.left = x;
this.update(x);
this.update(y);
return y;
}
private insertAt(node: AvlNode<K> | null, key: K): AvlNode<K> {
if (node === null) return { key, left: null, right: null, height: 1 };
const c = this.cmp(key, node.key);
if (c < 0) node.left = this.insertAt(node.left, key);
else if (c > 0) node.right = this.insertAt(node.right, key);
else return node;
this.update(node);
const b = this.height(node.left) - this.height(node.right);
if (b > 1 && this.cmp(key, node.left!.key) < 0) return this.rotateRight(node);
if (b < -1 && this.cmp(key, node.right!.key) > 0) return this.rotateLeft(node);
if (b > 1) { node.left = this.rotateLeft(node.left!); return this.rotateRight(node); }
if (b < -1) { node.right = this.rotateRight(node.right!); return this.rotateLeft(node); }
return node;
}
}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 AVL Tree?
Nothing at run time beyond JavaScript. The type-level contribution is the comparator signature, which lets the tree accept any key type instead of being limited to the primitives the relational operators happen to handle.
What is the most common TypeScript mistake when implementing AVL Tree?
The type system can require that a comparator exists and has the right signature, but it cannot check that it is a valid ordering. A comparator that is not transitive, or that is not antisymmetric, or that returns zero for values you consider distinct, type-checks perfectly and corrupts the tree at run time. Note also that constraining K extends number | string does not make the relational operators safe, because a union of both still compares a number against a string at run time with the usual coercion.
How does AVL Tree actually perform in TypeScript?
Erased. The generated code is the JavaScript version, and the comparator call is a normal function call that V8 will inline once the call site is monomorphic. 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.