41 - Data Structure and Algorithms - Tries
41 - Data Structure and Algorithms - Tries
Tries
The trie data structure works based on the common prefixes of strings. The
root node can have any number of nodes considering the amount of strings
present in the set. The root of a trie does not contain any value except the
pointers to its child nodes.
Standard Tries
Compressed Tries
Suffix Tries
The trie data structures also perform the same operations that tree data
structures perform. They are −
Insertion
Deletion
Search
Insertion
The insertion operation in a trie is a simple approach. The root in a trie does
not hold any value and the insertion starts from the immediate child nodes of
the root, which act like a key to their child nodes. However, we observe that
each node in a trie represents a singlecharacter in the input string. Hence the
characters are added into the tries one by one while the links in the trie act as
pointers to the next level nodes.
Deletion
Case 1 − The key is unique − in this case, the entire key path is deleted from
the node. (Unique key suggests that there is no other path that branches out
from one path).
Case 2 − The key is not unique − the leaf nodes are updated. For example, if
the key to be deleted is see but it is a prefix of another key seethe; we delete
the see and change the Boolean values of t, h and e as false.
Case 3 − The key to be deleted already has a prefix − the values until the prefix
are deleted and the prefix remains in the tree. For example, if the key to be
deleted is heart but there is another key present he; so we delete a, r, and t
until only he remains.
Search