Storing and searching ordered data - a binary tree, continued
Knowing that we've built the binary tree to maintain an order of its
elements,
we exploit this property to find elements:
bool find_recursively(BINTREE *t, int wanted)
{
if(t != NULL) {
int order = (t->value - wanted);
if(order == 0) {
return true;
}
else if(order > 0) {
return find_recursively(t->left, wanted);
}
else {
return find_recursively(t->right, wanted);
}
}
return false;
}
|
|
bool find_iteratively(BINTREE *t, int wanted)
{
while(t != NULL) {
int order = (t->value - wanted);
if(order == 0) {
return true;
}
else if(order > 0) {
t = t->left;
}
else {
t = t->right;
}
}
return false;
}
|
|
Of note:
CITS2002 Systems Programming, Lecture 19, p19, 3rd October 2023.
|