Skip to content

Latest commit

 

History

History
292 lines (147 loc) · 5.63 KB

Trie.md

File metadata and controls

292 lines (147 loc) · 5.63 KB

@kamilmielnik/trie


@kamilmielnik/trie / Trie

Class: Trie

Defined in: Trie.ts:18

A class representing the Trie data structure.

Constructors

new Trie()

new Trie(root): Trie

Defined in: Trie.ts:52

Creates a new Trie with optionally given root Node.

Parameters

root

Node = {}

Root Node of the Trie to be created.

Returns

Trie

Properties

root

readonly root: Node

Defined in: Trie.ts:45

Represents the root Node of the Trie. It's not a copy. Mutate at your own risk.

Methods

add()

add(word): Node

Defined in: Trie.ts:62

Inserts given word into the Trie.

Parameters

word

string

Word to be inserted into the Trie.

Returns

Node

Node representing the end of the added word.


find()

find(prefix): undefined | Node

Defined in: Trie.ts:72

Finds Node representing given prefix in the Trie.

Parameters

prefix

string

Prefix to be found.

Returns

undefined | Node

Node representing a given prefix, undefined if there is no such node.


has()

has(word): boolean

Defined in: Trie.ts:82

Tells you whether given word is in the Trie.

Parameters

word

string

Word to be found.

Returns

boolean

true if given word is in the Trie, false otherwise.


hasPrefix()

hasPrefix(prefix): boolean

Defined in: Trie.ts:94

Tells you whether there are any words with given prefix in the Trie.

See: https://en.wikipedia.org/wiki/String_operations#Prefixes

Parameters

prefix

string

Prefix to be found.

Returns

boolean

true if there are any words with given prefix in the Trie, false otherwise.


remove()

remove(word): boolean

Defined in: Trie.ts:104

Removes given word from the Trie if it exists.

Parameters

word

string

Word to be removed.

Returns

boolean

true if the word was removed, false otherwise.


serialize()

serialize(): string

Defined in: Trie.ts:121

Converts the Trie into a string.

The inverse of deserialize.

It serializes 42.8 MB Polish dictionary down to 18.7 MB (-56%).

It serializes 1.9 MB English (US) dictionary down to 1.4 MB (-30%).

It serializes 3 MB English (GB) dictionary down to 2 MB (-32%).

Returns

string

String with serialized data.


toArray()

toArray(options?): Descendant[]

Defined in: Trie.ts:131

Finds all descendants of the Trie's root and returns them as an array.

Parameters

options?

TraverseOptions

See TraverseOptions.

Returns

Descendant[]

An array of descendants.


traverse()

traverse(callback, options?): void

Defined in: Trie.ts:141

Visits every descendant Node of the Trie and calls a callback.

Parameters

callback

TraverseCallback

Callback that will be called for each visited Node. Return true from callback to stop traversing.

options?

TraverseOptions

See TraverseOptions.

Returns

void


deserialize()

static deserialize(serialized): Trie

Defined in: Trie.ts:27

Creates a new Trie by deserializing given string.

The inverse of serialize.

Parameters

serialized

string

String with serialized data.

Returns

Trie

Trie representing deserialized data.


fromArray()

static fromArray(words): Trie

Defined in: Trie.ts:37

Creates a new Trie based on array of words.

Parameters

words

string[]

array of words to put in the Trie.

Returns

Trie

New Trie containing all given words.