I have a node structure that has the following form
struct Node {
bool word;
int frecuencia;
char c;
Node* arr [ALPHABET];
};
The problem is that when you print the word shows a series of garbage characters and then typical Segmentation fault, I would greatly appreciate your help.
Ok thanks for the help, to put you in context I am using a trie data structure, the program receives a string (prefix), goes to a search function that assigns the node where the last character of the string is, that node is I pass it to the auto complete function that what it does or should do is find all the string suffixes entered.
The parameter that happened to autocomplete on its first call comes from the search function.
Node* act;
search(&word[0], act);
list<string> leftOver = autocomplete(act);
Here is the search function
bool search (char* word, Node* actual) {
Node* cursor = root;
int index;
for (int i = 0; i < strlen(word); i++) {
//hallar el indice del caracter en el alfabeto
for (int j = 0; j < ALPHABET; j++) {
if (tolower(word[i]) == chars[j]) {
index = j;
break;
}
}
if (cursor->arr[index] == NULL) {
actual = cursor;
return false;
}else {
cursor = cursor->arr[index];
}
}
actual = cursor;
if (cursor->word) {
cursor->frecuencia += 1;
return true;
}
return false;
}
This is the auto complete function
list<string> autocomplete (Node* cursor) {
list <string> words;
Node* children [ALPHABET];
if (cursor->arr != NULL) {
memcpy(children, cursor->arr, sizeof(Node*[ALPHABET]));
}else {return words;}
for (int i = 0; i < ALPHABET; i++) {
Node* child = children[i];
if (child != NULL) {
string word = "";
word += child->c;
if (child->word) {
words.push_back(word);
}
list <string> leftOver = autocomplete(child);
// cout << leftOver.size() << endl;
if (leftOver.size() > 0) {
for (int i = 0; i < leftOver.size(); i++) {
words.push_back(word + leftOver.back());
}
}
}
}
return words;
}