hey i'm trying to implement two functions that use binary search trees.
one of them is to search for a specific key value that is stored in a node.
here is the code for the node.
what i want to be able to do in my main is to have a boolean variable to say whether or not the value is part of the tree.
i have two functions to check to the value. one private and one public.
this is the public one:
here's the public function:
so in my main i have this.
the above will give
whereas if i have it like this
it doesn't print out the line
which it should
the second function i need to implement is to find the height of the tree t.
i have this piece of pseudocode written but can't figure out how to implement it.
anyone help out a fellow fellow?
one of them is to search for a specific key value that is stored in a node.
here is the code for the node.
Code:
struct node
{
int key; // value inside the node
node * l; //pointer to the left child node
node * r; //pointer to the right child node
node()
{
key = -1;
l = r = NULL;
}
node( int x, node * ll, node * rr)
{
key = x;
l = ll;
r = rr;
}
};
what i want to be able to do in my main is to have a boolean variable to say whether or not the value is part of the tree.
i have two functions to check to the value. one private and one public.
this is the public one:
Code:
bool binst::search(int key, node* leaf)
{
if(leaf != NULL)
{
if(key == leaf->key)
return true;
if(key < leaf->key)
return search(key, leaf->l);
else
return search(key, leaf->r);
}
else
return false;
}
here's the public function:
Code:
bool binst::search(int key)
{
return search(key, head); //head relates to the root node.
}
so in my main i have this.
Code:
binst t ;
//inserting values on to the tree
t.insert(10); t.insert(3); t.insert(14); t.insert(6); t.insert(3);
t.insert(6); t.insert(12); t.insert(20);
bool boo ;
boo = t.search(20) ;
cout << "boo :" << boo ;
Code:
boo: 1.
whereas if i have it like this
Code:
boo = t.search(34) ; //ie NOT on tree.
Code:
boo: 0
the second function i need to implement is to find the height of the tree t.
i have this piece of pseudocode written but can't figure out how to implement it.
Code:
int height( BinaryTree Node t)
{
if t is a null tree
return -1;
hl = height( left subtree of t);
hr = height( right subtree of t);
h = 1 + maximum of hl and hr;
return h;
}
anyone help out a fellow fellow?
