0% found this document useful (0 votes)
23 views

41 - Data Structure and Algorithms - Tries

Uploaded by

laraib
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

41 - Data Structure and Algorithms - Tries

Uploaded by

laraib
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Data Structure and Algorithms -

Tries

Bhabani Shankar Pradhan


A trie is a type of a multi-way search tree, which is fundamentally used to
retrieve specific keys from a string or a set of strings. It stores the data in an
ordered efficient way since it uses pointers to every letter within the alphabet.

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.

There are three types of trie data structures −

 Standard Tries

 Compressed Tries

 Suffix Tries

The real-world applications of trie include − autocorrect, text prediction,


sentiment analysis and data sciences.
Basic Operations in 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

The deletion operation in a trie is performed using the bottom-up approach.


The element is searched for in a trie and deleted, if found. However, there are
some special scenarios that need to be kept in mind while performing the
deletion operation.

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

Searching in a trie is a rather straightforward approach. We can only move


down the levels of trie based on the key node (the nodes where insertion
operation starts at). Searching is done until the end of the path is reached. If
the element is found, search is successful; otherwise, search is prompted
unsuccessful.

You might also like