嘗試和後綴樹實現
回答
的Ç算法庫(http://fragglet.github.io/c-algorithms/)
實現提供了一個Trie implementation in C。它是開源的,具有BSD風格的許可證。
C中的一個後綴樹的實現可以在這裏找到:https://github.com/0xtonyxia/suffix-tree
我希望幫助。
沒有任何鏈接正在工作 – 2016-07-10 02:09:56
@Rasmi Ranjan Nayak:鏈接現在已修復 – 2016-07-14 18:54:13
#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至第5講) 谷歌SCICOMP講座56小時講座(最長公共子串:O(n)的後綴樹,排序後綴)
廣義後綴樹實現 http://www.geeksforgeeks.org/generalized-suffix-tree-1/
快速串與後綴樹 http://marknelson.us/1996/08/01/suffix-trees/
搜索查找維基百科Ukkonen算法。 注意:無法發佈超過2個鏈接會導致聲譽不夠。
#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;
}
您能提供一些上下文嗎? – Robert 2015-03-16 10:20:50
- 1. 在C#中尋找後綴樹實現?
- 2. Python中的後綴樹實現
- 3. 後綴樹和嘗試。有什麼不同?
- 4. 後綴樹和B樹
- 5. 簡短的Java後綴樹的實現和用法?
- 6. 基本前綴樹實現問題
- 7. 後綴數組實現
- 8. 什麼被認爲是最好的Java後綴樹實現?
- 9. 天真的後綴樹在Java中的實現
- 10. 後綴樹:最長的重複子字符串實現
- 11. 大數據集的廣義後綴樹Java實現
- 12. PHPUnit的嘗試給予空後綴
- 13. 後綴數組與後綴樹
- 14. 從後綴樹生成後綴
- 15. Trie與後綴樹與後綴數組
- 16. 後綴樹構造
- 17. Python:假實際嘗試之前嘗試和發現異常
- 18. 嘗試(和失敗)實現ActionBarSherlock
- 19. 嘗試和失敗,以實現WebGL的
- 20. 嘗試實現Haskell二叉樹搜索的路徑記錄
- 21. 嘗試使用樹枝從數據庫呈現實體地址
- 22. 填充廣義後綴樹和實施資源
- 23. 後綴樹VS嘗試 - 用簡單的英文,有什麼區別?
- 24. 嘗試以下發現實現
- 25. 在後綴樹中遍歷
- 26. 後綴樹搜索時間
- 27. 後綴樹如何工作?
- 28. Matlab中的後綴樹
- 29. 令牌後綴樹教程
- 30. 後綴樹是否唯一?
可能重複的[試圖數據結構實現.........應用程序 - 字典............](http://stackoverflow.com/questions/ 3323117/tries-data-structure-implementation-application-dictionary) – 2010-07-27 03:25:02
如何通過選擇答案來解決這個問題?既然你問了同一個問題,這個問題肯定是多餘的。 – AnyOneElse 2013-09-04 13:01:05
Trie和Suffix樹上的wikipedia文章爲Trie提供了很好的解釋和僞代碼 – stan0 2015-03-16 13:53:24