-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary Search Tree all operations.cpp
176 lines (132 loc) · 3.92 KB
/
Binary Search Tree all operations.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
// constructor
Node(int value) {
data = value;
left = right = nullptr;
}
};
// function for inorder traversal
void inorderTraversal(Node* root) {
if (root == nullptr) return;
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}
// function for preorder traversal
void preorderTraversal(Node* root) {
if (root == nullptr) return;
cout << root->data << " ";
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// function for postorder traversal
void postorderTraversal(Node* root) {
if (root == nullptr) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
cout << root->data << " ";
}
// function to insert a node
Node* insert(Node* root, int value) {
if (root == nullptr)
return new Node(value);
if (value < root->data) {
root->left = insert(root->left, value);
} else {
root->right = insert(root->right, value);
}
return root;
}
// function to search for a value in the BST
Node* search(Node* root, int value) {
if (root == nullptr || root->data == value) return root;
if (value < root->data) {
return search(root->left, value);
} else { // fixed missing semicolon and conditional structure
return search(root->right, value);
}
}
// Function for min value node
Node* minValueNode(Node* node)
{
Node* current= node;
while(current && current->left !=nullptr )
{
current=current->left;
}
return current;
}
//Function to delete a node
Node* deletenode(Node* root, int value)
{
if (root==nullptr) return root;
if(value<root->data)
{
root->left= deletenode(root->left, value);
}
else if (value>root->data)
{
root->right=deletenode(root->right, value);
}
else
{
if(root->left==nullptr)
{
Node* temp = root->right;
delete root;
delete temp;
}
else if (root->right==nullptr)
{
Node* temp = root->left;
delete root;
delete temp;
}
Node* temp = minValueNode(root->right);
// Copy the in-order successor's content to this node
root->data = temp->data;
// Delete the in-order successor
root->right = deletenode(root->right, temp->data);
}
return root;
}
int main() {
Node* root = nullptr;
// Insert nodes into the BST
root = insert(root, 10);
root = insert(root, 25);
root = insert(root, 20);
root = insert(root, 5);
root = insert(root, 15);
cout << "The value of the root is: " << root->data << endl;
cout << "The value of the left is: " << root->left->data << endl;
cout << "The value of the right is: " << root->right->data << endl;
cout << "The inorder traversal value is: ";
inorderTraversal(root);
cout << endl;
cout << "The preorder traversal is: ";
preorderTraversal(root);
cout << endl;
cout << "The postorder traversal is: ";
postorderTraversal(root);
cout << endl;
// Call the search function
int valueToSearch = 15; // Specify the value to search for
Node* foundNode = search(root, valueToSearch);
// Check the result of the search
if (foundNode != nullptr) {
cout << "Value " << valueToSearch << " found in the BST." << endl;
} else {
cout << "Value " << valueToSearch << " not found in the BST." << endl;
}
// Call deleteNode function to delete a node
int valueToDelete = 25; // Specify the value to delete
root = deletenode(root, valueToDelete);
inorderTraversal(root);
return 0;
}