I am trying to write code to search through an avl tree for a number. Part of it is working for example I enter the numbers 2,5,9 into the tree.
I then search for 9, then 5 then 2 and it tells me 2 is not in the tree.
I think I need point the search at the root every time it searches but not sure how to do that. The code just needs to say if the number is in the tree or not.
if (tree->info == item)
{
cout << "Item " << item << " is the root\n";
tree->left = NULL;
tree->right = NULL;
return;
}
What do you think this is doing? A search function should not modify the tree.
Presumably AVLtree is a class that represents a tree, so I'm not really sure why you're feeding it a TreeNode*
Generally a search function would signal to the calling code that it was successful or not. That's the point at which you should be using cout, not in the search function.