Trie

class pydatastructs.Trie

Represents the trie data structure for storing strings.

Examples

>>> from pydatastructs import Trie
>>> trie = Trie()
>>> trie.insert("a")
>>> trie.insert("aa")
>>> trie.strings_with_prefix("a")
['a', 'aa']
>>> trie.is_present("aa")
True
>>> trie.delete("aa")
True
>>> trie.is_present("aa")
False

References

1

https://en.wikipedia.org/wiki/Trie

insert(string: str) None

Inserts the given string into the trie.

Parameters

string (str) –

Returns

Return type

None

is_present(string: str) bool

Checks if the given string is present as a prefix in the trie.

Parameters

string (str) –

Returns

  • True if the given string is present as a prefix;

  • False in all other cases.

delete(string: str) bool

Deletes the given string from the trie.

Parameters

string (str) –

Returns

  • True if successfully deleted;

  • None if the string is not present in the trie.

strings_with_prefix(string: str) list

Generates a list of all strings with the given prefix.

Parameters

string (str) –

Returns

strings – The list of strings with the given prefix.

Return type

list

root
__module__ = 'pydatastructs.strings.trie'