我试图通过在线查看一些示例来使用c ++实现后缀树。我遇到指针问题,我无法解决它。非常感谢任何帮助。
/*
* Implement suffix array to print the maximum suffix substring in a string
* Algorithm:
* Build a suffix tree and find the lowest node with multiple children
*
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <ctype.h>
using namespace std;
struct node{
char ch;
node* next[26];
int treeHeight;
int childCount;
int end = 0;
};
void insertSuffixIntoTree(node*& root, string suffix){
if (suffix.size() == 0){
return;
}
char c = suffix[0];
int index = tolower(c) - 'a';
if (root->next[index] == NULL){
root->next[index] = new node();
for (int k = 0; k < 26; k++){
root->next[index]->next[k] = NULL;
}
root->next[index]->ch = tolower(c);
if (suffix.size() == 1){
root->next[index]->end = 1;
}
}
suffix.erase(suffix.begin());
insertSuffixIntoTree(root->next[index], suffix);
}
void buildSuffixTree(node* root, string str){
if (root == NULL) cout << "CRAP" << endl;
for (int i = str.size() - 1; i >= 0; i--){
string suffix = str.substr(i);
cout << "suffix is " << suffix << endl;
insertSuffixIntoTree(root, suffix);
}
}
void printSuffixTree(node* root, string str){
if (root->end){
cout << str << endl;
return;
}
for (int i = 0; i<26; i++){
while (root->next[i]){
str += root->ch;
return printSuffixTree(root->next[i],str);
}
}
}
int main() {
string str;
node* suffixRoot = new node();
suffixRoot->ch = ' ';
for (int i = 0; i < 26; i++){
suffixRoot->next[i] = NULL;
}
cout << "enter the string" << endl;
cin >> str;
buildSuffixTree(suffixRoot, str);
//string result = findMaxSuffix(suffixRoot,str);
//cout<<"result is "<<result<<endl;
string result = "";
printSuffixTree(suffixRoot,result);
getchar();
return 0;
}
错误发生在buildSuffixTree方法内的insertIntoSuffixTree方法中。我的理解是我失去了我开始时的指针地址。有关如何解决这个问题的任何想法?
答案 0 :(得分:0)
很抱歉给您带来不便。我修改了代码,如下所示:
/*
* Implement suffix array to print the maximum suffix substring in a string
* Algorithm:
* Build a suffix tree and find the lowest node with multiple children
*
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <ctype.h>
using namespace std;
struct node{
char ch;
node* next[26];
int treeHeight;
int childCount;
int end = 0;
};
void insertSuffixIntoTree(node* root, string suffix){
if (suffix.size() == 0){
return;
}
char c = suffix[0];
int index = tolower(c) - 'a';
if (root->next[index] == NULL){
root->next[index] = new node();
for (int k = 0; k < 26; k++){
root->next[index]->next[k] = NULL;
}
root->next[index]->ch = tolower(c);
if (suffix.size() == 1){
root->next[index]->end = 1;
}
}
suffix.erase(suffix.begin());
insertSuffixIntoTree(root->next[index], suffix);
}
void buildSuffixTree(node* root, string str){
if (root == NULL) cout << "CRAP" << endl;
for (int i = str.size() - 1; i >= 0; i--){
string suffix = str.substr(i);
cout << "suffix is " << suffix << endl;
insertSuffixIntoTree(root, suffix);
}
}
bool checkEmptyVector(node * leaf){
for (int i = 0; i < 26; i++){
if (leaf->next[i] != NULL){
return false;
}
}
return true;
}
void printSuffixTree(node* root, string str){
if (root->end){
cout << str << endl;
}
if (checkEmptyVector(root)){
return;
}
for (int i = 0; i<26; i++){
//cout << "inside for loop, i is " << i << endl;
while (root->next[i]){
str += root->next[i]->ch;
printSuffixTree(root->next[i],str);
break;
}
}
}
int main() {
string str;
node* suffixRoot = new node();
suffixRoot->ch = ' ';
for (int i = 0; i < 26; i++){
suffixRoot->next[i] = NULL;
}
cout << "enter the string" << endl;
cin >> str;
buildSuffixTree(suffixRoot, str);
//string result = findMaxSuffix(suffixRoot,str);
//cout<<"result is "<<result<<endl;
string result = "";
printSuffixTree(suffixRoot,result);
getchar();
return 0;
}