Search Function

I have to create a search function for a binary tree. The tree is given below:

5
4 8
11 13 4
7 2 1

These are the specific instructions:
1. A function to search for a value in the binary tree given above. The function should return a pointer to that node if the value is found; otherwise the function should return a null pointer. The parameters should be a pointer to the root node of the binary tree and a value to be searched



My search function always returns a pointer that is pointing to NULL even though the values are in the tree. What am I doing wrong?
Last edited on
If either of the calls on lines 143 and 145 returns non-null you'll never know it, because you're ignoring the return value.
ok I've modified it and now it's returning the left nodes correctly. How do I fix the right? Is it even correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
BinaryTree *search_for_val(BinaryTree *bt, int val)
{
	if(!bt->isEmpty())
	{
		if(bt->getData() == val)
			return bt;
		else
			return search_for_val(bt->left(), val);
		//else 
			return search_for_val(bt->right(), val);
		return NULL;
	}
	return NULL;
}
Last edited on
Topic archived. No new replies allowed.