# Advantages of Trie Data Structure

## Introduction

Trie is a 'Tree' based data structure that is mostly used to effectively search words from a collection of Strings or a Dictionary. Trie is made up of nodes, each of which represents one letter, and Trie uses these nodes to store the full word.

To know more about the Implementation of Trie in-depth, you can visit __Introduction and implementation of Trie__

Source: __Wikipedia__

## Advantages

Trie is very effective when it comes to managing a dictionary and searching for strings.

Following are the advantages of Trie Data Structure -

- Trie allows us to input and locate strings in O(L) time, where L is the length of a single word. Lookups are dependent on the depth of the tree. So if a tree is balanced, like in the case of BST with n number of keys, the lookup will take O(logn) time. As a result, a BST takes O(m log n) time in the worst case.

- Tries allow for ordered iteration, whereas iterating over a hash table produces a pseudorandom order determined by the hash function (the order of hash collisions is also implementation specified), which is typically useless.

- Helps in finding longest-prefix matching, assisting in the discovery of the key with the longest possible prefix of characters, all of which are unique.

- For short keys like integers and pointers, attempts are often quicker than hash tables since they skip the hash function.

- Compared to BST, tries take up less space. Because the keys are not explicitly saved, each key requires just an amortised fixed amount of space to be stored.

- Deletion is also a straightforward O(L) algorithm where L is the length of the word to be deleted.

### Time Complexity Analysis

#### Time Complexity O(L)

Because we iterate the full word and continue inserting the node for each node if it is not there, all operations, including Insert, Delete, and Search, take O(Word Length) time. And our word is added, erased, or searched as soon as we iterate the complete word.

#### Space Complexity O(L)

Because we are entering each word in the Trie and each letter of the word takes up some memory, implementing trie requires O(Alphabet Size * Number Of Words * Word Length)= (26* Number Of Words * Word Length)

## Frequently Asked Questions

**What is the advantage of Tries that you consider most important?**

We can insert and search any string in the trie in O(L) time where L is word length.

**What is the disadvantage of Trie?**

The main disadvantage of Trie is that it takes a lot of memory to store all the Strings. For each node, we have too many node pointers (equal to number of characters of the alphabet).

**What are some applications of Trie?**

The longest common prefix, pattern searching, autocomplete and implementation of the dictionary are some of the common applications of a Trie Data Structure.

## Key Takeaways

Finally, the conclusion about attempted data structures is that they are quicker, but they demand a lot of memory to store the strings.

If you wish to practise more about Tries and similar questions, you can visit our platform __CodeStudio__

Happy Coding!

Comments

## No comments yet

## Be the first to share what you think