assignment_16.cpp
Problem Statement
assignment_16.cpp
WAP to create a Binary Search Tree and include following operations in tree: i. Insertion (Recursive and Iterative Implementation) ii. Deletion by copying iii. Deletion by Merging iv. Search a no. in BST v. Display its preorder, postorder and inorder traversals Recursively vi. Display its preorder, postorder and inorder traversals Iteratively vii. Display its level-by-level traversals viii. Count the non-leaf nodes and leaf nodes ix. Display height of tree x. Create a mirror image of tree xi. Check whether two BSTs are equal or not
Source Code
cpp
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;
struct node {
int data;
node *left;
node *right;
node(int val) : data(val), left(nullptr), right(nullptr) {}
};
class BST {
private:
node *root;
node* insertRecursive(node *current, int val) {
if (current == nullptr) return new node(val);
if (val < current->data) current->left = insertRecursive(current->left, val);
else if (val > current->data) current->right = insertRecursive(current->right, val);
else cout << "Duplicate value ignored.\n";
return current;
}
node* deleteByCopying(node *current, int key, bool &found) {
if (current == nullptr) return nullptr;
if (key < current->data) {
current->left = deleteByCopying(current->left, key, found);
} else if (key > current->data) {
current->right = deleteByCopying(current->right, key, found);
} else {
found = true;
if (current->left == nullptr) { node *temp = current->right; delete current; return temp; }
if (current->right == nullptr) { node *temp = current->left; delete current; return temp; }
node *pred = current->left;
while (pred->right != nullptr) pred = pred->right;
current->data = pred->data;
current->left = deleteByCopying(current->left, pred->data, found);
}
return current;
}
node* deleteByMerging(node *current, int key, bool &found) {
if (current == nullptr) return nullptr;
if (key < current->data) { current->left = deleteByMerging(current->left, key, found); return current; }
if (key > current->data) { current->right = deleteByMerging(current->right, key, found); return current; }
found = true;
if (current->left == nullptr) { node *temp = current->right; delete current; return temp; }
if (current->right == nullptr) { node *temp = current->left; delete current; return temp; }
node *successor = current->right;
while (successor->left != nullptr) successor = successor->left;
successor->left = current->left;
node *temp = current->right;
delete current;
return temp;
}
int countLeaf(node *current) const {
if (current == nullptr) return 0;
if (current->left == nullptr && current->right == nullptr) return 1;
return countLeaf(current->left) + countLeaf(current->right);
}
int countNonLeaf(node *current) const {
if (current == nullptr || (current->left == nullptr && current->right == nullptr)) return 0;
return 1 + countNonLeaf(current->left) + countNonLeaf(current->right);
}
int height(node *current) const {
if (current == nullptr) return -1;
return 1 + max(height(current->left), height(current->right));
}
void mirror(node *current) {
if (current == nullptr) return;
swap(current->left, current->right);
mirror(current->left);
mirror(current->right);
}
bool areEqual(node *root1, node *root2) const {
if (!root1 && !root2) return true;
if (!root1 || !root2 || root1->data != root2->data) return false;
return areEqual(root1->left, root2->left) && areEqual(root1->right, root2->right);
}
void destroy(node *current) {
if (current == nullptr) return;
destroy(current->left);
destroy(current->right);
delete current;
}
void preorderRecursive(node *current) const {
if (!current) return;
cout << current->data << " ";
preorderRecursive(current->left);
preorderRecursive(current->right);
}
void inorderRecursive(node *current) const {
if (!current) return;
inorderRecursive(current->left);
cout << current->data << " ";
inorderRecursive(current->right);
}
void postorderRecursive(node *current) const {
if (!current) return;
postorderRecursive(current->left);
postorderRecursive(current->right);
cout << current->data << " ";
}
public:
BST() : root(nullptr) {}
BST(const BST &other) = delete;
BST& operator=(const BST &other) = delete;
~BST() { destroy(root); }
void insertRecursive(int val) { root = insertRecursive(root, val); }
void insertIterative(int val) {
node *newNode = new node(val);
if (root == nullptr) { root = newNode; return; }
node *current = root, *parent = nullptr;
while (current != nullptr) {
parent = current;
if (val < current->data) current = current->left;
else if (val > current->data) current = current->right;
else { cout << "Duplicate value ignored.\n"; delete newNode; return; }
}
if (val < parent->data) parent->left = newNode;
else parent->right = newNode;
}
void deleteCopying(int key) {
bool found = false;
root = deleteByCopying(root, key, found);
if (!found) cout << "Element not found in BST.\n";
}
void deleteMerging(int key) {
bool found = false;
root = deleteByMerging(root, key, found);
if (!found) cout << "Element not found in BST.\n";
}
bool search(int key) const {
node *current = root;
while (current != nullptr) {
if (current->data == key) return true;
current = (key < current->data) ? current->left : current->right;
}
return false;
}
void preorderRecursive() const { preorderRecursive(root); cout << "\n"; }
void inorderRecursive() const { inorderRecursive(root); cout << "\n"; }
void postorderRecursive() const { postorderRecursive(root); cout << "\n"; }
void preorderIterative() const {
if (!root) return;
stack<node*> s;
s.push(root);
while (!s.empty()) {
node *curr = s.top(); s.pop();
cout << curr->data << " ";
if (curr->right) s.push(curr->right);
if (curr->left) s.push(curr->left);
}
cout << "\n";
}
void inorderIterative() const {
stack<node*> s;
node *curr = root;
while (curr != nullptr || !s.empty()) {
while (curr != nullptr) { s.push(curr); curr = curr->left; }
curr = s.top(); s.pop();
cout << curr->data << " ";
curr = curr->right;
}
cout << "\n";
}
void postorderIterative() const {
if (!root) return;
stack<node*> s1, s2;
s1.push(root);
while (!s1.empty()) {
node *curr = s1.top(); s1.pop();
s2.push(curr);
if (curr->left) s1.push(curr->left);
if (curr->right) s1.push(curr->right);
}
while (!s2.empty()) { cout << s2.top()->data << " "; s2.pop(); }
cout << "\n";
}
void levelOrder() const {
if (!root) return;
queue<node*> q;
q.push(root);
while (!q.empty()) {
node *curr = q.front(); q.pop();
cout << curr->data << " ";
if (curr->left) q.push(curr->left);
if (curr->right) q.push(curr->right);
}
cout << "\n";
}
void countNodes() const {
cout << "Leaf Nodes: " << countLeaf(root) << "\n";
cout << "Non-Leaf Nodes: " << countNonLeaf(root) << "\n";
}
void displayHeight() const { cout << "Height of Tree: " << height(root) << "\n"; }
void createMirror() { mirror(root); }
bool isEqual(const BST &other) const { return areEqual(root, other.root); }
};
int main() {
BST tree;
int choice, value;
while (true) {
cout << "1.InsertRec 2.InsertIter 3.DeleteCopy 4.DeleteMerge 5.Search\n";
cout << "6.PreorderRec 7.InorderRec 8.PostorderRec 9.PreorderIter 10.InorderIter\n";
cout << "11.PostorderIter 12.LevelOrder 13.CountNodes 14.Height 15.Mirror 16.CheckEqual 0.Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "Enter value: "; cin >> value; tree.insertRecursive(value); break;
case 2: cout << "Enter value: "; cin >> value; tree.insertIterative(value); break;
case 3: cout << "Enter value to delete: "; cin >> value; tree.deleteCopying(value); break;
case 4: cout << "Enter value to delete: "; cin >> value; tree.deleteMerging(value); break;
case 5:
cout << "Enter value to search: "; cin >> value;
cout << (tree.search(value) ? "Found\n" : "Not found\n");
break;
case 6: tree.preorderRecursive(); break;
case 7: tree.inorderRecursive(); break;
case 8: tree.postorderRecursive(); break;
case 9: tree.preorderIterative(); break;
case 10: tree.inorderIterative(); break;
case 11: tree.postorderIterative(); break;
case 12: tree.levelOrder(); break;
case 13: tree.countNodes(); break;
case 14: tree.displayHeight(); break;
case 15: tree.createMirror(); break;
case 16: {
BST tree2;
int n;
cout << "Enter number of nodes for second BST: "; cin >> n;
for (int i = 0; i < n; i++) { cin >> value; tree2.insertIterative(value); }
cout << (tree.isEqual(tree2) ? "Both BSTs are equal.\n" : "BSTs are not equal.\n");
break;
}
case 0: return 0;
default: cout << "Wrong choice.\n";
}
}
}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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269