Collection Kit

TernarySearchTree

Tree

A TernarySearchTree is a trie-like data structure that allows for efficient retrieval of strings. Each node has three children, representing less than, equal to, and greater than the current character.

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

Key Features

  • Efficient String Storage: Allows for efficient prefix searches and string retrieval.
  • Space Optimization: Uses less space compared to traditional tries.

Common Operations

Insert
Add a new string to the TernarySearchTree.
Search
Check if a string exists in the tree.
Delete
Remove a string from the tree.

Example Code

import { TernarySearchTree } from "collection-kit";

const tst = new TernarySearchTree();
tst.insert("cat");
tst.insert("cats");
tst.insert("dog");

console.log(tst.search("cat"));   // true
console.log(tst.search("car"));   // false