So I'm implementing a trie to store words in a dictionary file. I've implemented the insert operation; now I'm trying to print lexicograpically. I'm close to getting it, but I have a small problem that I'm not sure how to fix. I'm also trying to keep in mind the speed of my program, which is why I've opted for a trie over an array or a linked list. Here is what a single node looks like:
struct node {
int end;
int occurrences;
int superwords;
struct node* child[26];
};
"end" indicates the completion of a word (for example, end == 1 at letter 'k' in the word book; this prevents confusion in checking whether or not a word has actually been inserted in the tree).
Here's the method:
void preorder(struct node *follow, char hold[200], int s){
int i = 0;
if(follow == NULL){
return;
}
for(i = 0; i < 26; i++){
if(follow->child[i] == NULL){
continue;
}
else{
printf("%c",'a'+i);
hold[s] = 'a'+i;
s++;
if(follow->child[i]->end == 1){
printf("\n");
hold[s] = '\0';
printf("%s", hold);
}
preorder(follow->child[i], hold, s);
}
}
return;
}
The words I've inserted are: boo, book, booking, john, tex, text. They should be printed in that order and line separated. My output looks like:
boo
book
booking
bookingjohn
bjohntex
bjtext
bjtext
I know this probably has something to do with my "hold" array, which stores the prefixes of words so they don't get lost. I need to set the index back to zero somewhere to indicate the completion of a prefix and all its associated words (boo, book, booking being a good example) but haven't been successful. Any help would be much appreciated and I would be glad to further clarify my thought process.