Search binary tree

Greetings,

I attempting to search a binary tree for a provided value (10) returning either true or false but cannot complete the code... Any ideas where I went wrong?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 template< typename T >
	   static  bool  search(BinaryTreeNode<T> * p, T val)
        {
		   if ( root == NULL )     // empty tree
		   {
			   return false
		   }
		   else                    //  if the tree is not empty
		   {
			   int temp =  (p->data == val)? return true : return false;
			   int temp2 = search(p->left, val);
			   int temp3 = search(p->right, val);

		   }
       }


Called by
 
cout  <<  BinaryTree<int>::search( x.getRoot(), 10 )<<  "\n";
Last edited on
I dont think I've very far off here, any ideas?
Is the tree sorted?
I am getting the error:
Error 1 error LNK2001: unresolved external symbol "public: static int __cdecl gvfdg::BinaryTree<int>::search(class gvfdg::BinaryTreeNode<int> *,int)" (?search@?$BinaryTree@H@gvfdg@@SAHPAV?$BinaryTreeNode@H@2@H@Z)
Last edited on
No the tree is not sorted.
Is that function definition inside the class definition? In that case I don't think you should have template< typename T > above the function.
No the function def is not inside the class def, but I dont think I should remove the template <typename T>...i do get all sorts of errors if I do remove it.
Last edited on
In that case I think you should write
1
2
3
4
template< typename T >
bool  BinaryTree<T>::search(BinaryTreeNode<T> * p, T val)
{
	...

Oh yes, absolutely your right there - been looking at this for a while. Trying something like this now... but not getting a good return



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	   template< typename T >
	   bool  BinaryTree<T>::search(BinaryTreeNode<T> * p, T val)
        {
		   if ( root == NULL )     // empty tree
		   {
			   return false;
		   }
		   else                    //  if the tree is not empty
		   {
			   if (val < p->data)
				   return(search(p->left, val));
			   else return(search(p->right, val));

		   }
       }

Last edited on
else if (p->data < val) You should only do this if the tree is sorted. If it is not sorted you have to first call search on one of the child nodes and if that returns false you have to search the other child node as well.
Last edited on
getting the error
Error 1 error C2597: illegal reference to non-static member 'ghvg::BinaryTree<T>::root'



and

Error 2 error C3867: 'ghvg::BinaryTree<T>::root': function call missing argument list; use '&ghvg::BinaryTree<T>::root' to create a pointer to member


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	   template< typename T >
	   bool  BinaryTree<T>::search(BinaryTreeNode<T> * p, T val)
        {
		   if ( root == NULL )     // empty tree
		   {
			   return false;
		   }
		   else                    //  if the tree is not empty
		   {
			   if (val < p->data)
				   return(search(p->left, val));
			   else return(search(p->right, val));

		   }
       }
nvm, i got it... i had root in there and not p but the most recent code still isn't working...
Last edited on
Shouldn't it be p instead of root?
Got it...i took the dam == conditional out.
Thanks!
Topic archived. No new replies allowed.