I am trying to implement trie data structure but I am getting wrong answers and errors in my code , please help me debug my code
#include <bits/stdc++.h>
using namespace std;
struct node{
bool isWordEnd;
node * child[26];
};
node* getNode()
{
node *new_node=new node();
new_node->isWordEnd=false;
for(int i=0;i<26;i++)
{
new_node->child[i]=NULL;
}
return new_node;
}
bool isEmpty(node *root)
{
for(int i=0;i<26;i++)
{
if(root->child[i])
{
return false;
}
}
return true;
}
node * insert(node *root,string word)
{
node *cur=root;
for(int i=0;i<word.length();i++)
{
if(!cur->child[word[i]-'a'])
{
cur->child[word[i]-'a']=getNode();
}
cur=cur->child[word[i]-'a'];
}
cur->isWordEnd=true;
return root;
}
bool search(node *root,string word)
{
node *cur=root;
for(int i=0;i<word.length();i++)
{
if(!cur->child[word[i]-'a'])
{
return false;
}
cur=cur->child[word[i]-'a'];
}
return (cur!=NULL && cur->isWordEnd);
}
node* remove(node * root, string word , int depth=0)
{
if(!root)
{
return NULL;
}
//last leaf letter
if(word.length()==depth)
{
if(root->isWordEnd)
{
root->isWordEnd=false;
}
if(isEmpty(root))
{
delete(root);
root=NULL;
}
return root;
}
//if it is not the leaf letter use recursion
root->child[word[depth]-'a']=remove(root->child[word[depth]-'a'],word,depth+1);
//if it is not empty and end of word is false
if(!isEmpty(root) && root->isWordEnd==false)
{
delete(root);
root=NULL;
}
return root;
}
int main()
{
node* root=getNode();
insert(root,"rohit");
insert(root,"rahul");
insert(root,"rohan");
cout<<search(root,"rohit")<<endl;
cout<<search(root,"lebron")<<endl;
remove(root,"roh");
cout<<search(root,"rohit")<<endl;
return 0;
}