2016-07-08 10 views
0

Ich habe versucht, Suffix-Struktur mit C++ zu implementieren, indem Sie einige Beispiele online betrachten. Ich stoße auf das Zeigerproblem und kann das Problem nicht beheben. Jede Hilfe wird sehr geschätzt.Zeiger Problem beim Implementieren der Suffix-Struktur in 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; 
} 

Der Fehler geschieht bei insertIntoSuffixTree Methode innerhalb buildSuffixTree Methode. Mein Verständnis ist, dass ich meine Zeigeradresse verliere, mit der ich anfing. Irgendeine Idee, wie Sie dieses Problem umgehen können?

+1

Was lässt Sie denken, dass Sie "Ihre Zeigeradresse verlieren"? Haben Sie das in einem Debugger ausgeführt und einen unerwarteten Wert in einem Ihrer 'node' Zeiger gesehen? Sie sagten "Der Fehler tritt bei der' insertIntoSuffixTree' Methode auf "- * Welcher Fehler *? Schreiben Sie immer Fehlermitteilungen als Teil Ihrer Frage. – WhozCraig

+0

Ich verlor die Zeigeradresse nicht. Ich bekam für jedes Suffix, das hinzugefügt wurde, eine andere Startadresse. Das war der Fehler. Ich habe den Fehler herausgefunden und selbst behoben. Ich dachte, ich müsste einen Doppelzeiger oder so verwenden, aber es stellte sich heraus, dass es viel einfacher war. Den korrigierten Code unten anfügen, weiß nicht, ob irgendjemand es dennoch betrachten wird :) –

+1

[using namespace std; schlechte Idee] (http://stackoverflow.com/questions/1452721/why-isusing-namespace-std-in-c-considered-bad-practice) –

Antwort

0

Entschuldigung für etwaige Unannehmlichkeiten. Ich habe den Code wie folgt behoben:

/* 
* 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; 
} 
+0

Bitte lesen Sie den Link in meinem Kommentar oben –