#include <iostream>
using namespace std;
struct Node {
int val;
Node* left;
Node* right;
Node(int x) : val(x), left(nullptr), right(nullptr) {}
};
// Insert helper to build tree for testing
Node* insert(Node* root, int val) {
if (!root) return new Node(val);
if (val < root->val) root->left = insert(root->left, val);
else root->right = insert(root->right, val);
return root;
}
// Search function returns true if val found
bool searchBST(Node* root, int val) {
Node* curr = root;
while (curr) {
if (val == curr->val) return true; // found value
else if (val < curr->val) curr = curr->left; // go left
else curr = curr->right; // go right
}
return false; // not found
}
int main() {
Node* root = nullptr;
int values[] = {8,3,10,1,6,14,4,7,13};
for (int v : values) root = insert(root, v);
int target = 7;
bool found = searchBST(root, target);
cout << (found ? "7 found" : "7 not found") << endl;
return 0;
}start searching from root node
continue until we reach a null pointer or find value
if (val == curr->val) return true;
check if current node has the target value
else if (val < curr->val) curr = curr->left;
move left if target is smaller
move right if target is larger