Trie
About
A General Tree specifically used for efficient retrieval of strings, commonly used in autocomplete features.
Each node can have any number of children.
Searching an element
Inserting an element
Starting a the root of Trie.
Grab one character of the string being added. (From left to right, in most Languages)
Check if the current node has this character.
If it doesn't add the new node with this character.
Set the current node to this node and Repeat 2.
The last node which refers to the last character of the word being added, can have a flag like
isWordto inform that this node completes a word on theTrie.
Deleting an element
Recursevely walk the Tree until the end of the deleted string.
Delete the nodes on the
Post Sectionof the Recursion, that have only one child.
In some scenarios no nodes will be deleted only the isWord will be set to false.
Last updated