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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Advantages of Trie Data Structure
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Gap Buffer Data Structure
- Introduction to the Probabilistic Data Structure
- Tango Tree Data Structure
- Design an efficient data structure for given operations
- Data Structure for Dictionary and Spell Checker?
- Ropes Data Structure (Fast String Concatenation)
- Inversion Count using Policy Based Data Structure
- Design a Queue data structure to get minimum or maximum in O(1) time
- Dynamic Disjoint Set Data Structure for large range values
- 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
- Introduction of Smart Pointers in C++ and It's Types
- Boggle | Set 2 (Using Trie)
- Trie | (Delete)
- Trie | (Display Content)
- Trie | (Insert and Search)
- Bottom-up traversal of a Trie
- Persistent Trie | Set 1 (Introduction)
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