Documentation
Trie

Trie

The Trie data structure is a tree-like data structure used for storing a dynamic set of strings. It is commonly used for efficient retrieval and storage of keys in a large dataset.

Operations

  • insert: Adds a word to the Trie.
  • remove: Removes a word from the Trie.
  • has: Checks if a word exists in the Trie.
  • startsWith: Checks if a prefix exists in the Trie.
  • clear: Empty the trie.
  • toArray: Convert a trie to an array.

APIs

Constructor

import { Trie } from '@ultify/datastructure'

// Create an empty heap
const foo = new Trie()
console.log("Empty Trie:", foo.toArray())

// Create a trie from an array
const bar = new Trie('foo', 'bar')
console.log("Trie:", bar.toArray())

fromArray (static)

import { Trie } from '@ultify/datastructure'

// Create a Trie
const foo = Trie.fromArray(['foo', 'bar'])
console.log(foo.toArray())

toArray

import { Trie } from '@ultify/datastructure'

// Create a Trie
const foo = new Trie('foo', 'bar')

console.log(foo.toArray())

Ultify's trie also supports Iteration Protocol so you can iterate through every word of a Trie.

import { Trie } from '@ultify/datastructure'

// Create a Trie
const foo = new Trie('foo', 'bar')

for (const word of foo) {
  console.log(word)
}

Insert

import { Trie } from '@ultify/datastructure'

// Create an empty trie
const foo = new Trie()

foo.insert('foo')
foo.insert('bar')

console.log(foo.toArray())

Remove

import { Trie } from '@ultify/datastructure'

// Create an empty trie
const foo = new Trie()

foo.insert('foo')
foo.insert('bar')

console.log(foo.toArray())

foo.remove('bar')

console.log(foo.toArray())

Has

import { Trie } from '@ultify/datastructure'

// Create a Trie
const foo = new Trie('foo', 'bar')

console.log(foo.has('foo'))

Starts With

import { Trie } from '@ultify/datastructure'

// Create a Trie
const foo = new Trie('foo', 'bar')

console.log(foo.startsWith('fo'))

Clear

import { Trie } from '@ultify/datastructure'

// Create a Trie
const foo = new Trie('foo', 'bar')
console.log(foo.toArray())

foo.clear()
console.log(foo.toArray())

Node Count (readonly)

import { Trie } from '@ultify/datastructure'

// Create a Trie
const foo = new Trie('foo', 'bar')

console.log(foo.nodeCount)

Word Count (readonly)

import { Trie } from '@ultify/datastructure'

// Create a Trie
const foo = new Trie('foo', 'bar')

console.log(foo.wordCount)