This code is to show you the implementation of Trie Tree using C++ Class. To know more about the Trie Tree Data Structure, please read my post, Trie Tree Implementation… I’m sure you’ll like it..! 🙂 … I have no experience in OOP with C++. In fact, this is my first code in OOP with C++. So, do let me know if my implementation is bad..! 😛 … Compared to my previous code in the post where I talk about the Data Structure, this one comes with a little optimization to the insert() function.
Feel free to comment if you have any doubts..! Keep practicing..! Happy Coding..! 😀
8 thoughts on “Trie Tree using C++ Class”
Hi! you mentioned that insert must be recursive, but i dont see the recursion in there,,,, hope u can help me,
thanks your blog seems cool!
Hi Claraval..! 🙂 …. By “recursively”, I actually meant simply adding the nodes one-after-the-other… I wasn’t referrring particulary to a recusrive implementation… I have corrected my statement to avoid the ambiguity… The insert function is a simple iterative one… 🙂
I guess the program probably has problems with memory leaks. At least, I don’t see a destructor for class `TrieTreeNode` and `TrieTree` that free the memory allocated by `calloc` functions.
Sorry for the late reply… I am new to C++ OOP, so I haven’t had much occasion of practicing destructors… But it is an excellent suggestion, I will modify the code soon, thank you!… BTW… That’s one intriguing website you have! 😛
Hello can you please reply why trietree.root is necessary to pass as root is already there in class itself, i can’t get why the search works fine even when the root is not passed to it, but deletetion doesnot work.
I just added an argument root so as to create a generic function which will allow me to search from any node not just from the root. Using my function I could search only in a subtree by passing the root of the subtree. So yes you can remove that argument in both methods if you want to. Deletion should work without that argument, what was the error you got?
Thanks for the answer. 😀 Actually when i compile the code without passing root in search, the code works fine for search but the remove doesnot work(it doesn’t remove the word from the trie), and it works when i pass the root. One more thing I wanted to know is the occurences vector that you made can just be implemented with a variable , say int Occurences, so whenever i insert a word i just have to do temp->Occurences++ and during deletion also i can just use temp->Occurences– , so is it that the vector is used only for the lexicographic printing ? By the way your explanations are wonderful, I always enjoy reading algos from here 😀