#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
typedef struct trie trie;
struct trie
{
char key;
trie *next,*children;
};
trie *newnode(char s)
{
trie *t=(trie *)malloc(sizeof(trie));
t->key=s;
t->next=t->children=NULL;
}
void insert(trie **t,char *s,int start)
{if(s[start]=='\0')
{
*t=newnode('#');
return;
}
if(*t==NULL)
{
*t=newnode(s[start]);
insert(&(*t)->children,s,start+1);
}
if((*t)->key==s[start])
insert(&(*t)->children,s,start+1);
else
insert(&(*t)->next,s,start);
}
bool search(trie *t ,char *s,int start)
{
if(t==NULL)
return false;
if(t->key=='#' && s[start]=='\0')
return true;
if(t->key!='#' && s[start]=='\0' || t->key=='#' && s[start]!='\0')
return false;
if(t->key==s[start])
return search(t->children,s,start+1);
else
return search(t->next,s,start);
return false;
}
/*void push(trie **t ,char *str)
{ int i=0;
for(i=0;i<strlen(str);i++)
insert(t,str[i]);
}*/
main()
{ int i=0;
trie *t=NULL;
char ch='y';
while(ch=='y')
{
{char str[20];
fflush(stdin);
printf("Enter the word ");
gets(str);
insert(&t,str,0);
}
// push(&t,str);
fflush(stdin);
printf("more y/n ::");
ch=getchar();
}
ch='y';
while(ch=='y')
{char str[20];
fflush(stdin);
printf("Enter the string you want to search::");
gets(str);
fflush(stdin);
if(search(t,str,0))
printf("Found");
else
printf("Not Found");
printf("\n more y/n ::");
scanf("%c",&ch);
}
getch();
}