2016-07-08 5 views
0

온라인에서 몇 가지 예를 살펴봄으로써 C++을 사용하여 접미어 트리를 구현하려고했습니다. 포인터 문제가 발생하여 고칠 수 없습니다. 어떤 도움이라도 대단히 감사합니다.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 메소드에서 발생합니다. 내 이해는 내가 시작한 포인터 주소를 잃어 버리고있다. 이 문제를 해결하는 방법에 대한 아이디어가 있습니까?

+1

"포인터 주소를 잃어 버렸습니다"라고 생각하는 이유는 무엇입니까? 디버거에서 이것을 실행하고'node' 포인터 중 하나에 예상치 못한 값이 있는지 확인하십시오. 당신은 "오류가'insertIntoSuffixTree' 메쏘드에서 일어나고 있다고 말했습니다."- * 무슨 오류 *? 항상 질문에 오류 메시지를 게시하십시오. – WhozCraig

+0

나는 포인터 주소를 잃지 않았다. 내가 추가 된 모든 접미사에 대해 다른 시작 주소를 얻고있었습니다. 그것은 오류였다. 나는 그 실수를 알아 냈고 그것을 스스로 고쳤다. 더블 포인터 같은 것을 사용해야 할 필요가 있다고 생각했지만, 그보다 훨씬 쉽습니다. 아래에 수정 된 코드를 첨부하면 어떤 사람이 보는지 알 수 없습니다. –

+1

[using namespace std; 나쁜 생각] (http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) –

답변

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

위의 내 코멘트에있는 링크를 읽으십시오 –