Collection Kit

AVLTree

Tree

An AVLTree is a self-balancing binary search tree where the difference in heights between left and right subtrees is at most one, ensuring O(log n) time complexity for search, insertion, and deletion operations.

Import Statement
import { AVLTree } from "collection-kit";

Key Features

  • Self-Balancing: Automatically maintains balance after insertions and deletions.
  • Binary Search Tree Properties: Follows the properties of binary search trees.

Common Operations

Insert
Add a new element while maintaining balance.
Remove
Delete an element while maintaining balance.
Search
Retrieve an element from the tree.

Example Code

import { AVLTree } from "collection-kit";

const tree = new AVLTree();
tree.insert(10);
tree.insert(20);
tree.insert(5);
tree.insert(15);

console.log("Search 15:", tree.search(15)); // true
tree.remove(20);
console.log("Search 20:", tree.search(20)); // false