Question

Write a C++ code to recursively search a BST, with the root pointed to by root.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey there, I hope you and your loved ones are safe and sound.

If my answer helps you in any way, please upvote. I would really appreciate that.

Answer:

 #include <iostream> using namespace std; struct Node { int value; struct Node *left, *right; Node(int value) { this->value = value; left = right = NULL; } }; // Function to traverse the tree in preorder // and check if the given node exists in it bool checkNodeExists(struct Node* node, int key) { if (node == NULL) return false; if (node->value == key) return true; bool resOne = checkNodeExists(node->left, key); //Recursive call if(resOne) return true; bool resTwo = checkNodeExists(node->right, key); return resTwo; } // Driver Code int main() { struct Node* root = new Node(0); root->left = new Node(1); root->left->left = new Node(3); root->left->left->left = new Node(7); root->left->right = new Node(4); root->left->right->left = new Node(8); root->left->right->right = new Node(9); root->right = new Node(2); root->right->left = new Node(5); root->right->right = new Node(6); int key = 4; if (checkNodeExists(root, key)) cout << "YES Key Exists"; else cout << "NO, Key does not exist"; return 0; } 

Code snippet Language C++ Code content #include <iostream> using namespace std; struct Node { int value; struct Node *left, *

Code snippet Language C++ Code content if (resone) return true; bool restwo = checkNodeExists (node->right, key); return resT

Output:

- O X C:\Users\beris\Desktop\New folder aaa.exe YES Key Exists Process returned 0 (0x0)_execution time : 5.045 s Press any

Add a comment
Know the answer?
Add Answer to:
Write a C++ code to recursively search a BST, with the root pointed to by root.
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