Collection Kit

Trie

Tree

A Trie (prefix tree) is a tree-like data structure used to store a dynamic set of strings, allowing for efficient retrieval of keys based on their prefixes.

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

Key Features

  • Prefix-Based Storage: Efficiently stores strings by sharing common prefixes.
  • Fast Search: Allows for quick lookups, insertions, and deletions.

Common Operations

Insert
Add a new string to the trie.
Search
Check if a string exists in the trie.
StartsWith
Check if any string in the trie starts with a given prefix.
Delete
Remove a string from the trie.

Example Code

import { Trie } from "collection-kit";

const trie = new Trie();
trie.insert("apple");
trie.insert("app");
trie.insert("application");

console.log(trie.search("app"));        // true
console.log(trie.startsWith("appl"));   // true
console.log(trie.search("banana"));     // false