Trie

Aim: Searching for a string in a tree in O(L)O(L) time (where LL is the length of the string), Performing Partial String Operations, Prefix Queries, etc.

Option 1: Just use a Tree!

If we store a string at every node of a tree, in which all the nodes are sorted lexicographically, then how much time do we take to find a string in the tree?

It takes O(L)O(L) to compare two strings of length LL. So, in the worst case it would take O(hL)O(hL)time to find a string in a tree, where hh is the height of the tree. We can do better than this!

Using Tries!

Store a letter (instead of a string) at every node of the tree. Then, you just have to compare each letter at every level of the trie. You need to have an end-of-string character to indicate that a string ends at that particular node. This can be done simply by using a variable at every node. Each node stores an array of its children. If you wish to have only lower-case strings in your trie, each node will have an array of length 26 for its children.

Note that it takes O(1)O(1) to find the child since you maintain an array of children at each node. For example, if you are searching for “c” as the next character, you know that if the node exists, it would be at index 2 of the children array. No need to loop through the entire array. Thus, O(1)O(1) lookup time.

Time

  • Tries are much faster than trees for string-comparisons (and other cool stuff too!).

  • Does not depend on the size of the total text.

  • Does not depend on the number of strings in the trie.

Space

  • Trie tends to use more space

  • BST and Trie use O(text size)O(\text{text size}) space.

  • Trie has more nodes and more overhead.

Applications of Tries

  • Searching, sorting and enumerating strings in a “dictionary”

  • Performing partial string operations inlcluding but not limited to:

    • Prefix queries: find all the strings that start with a specific substring

    • Long prefix: what is the longest prefix of a given string in the trie

    • Wildcards: find a string of the form pi??e where ? could be any letter

Basic Trie implementation

Supports insertion, search, prefix query, wildcards

Last updated

Was this helpful?