#include <iostream>
using namespace std;
struct Node {
int val;
Node* left;
Node* right;
Node(int v) : val(v), left(nullptr), right(nullptr) {}
};
// Plain binary tree search (no order)
bool searchPlain(Node* root, int target) {
if (!root) return false;
if (root->val == target) return true;
// search left subtree
if (searchPlain(root->left, target)) return true;
// search right subtree
return searchPlain(root->right, target);
}
// BST search (uses order)
bool searchBST(Node* root, int target) {
if (!root) return false;
if (root->val == target) return true;
if (target < root->val) return searchBST(root->left, target);
else return searchBST(root->right, target);
}
int main() {
// Plain binary tree construction
Node* plainRoot = new Node(5);
plainRoot->left = new Node(3);
plainRoot->right = new Node(8);
plainRoot->left->left = new Node(7);
plainRoot->right->left = new Node(1);
plainRoot->right->right = new Node(9);
// BST construction
Node* bstRoot = new Node(5);
bstRoot->left = new Node(3);
bstRoot->right = new Node(8);
bstRoot->left->left = new Node(1);
bstRoot->right->right = new Node(9);
int target = 7;
cout << "Plain Binary Tree search for " << target << ": " << (searchPlain(plainRoot, target) ? "Found" : "Not Found") << endl;
cout << "BST search for " << target << ": " << (searchBST(bstRoot, target) ? "Found" : "Not Found") << endl;
return 0;
}
stop search if node is null
if (root->val == target) return true;
found target node
if (searchPlain(root->left, target)) return true;
search left subtree in plain binary tree
return searchPlain(root->right, target);
search right subtree if not found in left
if (target < root->val) return searchBST(root->left, target);
go left if target less than current node in BST
else return searchBST(root->right, target);
go right if target greater or equal in BST
Plain Binary Tree search for 7: Found
BST search for 7: Not Found