我一直在尝试编写后缀trie的C++代码,但是我希望此代码能够跟踪每个节点上字符或子字符串在后缀trie构造过程中出现的频率的计数器:记住那我只有4个字符A,C,G和TC++中的后缀Trie
下面的代码是我尝试但工作其无法正常工作:
#include<iostream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
struct SuffixTreeNode{
char c;
struct SuffixTreeNode* one;
struct SuffixTreeNode* two;
struct SuffixTreeNode* three;
struct SuffixTreeNode* four;
//int count;
};
SuffixTreeNode* CreateNode(char ch){
SuffixTreeNode* newnode=new SuffixTreeNode();
newnode->c=ch;
newnode->one=NULL;
newnode->two=NULL;
newnode->three=NULL;
newnode->four=NULL;
//count=0;
}
SuffixTreeNode* Insert(SuffixTreeNode* root,char ch){
if (root==NULL){
root=CreateNode(ch);
}
else if(ch=='a'){
root->one=Insert(root->one,ch);
}
else if(ch=='c'){
root->two=Insert(root->two,ch);
}
else if(ch=='g'){
root->three=Insert(root->three,ch);
}
else if(ch=='t') {
root->four=Insert(root->four,ch);
}
return root;
}
bool Search(SuffixTreeNode* root, int data){
if(root==NULL) return false;
else if (root->c==data) return true;
else if (root->c=='a')return Search(root->one,data);
else if (root->c=='c')return Search(root->two,data);
else if (root->c=='g')return Search(root->three,data);
else return Search(root->four,data);
}
int main(){
SuffixTreeNode* root=NULL;
char str;
root=Insert(root,'a');
root=Insert(root,'c');
root=Insert(root,'c');
root=Insert(root,'t');
root=Insert(root,'a');
root=Insert(root,'g');
cout<<"Enter character to be searched\n";
cin>>str;
if(Search(root,str)==true)cout<<"Found\n";
else cout<<"Not found\n";
}
而C标签刚刚滑入,对不对?不要为无关的,**不同的**语言添加标签。 – Olaf
坦率地说'C++'标签应该被删除。这不是C++ ...为什么你要包含c和C++版本的头文件?你也真的想要c或C++吗?它乞求使用对象。另外在一个更普遍的说明。你错过了一个问题。这是不好的说“这是我的破碎,调试它”,并被视为脱离主题根据条款:“*寻求调试帮助(”为什么不是这个代码工作?“)的问题必须包括所需的行为,特定问题或错误,以及在问题本身中重现问题所需的最短代码。*“所以,请帮助别人帮助你。 – luk32
@ luk32 honnestly,与'''''''cout'它绝对不是C + + –
Christophe