Trie Data Structure using smart pointer and OOP in C++
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.