Question

Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...

Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values.

Use the following prototype and data structure:

struct node {
    node *left;
    node *right;
    int val;
};

// First parameter:  pointer to a node
// Second parameter:  the value to find
bool searchValue(node *, int);

The function searchValue() should return true if the integer value in the tree, false otherwise. The first call to searchValue() should pass a pointer to the root of the tree.

No main() is required, only your implementation of searchValue.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
struct node {
    node *left;
    node *right;
    int val;
};

// First parameter:  pointer to a node
// Second parameter:  the value to find
bool searchValue(node *n, int num) {
    if (n == nullptr) {
        return false;
    } else {
        if (num == n->val) {
            return true;
        } else {
            return searchValue(n->left, num) || searchValue(n->right, num);
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a recursive function (C++) that searches a binary tree that is NOT ordered like a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT