#include<iostream>
#include <cstdio>
#include<string>
using namespace std;
typedef struct node node;
struct node
{
int value;
node * child[26];
}*root;
node * newnode()
{
node * nn;
nn=(node*)malloc(sizeof(node));
nn->value=0;
for(int i=0;i<26;i++)
{
nn->child[i]=NULL;
}
return nn;
}
bool insert(string s)
{
node * temp=root;
bool xx=false;
for(int level=0;level<s.length();level++)
{
int index=s[level]-'0';
temp=temp->child[index];
if(temp==NULL)
{
cout<<"here"<<endl;
temp=newnode();
xx=true;
}
if(temp->value==1)
{
return false;
}
}
temp->value=1;
if(xx==true)
return true;
else
return false;
}
int main()
{
root=newnode();
string s;
int n;
int t;
bool p=true;
cin>>t;
while(t--)
{
root=NULL;
root=newnode();
cin>>n;
p=true;
while(n--)
{
cin>>s;
if(p==true)
{
bool z=insert(s);
if(z==false)
{
p=false;
}
}
if(root->child[9]==NULL)
cout<<"nnnnn"<<endl;
}
if(p==false)
{
cout<<"NO"<<endl;
}
else
cout<<"YES"<<endl;
}
return 0;
}
this is my code for tries. i am allocating memory to each node in the insert function. but when i check that it is value of the inserted node is null, then it says yes. problem is in insert function. i am calling this from main(). when i have inserted one string, then it should insert it again. please see whats can be the solution. thanks. i am entering "911" 2-3 times. and it is saying that root->child[9] is till NULL, which should not be there. thanks