-
Notifications
You must be signed in to change notification settings - Fork 0
/
searching_in_binary_trees.cpp
47 lines (38 loc) · 1.26 KB
/
searching_in_binary_trees.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
// Define a structure for a binary tree node
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int value) : data(value), left(nullptr), right(nullptr) {}
};
// Function to search for a key in the binary tree
bool search(TreeNode* root, int key) {
if (root == nullptr) {
return false; // The tree is empty, or the key is not found
}
if (root->data == key) {
return true; // Key found in the current node
}
if (key < root->data) {
return search(root->left, key); // Search in the left subtree
} else {
return search(root->right, key); // Search in the right subtree
}
}
int main() {
TreeNode* root = new TreeNode(50);
root->left = new TreeNode(30);
root->right = new TreeNode(70);
root->left->left = new TreeNode(20);
root->left->right = new TreeNode(40);
root->right->left = new TreeNode(60);
root->right->right = new TreeNode(80);
int keyToSearch = 30;
if (search(root, keyToSearch)) {
std::cout << keyToSearch << " found in the binary tree." << std::endl;
} else {
std::cout << keyToSearch << " not found in the binary tree." << std::endl;
}
return 0;
}