-
Notifications
You must be signed in to change notification settings - Fork 0
/
practicalFour.cpp
310 lines (271 loc) · 5.89 KB
/
practicalFour.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
Beginning with an empty binary search tree, Construct binary search tree by inserting the
values in the order given. After constructing a binary tree -
i. Insert new node
ii. Minimum data value found in the tree
iii. Search a value
iv. Display all the nodes.
v. Find number of nodes in longest path from root
vi. Change a tree so that the roles of the left and right pointers are swapped at every node
*/
#include "bits/stdc++.h"
using namespace std;
class Node
{
public:
int data;
Node *left, *right;
Node(int data, Node *left, Node *right)
{
this->data = data;
this->left = left;
this->right = right;
}
};
class BST
{
public:
Node *root;
BST()
{
root = NULL;
}
Node *insert(Node *root, int x)
{
Node *temp = new Node(x, NULL, NULL);
if (!root)
{
root = temp;
}
else if (root->data > x)
root->left = insert(root->left, x);
else if (root->data < x)
root->right = insert(root->right, x);
return root;
}
void search(Node *root, int x)
{
if (root)
{
if (root->data == x)
{
cout << "Entry found :) " << endl;
return;
}
else if (x < root->data)
search(root->left, x);
else if (x > root->data)
search(root->right, x);
else
return;
}
}
void min(Node *root)
{
Node *p = root;
while (p->left)
p = p->left;
cout << p->data << endl;
}
void inorder(Node *root)
{
if (root)
{
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}
}
void preorder(Node *root)
{
if (root)
{
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
void postorder(Node *root)
{
if (root)
{
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
}
int height(Node *root) {
if (root == NULL) {
return 0;
}
return 1 + max(height(root->left), height(root->right));
}
void swapChildren(Node *root) {
if (root != NULL) {
swap(root->left, root->right);
swapChildren(root->left);
swapChildren(root->right);
}
}
};
int main()
{
BST tree;
int ch = 5;
do
{
cout << "Choose Operation: " << endl;
cout << "1. Insert" << endl;
cout << "2. Traversal" << endl;
cout << "3. Search" << endl;
cout << "4. Min" << endl;
cout << "5. Longest Path from root" << endl;
cout << "6. Swap nodes" << endl;
cout << "7. Exit " << endl;
cin >> ch;
switch (ch)
{
case 1:
[&]()
{
int n;
cout << "Enter the number of Elements you want to insert: ";
cin >> n;
for (int i = 0; i < n; i++)
{
int ip;
cin >> ip;
tree.root = tree.insert(tree.root, ip);
}
cout << "Insertion Complete ... " << endl
<< endl;
}();
break;
case 2:
[&]()
{
cout << "Traversals ..." << endl;
cout << "PreOrder" << endl;
tree.preorder(tree.root);
cout << endl;
cout << "InOrder" << endl;
tree.inorder(tree.root);
cout << endl;
cout << "PostOrder" << endl;
tree.postorder(tree.root);
cout << endl;
}();
break;
case 3:
[&]() {
int ip;
cout << "Enter element to search : ";
cin >> ip;
tree.search(tree.root, ip);
}();
break;
case 4:
[&]() {
cout << "Min Element : ";
tree.min(tree.root);
}();
break;
case 5:
[&]() {
cout << "Longest path : ";
cout << tree.height(tree.root) << endl;
}();
break;
case 6:
[&]() {
cout << "Swap nodes: ";
tree.swapChildren(tree.root);
tree.preorder(tree.root);
}();
break;
}
} while (ch != 7);
return 0;
}
/*
---------------------------------------------------------------------------------------------------
Output
---------------------------------------------------------------------------------------------------
Choose Operation:
1. Insert
2. Traversal
3. Search
4. Min
5. Longest Path from root
6. Swap nodes
7. Exit
1
Enter the number of Elements you want to insert: 3
10 9 15
Insertion Complete ...
Choose Operation:
1. Insert
2. Traversal
3. Search
4. Min
5. Longest Path from root
6. Swap nodes
7. Exit
2
Traversals ...
PreOrder
10 9 15
InOrder
9 10 15
PostOrder
9 15 10
Choose Operation:
1. Insert
2. Traversal
3. Search
4. Min
5. Longest Path from root
6. Swap nodes
7. Exit
3
Enter element to search : 15
Entry found :)
Choose Operation:
1. Insert
2. Traversal
3. Search
4. Min
5. Longest Path from root
6. Swap nodes
7. Exit
4
Min Element : 9
Choose Operation:
1. Insert
2. Traversal
3. Search
4. Min
5. Longest Path from root
6. Swap nodes
7. Exit
5
Longest path : 2
Choose Operation:
1. Insert
2. Traversal
3. Search
4. Min
5. Longest Path from root
6. Swap nodes
7. Exit
6
Swap nodes: 10 15 9 Choose Operation:
1. Insert
2. Traversal
3. Search
4. Min
5. Longest Path from root
6. Swap nodes
7. Exit
7
*/