Tries is a tree that stores strings. Maximum number of children of a node is equal to size of alphabet. Trie supports search, insert and delete operations in O(L) time where L is length of key.
Hashing:- In hashing, we convert key to a small value and the value is used to index data. Hashing supports search, insert and delete operations in O(L) time on average.
Self Balancing BST : The time complexity of search, insert and delete operations in a self-balancing Binary Search Tree (BST) (like Red-Black Tree, AVL Tree, Splay Tree, etc) is O(L Log n) where n is total number words and L is length of word. The advantage of Self balancing BSTs is that they maintain order which makes operations like minimum, maximum, closest (floor or ceiling) and k-th largest faster. Please refer Advantages of BST over Hash Table for details.
Why Trie? :-
- With Trie, we can insert and find strings in O(L) time where L represent the length of a single word. This is obviously faster than BST. This is also faster than Hashing because of the ways it is implemented. We do not need to compute any hash function. No collision handling is required (like we do in open addressing and separate chaining)
- Another advantage of Trie is, we can easily print all words in alphabetical order which is not easily possible with hashing.
- We can efficiently do prefix search (or auto-complete) with Trie.
Issues with Trie :-
The main disadvantage of tries is that they need a lot of memory for storing the strings. For each node we have too many node pointers(equal to number of characters of the alphabet), if space is concerned, then Ternary Search Tree can be preferred for dictionary implementations. In Ternary Search Tree, the time complexity of search operation is O(h) where h is the height of the tree. Ternary Search Trees also supports other operations supported by Trie like prefix search, alphabetical order printing, and nearest neighbor search.
The final conclusion is regarding tries data structure is that they are faster but require huge memory for storing the strings.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Trie Data Structure using smart pointer and OOP in C++
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Gap Buffer Data Structure
- Tango Tree Data Structure
- Introduction to the Probabilistic Data Structure
- Applications of tree data structure
- Data Structure for Dictionary and Spell Checker?
- Design an efficient data structure for given operations
- Ropes Data Structure (Fast String Concatenation)
- Inversion Count using Policy Based Data Structure
- Dynamic Disjoint Set Data Structure for large range values
- Design a Queue data structure to get minimum or maximum in O(1) time
- Design a data structure that supports insert, delete, getRandom in O(1) with duplicates
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Trie | (Delete)
- Boggle | Set 2 (Using Trie)
- Search in a trie Recursively
- Trie | (Display Content)
- Insertion in a Trie recursively
- Trie | (Insert and Search)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.