i was able to make an add functin for a binary tree. in my code, i am able to add a string for the name, university and and int value for an id.
now, i need help being able to search the binary tree for a specified id, then return it if it is found. this is what i have so far,

void MyBST :: search(treenode* r, int& stid, bool& found)
 {          if (r == NULL)             
            found = false;   
            else if (stid < r->id)          
            search(r->left, stid, found);     
            else if (stid > r->id)          
            search(r->right, stid, found);  
            else     
      {            
            stid = r->id;      
            found = true;  
          }
       }

what this should do, is look at the root of the tree. if it is the item being searched, display it, if not check whether the item needed is bigger or smaller than the one being looked at. it keeps repeating this process untill the specified number is found. it doesnt seem to work for me, and any help would be greatly appreciated.
thank you

Make your code easier by declaring the function as a boolean function instead of having a found flag. Thus after that change it becomes seemingly simple :

bool MyBS::search(const Node* root, int key){
   if root is null return false;
   else if key < root.key
           search(root.leftChild,key);
   else if key > root.key
           search(root.rightChild,key);
   else return true; //root.key == key   
}

Edited 6 Years Ago by firstPerson: n/a

By implementing a binary search tree, it is presumed that the key value is sorted. In your case, the 'id' is sorted.

One thing to note about your implementation is that, the stopping condition have to be check first. If the 'id' is not found, then check if it is larger or smaller than current node's value.

Here's an example of the implementation:

bool MyBST::search(treenode* r, int &stid) 
{          
	if (r == NULL)
		return false;	// end of tree
	
	// stopping condition must be checked first
	if(stid == r->id)
	{
		return true;	// return true if found
	}
	// smaller than current - continue searching left node
	else if (stid < r->id)         
	{
		return search(r->left, stid, found);
	}
	// not smaller, must be larger - continue searching right node
	else
	{
		return search(r->right, stid, found);
	}
 }

The concept of recursive returns a bool value to the calling method. At the end of execution, the caller will be returned a 'true' or 'false' value indicating the occurrence of the value in the binary tree.

i tried implementing what you stated, but when it goes to run the code, the command line stays balck and then it crashes. here is the code that im using.

//prototype
 void search(treenode* r, int &stid);   


///////
///////
///////

//actual function
void MyBST::search(treenode* r, int &stid) 
{      
  
    if(stid == r->id)	
       {	
          cout<<r->id <<endl;
 
       }
    
       else if (stid < r->id)   
         {	
            search(r->left, stid);
         }
    
       else
         {	
             search(r->right, stid);
         }
 }


//after using the add funciton that i have working, i try to run this line of code
//
//
//
     MyBST studentinfo;
     studentinfo.add("Mike","UTPA", 456321); 
     studentinfo.add("Wallace", "TST", 123569);
        
     studentinfo.display(456321);

Have you tried running your codes in debug mode and step in to see what's happening behind?

Here are a few pointers of what can go wrong with your code:

1. At the top of the search function, treenode r has to be checked whether it is NULL value. Directly accessing its id (r->id) will cause the program to crash if its not initiated.

2. Make sure you initialize the left and right pointer of a treenode to NULL when declaring a new treenode. (that provides a way to determine if you have reached the leaf node)

3. Ensure that your data is stored in the correct place when using the add() function. (sorted in ascending order)

This question has already been answered. Start a new discussion instead.