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
- Advantages of Trie Data Structure
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Gap Buffer Data Structure
- Tango Tree Data Structure
- Data Structure for Dictionary and Spell Checker?
- Design an efficient data structure for given operations
- Ropes Data Structure (Fast String Concatenation)
- Dynamic Disjoint Set Data Structure for large range values
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Smart Pointers in C++
- Trie | (Delete)
- Boggle | Set 2 (Using Trie)
- Trie | (Display Content)
- Trie | (Insert and Search)
- 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