-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGROUP-B-6.cpp
More file actions
286 lines (257 loc) · 6.16 KB
/
GROUP-B-6.cpp
File metadata and controls
286 lines (257 loc) · 6.16 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
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
/*
SAYALI RAHANE
CLASS- SE A
BATCH- C50
GROUP-B-6. 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. Find number of nodes in longest path from root
iii. Minimum data value found in the tree
iv. Change a tree so that the roles of the left and right pointers are swapped at every node
v. Search a value
*/
//header files
#include <iostream>
using namespace std;
int level = 0;
struct Node
{
int data;
struct Node *left;
struct Node *right;
} *Root,*Parent;
//function to create root of BST
void createBST(int val)
{
struct Node *Newnode;
Newnode = new struct Node;
Newnode->data = val;
Newnode->left = NULL;
Newnode->right = NULL;
if(Root == NULL)
{
Root = Newnode;
cout<<"\n\t Root "<<Root->data<<" of BST is Ready Now ";
}
else
{
cout<<"\n\t Root already exists";
}
}
//function to display BST in Pre-Order_BST
//data-leftlright
void preorderBST(struct Node *root)
{
if(root!=NULL)
{
cout<<" "<<root->data;
preorderBST(root->left);
preorderBST(root->right);
}
}
//Function to display BST in Inorder_BST
//(Left->Data->Right)
void inorderBST(struct Node *root)
{
if(root!=NULL)
{
preorderBST(root->left);
cout<<" "<<root->data;
preorderBST(root->right);
}
}
//Function to display BST in Postorder_BST
//(Left->Right->Data)
void postorderBST(struct Node *root)
{
if(root!=NULL)
{
preorderBST(root->left);
preorderBST(root->right);
cout<<" "<<root->data;
}
}
//Function to create New Node
struct Node* createNode(int val)
{
struct Node *Newnode;
Newnode = new struct Node;
Newnode->data = val;
Newnode->left = NULL;
Newnode->right = NULL;
return Newnode;
}
//Function to Insert New Nodes in BST
void insertBST(struct Node *current, struct Node *Newnode)
{
if(current!=NULL)
{
if(Newnode->data <= current->data)
{
if(current->left == NULL)
{
current->left = Newnode;
cout<<"\n\n Newnode "<<Newnode->data<<" is inserted as left child of "<<current->data;
}
else
{
insertBST(current->left,Newnode);
}
}
else
{
if(current->right == NULL)
{
current->right = Newnode;
cout<<"\n\n Newnode "<<Newnode->data<<" is inserted as right child of "<<current->data;
}
}
}
else
{
insertBST(current->right,Newnode);
}
}
void searchNode(struct Node *root, int Key)
{
if(root!=NULL)
{
if(Key == root->data)
{
cout<<"\n\t Key is found on level "<<level;
}
else if(Key < root->data)
{
level++;
searchNode(root->left,Key);
}
else
{
level++;
searchNode(root->right,Key);
}
}
else
{
cout<<"\n\t Key not found";
}
}
void minNode()
{
struct Node *p;
p = Root;
while(p->left != NULL)
{
p = p->left;
}
cout<<"\n\n Min Value : "<<p->data;
}
void maxNode()
{
struct Node *p;
p = Root;
while(p->right != NULL)
{
p = p->right;
}
cout<<"\n\n Max Value : "<<p->data;
}
struct Node* minValueNode(struct Node* node)
{
struct Node* current = node;
/* loop down to find the leftmost leaf */
while(current && current->left != NULL)
{
current = current->left;
}
return current;
}
struct Node *deleteNode(struct Node *root, int Key)
{
if(root = NULL)
{
return root;
}
//If the key to be deleted is smaller than the root's key, then it lies in left subtree
if(Key < root->data)
{
root->left = deleteNode(root->left,Key);
}
//If the key to be deleted is greater than the root's key, then it lies in right subtree
else if(Key > root->data)
{
root->right = deleteNode(root->right,Key);
}
else
{
// node has no child
if(root->left == NULL and root->right == NULL)
{
return NULL;
}
// node with only one child or no child
else if(root->left == NULL)
{
struct Node* temp = root->right;
delete root;
return temp;
}
else if(root->right == NULL)
{
struct Node* temp = root->left;
delete root;
return temp;
}
// node with two children: Get the inorder successor
struct Node* temp = minValueNode(root->right);
// Copy the inorder successor's content to this node
root->data = temp->data;
// Delete the inorder successor
root->right = deleteNode(root->right,temp->data);
}
return root;
}
int main()
{
cout<<"\n\n Binary Search Tree and its operations ";
struct Node *Newnode;
int Key;
Root = NULL;
cout<<"\n\n Creating BST";
createBST(25);
cout<<"\n\n Inserting new nodes in BST";
Newnode = createNode(10);
insertBST(Root,Newnode);
Newnode = createNode(20);
insertBST(Root,Newnode);
Newnode = createNode(30);
insertBST(Root,Newnode);
Newnode = createNode(40);
insertBST(Root,Newnode);
cout<<"\n\n Display / Traversing BST";
cout<<"\n\n Preorder Traversal";
preorderBST(Root);
cout<<"\n\n Inorder Traversal";
inorderBST(Root);
cout<<"\n\n Postorder Traversal";
postorderBST(Root);
cout<<"\n\n Searching a Key in BST";
cout<<"\n\n Enter the key to search : ";
cin>>Key;
searchNode(Root,Key);
cout<<"\n\n Find Max and Min Nodes";
minNode();
maxNode();
cout<<"\n\n Delete a Key from BST";
level = 0;
Root = deleteNode(Root,20);
cout<<"\n\n After Deleting Inorder Traversal";
inorderBST(Root);
Root = deleteNode(Root,30);
cout<<"\n\n After Deleting Inorder Traversal";
inorderBST(Root);
Root = deleteNode(Root,40);
cout<<"\n\n After Deleting Inorder Traversal";
inorderBST(Root);
return 0;
}