@kamilmielnik/trie
Ƭ Descendant: Object
Name | Type |
---|---|
node |
Node |
prefix |
string |
Ƭ TraverseCallback: (descendant
: Descendant
) => boolean
| void
▸ (descendant
): boolean
| void
Name | Type |
---|---|
descendant |
Descendant |
boolean
| void
Ƭ TraverseOptions: Object
Name | Type | Description |
---|---|---|
prefix? |
string |
Set the prefix to be applied to all descendants. It should be the prefix represented by the Node at which traversing starts. Defaults to empty string. |
sort? |
boolean |
Set to true to visit Nodes in alphabetical order. Defaults to false. |
wordsOnly? |
boolean |
Set to true to only visit Nodes representing complete words. Defaults to false. |
• Const
CLOSE_PARENS: ")"
Represents end of a node in serialized format.
• Const
OPEN_PARENS: "("
Represents start of a node in serialized format.
▸ add(node
, word
): Node
Inserts given word
into given node
.
Name | Type | Description |
---|---|---|
node |
Node |
Node to insert the word to. |
word |
string |
Word to be inserted into node . |
Node representing the end of the added word.
▸ deserialize(serialized
): Node
Creates a new Node by deserializing given string.
The inverse of serialize.
Name | Type | Description |
---|---|---|
serialized |
string |
String with value returned by serialize. |
Instance of a root Node of deserialized string.
▸ find(node
, prefix
): undefined
| Node
Finds Node representing given prefix in given Node.
Name | Type | Description |
---|---|---|
node |
Node |
Node to look for prefix in. |
prefix |
string |
Prefix to be found. |
undefined
| Node
Node representing a given prefix, undefined if there is no such node.
▸ fromArray(words
): Node
Creates a new Node based on array of words.
Name | Type |
---|---|
words |
string [] |
New Node containing all given words.
Params
words - array of words to put in the Node.
▸ has(node
, word
): boolean
Tells you whether given word is in the Node.
Name | Type | Description |
---|---|---|
node |
Node |
Node to look for word in. |
word |
string |
Word to be found. |
boolean
true if given word is in the Node, false otherwise.
▸ hasPrefix(node
, prefix
): boolean
Tells you whether there are any words with given prefix in the Node.
See: https://en.wikipedia.org/wiki/String_operations#Prefixes
Name | Type | Description |
---|---|---|
node |
Node |
Node to look for prefix in. |
prefix |
string |
Prefix to be found. |
boolean
true if there are any words with given prefix in the Node, false otherwise.
▸ nodeKeyComparator(key1
, key2
): number
Comparator that allows sorting Node keys alphabetically with the exception of "wordEnd" which should always come first.
Name | Type | Description |
---|---|---|
key1 |
string |
First key to compare. |
key2 |
string |
Second key to compare. |
number
-1 if key1 should precede key2, 0 if they're the same, 1 if key2 should precede key1.
▸ remove(node
, word
): boolean
Removes given word from given Node if it exists.
Name | Type | Description |
---|---|---|
node |
Node |
Node to remove word from. |
word |
string |
Word to be removed. |
boolean
true if the word was removed, false otherwise.
▸ serialize(node
): string
Converts given Node 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%).
Name | Type | Description |
---|---|---|
node |
Node |
Node to serialize. |
string
String with serialized data.
▸ toArray(node
, options?
): Descendant
[]
Finds all descendants of given Node and returns them as an array.
Name | Type | Description |
---|---|---|
node |
Node |
Node to look for descendants in. |
options? |
TraverseOptions |
See TraverseOptions. |
An array of descendants.
▸ traverse(node
, callback
, options?
): void
Visits every descendant Node of given Node and calls a callback.
Name | Type | Description |
---|---|---|
node |
Node |
Node to look for descendant Nodes in. |
callback |
TraverseCallback |
Callback that will be called for each visited Node. Return true from callback to stop traversing. |
options |
TraverseOptions |
See TraverseOptions. |
void