-
Notifications
You must be signed in to change notification settings - Fork 0
/
BSTs.cpp
210 lines (176 loc) · 5.12 KB
/
BSTs.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include<iostream>
#include<queue>
using namespace std;
class Node {
public:
int data;
Node* left;
Node* right;
Node(int val) {
data = val;
left = right = NULL;
}
};
class BST {
private:
Node* root;
// Helper function to insert a value into the BST
Node* insertUtil(Node* root, int val) {
if (root == NULL) {
root = new Node(val);
return root;
}
if (val < root->data)
root->left = insertUtil(root->left, val);
else if (val > root->data)
root->right = insertUtil(root->right, val);
return root;
}
void inorderUtil(Node* root) {
if (root == NULL) return;
inorderUtil(root->left);
cout << root->data << " ";
inorderUtil(root->right);
}
void preorderUtil(Node* root) {
if (root == NULL) return;
cout << root->data << " ";
preorderUtil(root->left);
preorderUtil(root->right);
}
void postorderUtil(Node* root) {
if (root == NULL) return;
postorderUtil(root->left);
postorderUtil(root->right);
cout << root->data << " ";
}
void levelOrderUtil(Node* root) {
if (root == NULL) return;
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* current = q.front();
q.pop();
cout << current->data << " ";
if (current->left != NULL) q.push(current->left);
if (current->right != NULL) q.push(current->right);
}
}
// Helper function to calculate height of the tree
int heightUtil(Node* root) {
if (root == NULL) return 0;
int leftHeight = heightUtil(root->left);
int rightHeight = heightUtil(root->right);
return max(leftHeight, rightHeight) + 1;
}
bool searchUtil(Node* root, int val) {
if (root == NULL) return false;
if (root->data == val) return true;
if (val < root->data) return searchUtil(root->left, val);
return searchUtil(root->right, val);
}
// Helper function to find minimum value node in the subtree rooted at "node"
Node* minValueNode(Node* node) {
Node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
public:
BST() {
root = NULL;
}
// Function to insert a value into the BST
void insert(int val) {
root = insertUtil(root, val);
}
// Function to perform inorder traversal
void inorderTraversal() {
cout << "Inorder Traversal: ";
inorderUtil(root);
cout << endl;
}
// Function to perform preorder traversal
void preorderTraversal() {
cout << "Preorder Traversal: ";
preorderUtil(root);
cout << endl;
}
// Function to perform postorder traversal
void postorderTraversal() {
cout << "Postorder Traversal: ";
postorderUtil(root);
cout << endl;
}
// Function to perform level-order traversal
void levelOrderTraversal() {
cout << "Levelorder Traversal: ";
levelOrderUtil(root);
cout << endl;
}
// Function to calculate height of the tree
int height() {
return heightUtil(root);
}
// Function to search for a value in the BST
bool search(int val) {
return searchUtil(root, val);
}
// Function to delete a node with given key
void remove(int key) {
root = removeUtil(root, key);
}
// Helper function to delete a node with given key
Node* removeUtil(Node* root, int key) {
if (root == NULL) return root;
if (key < root->data)
root->left = removeUtil(root->left, key);
else if (key > root->data)
root->right = removeUtil(root->right, key);
else {
if (root->left == NULL) {
Node* temp = root->right;
delete root;
return temp;
} else if (root->right == NULL) {
Node* temp = root->left;
delete root;
return temp;
}
Node* temp = minValueNode(root->right);
root->data = temp->data;
root->right = removeUtil(root->right, temp->data);
}
return root;
}
};
int main() {
BST tree;
// Insert elements into the BST
tree.insert(50);
tree.insert(30);
tree.insert(20);
tree.insert(40);
tree.insert(70);
tree.insert(60);
tree.insert(80);
// Perform traversals
tree.inorderTraversal();
tree.preorderTraversal();
tree.postorderTraversal();
tree.levelOrderTraversal();
// Calculate height
cout << "Height of the BST: " << tree.height() << endl;
// Search for elements
int searchKey = 60;
if (tree.search(searchKey))
cout << searchKey << " is present in the BST." << endl;
else
cout << searchKey << " is not present in the BST." << endl;
// Delete elements
// tree.remove(20);
// tree.remove(30);
// Perform traversals after deletion
// tree.inorderTraversal();
return 0;
}