Collection Kit

RedBlackTree

Tree

A RedBlackTree is a balanced binary search tree with an additional property that ensures the tree remains balanced during insertions and deletions, providing O(log n) time complexity for these operations.

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

Key Features

  • Color Properties: Each node is colored red or black, ensuring balance.
  • 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 { RedBlackTree } from "collection-kit";

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

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