We will implement trie using smart pointers in C++ and OOP. Here, We have already discussed the implementation of trie data using recursion
In our implementation node of a trie look like :
The interface is the class showing the functionalities of our object class which has only pure virtual functions. Here, the concept interface discussed in more detail.
nullptrs stores NULL which suggests that there is no path for any other string from that position
Below image shows the insertion of “geeks” into an empty trie.
Below image shows the insertion of “geeksfor” into the trie. geeks p is already exiting in the trie. so, it overlaps.
Below image shows the insertion of “trie” into the trie
Why shared_ptr instead of all other smart pointers (auto_ptr, unique_ptr, weak_ptr)?
We are using shared_ptr due to the fact that while constructing the Trie we may encounter a situation where we may have to share the resource of a particular pointer to the other temporary shared_ptr to traverse Trie. In that shared_ptr is better than any other above mentioned pointers.
Below is the implementation of the above approach :
geeksfor: Is Present nothing: Not Present geeksforgeeks: Successfully deleted geeksforgeeks: Not Present
Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.
- Advantages of Trie Data Structure
- Commonly Asked OOP Interview Questions | Set 1
- Pointer to an Array | Array Pointer
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Introduction of Smart Pointers in C++ and It's Types
- Auto-complete feature using Trie
- Pattern Searching using a Trie of all Suffixes
- Find shortest unique prefix for every word in a given list | Set 1 (Using Trie)
- Longest Common Prefix using Trie
- Count of distinct substrings of a string using Suffix Trie
- Boggle | Set 2 (Using Trie)
- Trie memory optimization using hash map
- Sorting array of strings (or words) using Trie
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
- Program for assigning usernames using Trie
- Implement a Dictionary using Trie
- Count inversions in an array | Set 4 ( Using Trie )
- Print all possible combinations of words from Dictionary using Trie
- Count the number of words with given prefix using Trie
- Print Strings In Reverse Dictionary Order Using Trie
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.
Improved By : shubham_singh