【问题标题】:Tries and Suffix Trees implementation尝试和后缀树实现
【发布时间】:2026-01-05 06:55:01
【问题描述】:

我研究过 Tries 和 Suffix Trees,并希望实现相同的功能。请分享一些链接,我可以从中了解结构和实施的基本理念。

如果包含任何好的示例,将是一个加分项。

用 C 实现。

【问题讨论】:

标签: c data-structures trie


【解决方案1】:

C 算法库 (http://fragglet.github.io/c-algorithms/) 提供了Trie implementation in C。它是具有 BSD 风格许可证的开源软件。

可以在此处找到 C 中的后缀树实现:https://github.com/0xtonyxia/suffix-tree

希望对你有帮助。

【讨论】:

  • @Rasmi Ranjan Nayak:链接现已修复
【解决方案2】:
#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();  

}

【讨论】:

    【解决方案3】:

    以下是我发现非常有用的链接。

    6 小时后缀树讲座(第 3 讲至第 5 讲) Google SCICOMP Lecture 5 (Longest common substring problem: O(n) suffix tree, sort suffixes)

    广义后缀树实现 http://www.geeksforgeeks.org/generalized-suffix-tree-1/

    使用后缀树进行快速字符串搜索 http://marknelson.us/1996/08/01/suffix-trees/

    在*上查找 Ukkonen 算法。 注意:不能发布超过 2 个链接会导致声誉不足。

    【讨论】:

      【解决方案4】:
      #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;
      }
      

      【讨论】:

      • 您能提供一些上下文吗?