structs.Trie Extends
Class for a Trie datastructure. Trie data structures are made out of trees of Trie classes.

Inheritance

Constructor

goog.structs.Trie(opt_trie)

Parameters

opt_trie : goog.structs.Trie.<VALUE> | Object.<string, VALUE>>
Optional goog.structs.Trie or Object to initialize trie with.

Instance Methods

Public Protected Private
add(keyvalue)
Adds the given key/value pair in the trie. Throw an exception if the key already exists in the trie. O(L), where L is the length of the key.
Arguments:
key : string
The key.
value : VALUE
The value.
code »
clear()
Completely empties a trie of all keys and values. ~O(1)
code »
clone() !goog.structs.Trie.<VALUE>
Clones a trie and returns a new trie. O(N), where N is the number of nodes in the trie.
Returns: !goog.structs.Trie.<VALUE>  A new goog.structs.Trie with the same key value pairs.
code »
containsKey(key) boolean
Checks to see if a certain key is in the trie. O(L), where L is the length of the key.
Arguments:
key : string
A key that may be in the trie.
Returns: boolean  Whether the trie contains key.
code »
containsPrefix(prefix) boolean
Checks to see if a certain prefix is in the trie. O(L), where L is the length of the prefix.
Arguments:
prefix : string
A prefix that may be in the trie.
Returns: boolean  Whether any key of the trie has the prefix.
code »
containsValue(value) boolean
Checks to see if a certain value is in the trie. Worst case is O(N) where N is the number of nodes in the trie.
Arguments:
value : !VALUE
A value that may be in the trie.
Returns: boolean  Whether the trie contains the value.
code »
get(key) VALUE | undefined
Retrieves a value from the trie given a key. O(L), where L is the length of the key.
Arguments:
key : string
The key to retrieve from the trie.
Returns: VALUE | undefined  The value of the key in the trie, or undefined if the trie does not contain this key.
code »
getChildNode_(path) !goog.structs.Trie.<VALUE> | undefined
Traverse along the given path, returns the child node at ending. Returns undefined if node for the path doesn't exist.
Arguments:
path : string
The path to traverse.
Returns: !goog.structs.Trie.<VALUE> | undefined  No description.
code »
getCount() number
Returns the number of key value pairs in the trie. O(N), where N is the number of nodes in the trie. TODO: This could be optimized by storing a weight (count below) in every node.
Returns: number  The number of pairs.
code »
getKeyAndPrefixes(keyopt_keyStartIndex) !Object.<string, VALUE>
Retrieves all values from the trie that correspond to prefixes of the given input key. O(L), where L is the length of the key.
Arguments:
key : string
The key to use for lookup. The given key as well as all prefixes of the key are retrieved.
opt_keyStartIndex : ?number=
Optional position in key to start lookup from. Defaults to 0 if not specified.
Returns: !Object.<string, VALUE>  Map of end index of matching prefixes and corresponding values. Empty if no match found.
code »
getKeys(opt_prefix) !Array.<string>
Gets the keys of the trie. Not returned in any reliable order. O(N) where N is the number of nodes in the trie (or prefix subtree).
Arguments:
opt_prefix : string=
Find only keys with this optional prefix.
Returns: !Array.<string>  The keys in the trie.
code »
getKeysInternal_(keySoFarallKeys)
Private method to get keys from the trie. Builds the keys as it goes.
Arguments:
keySoFar : string
The partial key (prefix) traversed so far.
allKeys : !Array.<string>
The partially built array of keys seen so far.
code »
getValues() !Array.<VALUE>
Gets the values of the trie. Not returned in any reliable order. O(N) where N is the number of nodes in the trie. Calls getValuesInternal_.
Returns: !Array.<VALUE>  The values in the trie.
code »
getValuesInternal_(allValues)
Gets the values of the trie. Not returned in any reliable order. O(N) where N is the number of nodes in the trie. Builds the values as it goes.
Arguments:
allValues : !Array.<VALUE>
Array to place values into.
code »
isEmpty() boolean
Returns true if this trie contains no elements. ~O(1).
Returns: boolean  True iff this trie contains no elements.
code »
remove(key) !VALUE
Removes a key from the trie or throws an exception if the key is not in the trie. O(L), where L is the length of the key.
Arguments:
key : string
A key that should be removed from the trie.
Returns: !VALUE  The value whose key was removed.
code »
set(keyvalue)
Sets the given key/value pair in the trie. O(L), where L is the length of the key.
Arguments:
key : string
The key.
value : VALUE
The value.
code »
setAll(trie)
Adds multiple key/value pairs from another goog.structs.Trie or Object. O(N) where N is the number of nodes in the trie.
Arguments:
trie : !Object.<string, VALUE> | !goog.structs.Trie.<VALUE>
Object containing the data to add.
code »
setOrAdd_(keyvalueopt_add)
Helper function for set and add. Adds the given key/value pair to the trie, or, if the key already exists, sets the value of the key. If opt_add is true, then throws an exception if the key already has a value in the trie. O(L), where L is the length of the key.
Arguments:
key : string
The key.
value : VALUE
The value.
opt_add : boolean=
Throw exception if key is already in the trie.
code »

Instance Properties

childNodes_ :
This trie's child nodes.
Code »
value_ :
This trie's value. For the base trie, this will be the value of the empty key, if defined.
Code »

Package structs

Package Reference