'Search for an element in a complete ternary tree
The function I need: return the index of the node in the tree if the key is found, return -1 otherwise.
My pseudo-code:
int search(Node node,int key){
if(node->key==key){
return node->index;
}
if(node->leftchild exists){
search(node->leftchild,key);
}
if(node->middlechild exists){
search(node->middlechild,key);
}
if(node->rightchild exists){
search(node->rightchild,key);
}
}
When I call the function I will initially pass the root:
int result = search(root,key);
The part I'm missing:
After every recursion call has been made, if the condition node->key==key was never met, return -1
What is the right way to add the part I'm missing in the function I wrote above in pseudo-code?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
