I have a homework, done the most part of it but i'm stuck at a point. I have to search through a binary tree and find a keyword, if the keyword doesn't show up I have to find the lexicographically next string at the tree witch has as a prefix the keyword I wanted to find, until no other string satisfies the previous criteria.
The code bellow is for searching after I haven't found the exact word.
int successor(TreeNode *v,char* x){
int lenght = strlen(x);
printf("%d\n", lenght);
if (v != NULL) {
if (strncmp(x , v->key, lenght) == 0)
{
// found
printf("%s, %d\n", v->key, v->appears);
}
else if (strncmp(x , v->key, lenght) < 0)
return successor(v->left, x);
else if (strncmp( x , v->key, lenght) > 0)
return successor(v->right, x);
else
printf("Query string not found.\n");
}
}
else return 0; }
EXAMPLE
If I have the words: tree traversal trees
tree <---(not root)
traversal trees
If I search: "tr"
I get only traversal back.
I can't go left or right after traversal cause is a leaf, and i can't find a way to show up tree and trees too.
I have tried some things but it didn't work out so now I'm asking you, besides that I don't even know how to handle the lexicographically next keyword or what I have to do with it!
Any help appreciated! :D