-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Search_Tree.cpp
More file actions
68 lines (58 loc) · 1.63 KB
/
Copy pathBinary_Search_Tree.cpp
File metadata and controls
68 lines (58 loc) · 1.63 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;
// Define the structure for a BST node
struct Node {
int key;
Node* left;
Node* right;
Node(int item) : key(item), left(nullptr), right(nullptr) {}
};
// Function to search for a key in the BST
Node* search(Node* root, int key) {
if (root == nullptr || root->key == key)
return root;
if (root->key < key)
return search(root->right, key);
return search(root->left, key);
}
// Function to insert a new key into the BST
Node* insert(Node* root, int key) {
if (root == nullptr)
return new Node(key);
if (key < root->key)
root->left = insert(root->left, key);
else if (key > root->key)
root->right = insert(root->right, key);
return root;
}
// In-order traversal (prints keys in sorted order)
void inOrderTraversal(Node* root) {
if (root) {
inOrderTraversal(root->left);
cout << root->key << " ";
inOrderTraversal(root->right);
}
}
int main() {
Node* root = nullptr;
// Insert some keys into the BST
root = insert(root, 50);
insert(root, 30);
insert(root, 70);
insert(root, 20);
insert(root, 40);
// Search for a key
int searchKey = 40;
Node* result = search(root, searchKey);
if (result)
cout << "Key " << searchKey << " found in the BST." << endl;
else
cout << "Key " << searchKey << " not found in the BST." << endl;
// Print the keys in sorted order
cout << "In-order traversal of the BST: ";
inOrderTraversal(root);
cout << endl;
// Clean up memory (optional)
// TODO: Implement deletion if needed
return 0;
}