Write C code to search for a value in a binary search tree (BST).

October 28, 2007 · Filed Under Placement Questions, Tree 



Don't want to miss a single bit? Subscribe By Email for Daily Jobs

Enter your email address :

All the latest from JobsAdda on Jobsadda.com delivered to your mailbox everyday.

Here are a few C programs….

mynode *search(int value, mynode *root)
{
while(root!=NULL && value!=root->value)
{
root = (value < root->value)?root->left:root->right;
}

return(root);
}

Here is another way to do the same

mynode *recursive_search(int item, mynode *root)
{
if(root==NULL || item == root->value){return(root);}
if(iteminfo)return{recursive_search(item, root->left);}
return{recursive_search(item, root->right);}
}

Related Articles


Get Latest JobsAdda.com Jobs, news & updates  via Email