Could somebody show me how will this look on C++? // EFFECTS : Searches the tree
ID: 3571726 • Letter: C
Question
Could somebody show me how will this look on C++?
// EFFECTS : Searches the tree rooted at 'node' for an element equivalent
// to 'query'. If one is found, returns a pointer to the node
// containing it. If the tree is empty or the element is not
// found, returns a null pointer.
//
// NOTE: This function must be tail recursive.
// HINT: Equivalence is defined according to the Compare functor
// associated with this instantiation of the BinarySearchTree
// template, NOT according to the == operator. Use the "less"
// parameter to compare elements.
// Two elements A and B are equivalent if and only if A is
// not less than B and B is not less than A.
static Node * find_impl(Node *node, const T &query, Compare less)
Explanation / Answer
static Node * find_impl(Node *node, const T &query, Compare less){
if(node == NULL){
return NULL;
}
else if(less(query, node->data)){
return find_impl(node->left, query, less);
}
else if(less(node->data, query)){
return find_impl(node->right, query, less);
}
else{
return node;
}
}