#include <iostream>
#include <string.h>
using namespace std;
int high;
struct stnode
{
stnode* ptr[27];
int start;
int end;
stnode(int s,int e)
{
for (int i = 0; i < 27; ++i)
{
ptr[i]=NULL;
}
start=s;
end=e;
}
}*root=NULL;
stnode* fun(stnode* child,string str,int ind)
{
int s=child->start;
while(s<=child->end&&str.at(s)==str.at(ind))
{
s++;
ind++;
}
if(s<=child->end)
{
child->ptr[str.at(ind)-'a']=new stnode(ind,high);
if(str.at(s)=='$')
child->ptr[26]=new stnode(s,child->end);
else
child->ptr[str.at(s)-'a']=new stnode(s,child->end);
child->end=s-1;
return child;
}
else
{
if(child->ptr[str.at(ind)-'a'])
{
child->ptr[str.at(ind)-'a']=fun(child->ptr[str.at(ind)-'a'],str,ind);
return child;
}
else
{
child->ptr[str.at(ind)-'a']=new stnode(ind,high);
return child;
}
}
}
stnode* create(stnode* root,string str,int ind)
{
if(!root)
{
root=new stnode(ind,high);
return root;
}
if(str.at(ind)=='$')
{
root->ptr[26]=new stnode(ind,high);
return root;
}
if(!root->ptr[str.at(ind)-'a'])
{
root->ptr[str.at(ind)-'a']=new stnode(ind,high);
return root;
}
root->ptr[str.at(ind)-'a']=fun(root->ptr[str.at(ind)-'a'],str,ind);
return root;
}
void display(stnode* root,string str)
{
if(!root)
return;
if(root->start!=-1)
{
for(int i=root->start;i<=root->end;i++)
{
cout<<str.at(i);
}
cout<<"\n";
}
for(int i=0;i<27;i++)
{
display(root->ptr[i],str);
}
}
int main(int argc, char const *argv[])
{
string str;
cout<<"enter the string.\n";
cin>>str;
str=str+"$";
high=str.length()-1;
root=new stnode(-1,-1);
for(int i=str.length()-1;i>=0;i--)
{
root=create(root,str,i);
display(root,str);
cout<<"\n\n\n";
}
return 0;
}
3323117/try-data-structure-implementation-application-dictionary) –
Que diriez-vous de fermer cette question en choisissant une réponse? Puisque vous avez immédiatement posé la même question, celle-ci est définitivement redondante. – AnyOneElse
Les articles wikipedia sur Trie et Suffix arbre fournissent de bonnes explications et pseudocode pour Trie – stan0