Skip to content

Commit 42becf8

Browse files
authored
Create main.cpp
1 parent e71dead commit 42becf8

File tree

1 file changed

+305
-0
lines changed

1 file changed

+305
-0
lines changed
Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
#include <iostream>
2+
#include <queue>
3+
using namespace std;
4+
5+
class Node{
6+
public:
7+
int data;
8+
Node* right;
9+
Node* left;
10+
11+
Node(int data){
12+
this -> data = data;
13+
this -> left = NULL;
14+
this -> right = NULL;
15+
}
16+
};
17+
18+
Node* insertIntoBST(Node* root, int data){
19+
// base case
20+
if(root == NULL){
21+
root = new Node(data);
22+
return root;
23+
}
24+
25+
if(data > root -> data){
26+
// right part insertion
27+
root -> right = insertIntoBST(root -> right, data);
28+
}else{
29+
// left part insertion
30+
root -> left = insertIntoBST(root -> left, data);
31+
}
32+
33+
return root;
34+
}
35+
36+
void takeInput(Node* &root){
37+
int data;
38+
cin >> data;
39+
40+
while (data != -1){
41+
root = insertIntoBST(root, data);
42+
cin >> data;
43+
}
44+
}
45+
46+
void levelOrderTraversal(Node* root){
47+
queue<Node*> q;
48+
q.push(root);
49+
q.push(NULL);
50+
51+
while(!q.empty()){
52+
Node* temp = q.front();
53+
q.pop();
54+
55+
if(temp == NULL){
56+
cout << endl;
57+
if(!q.empty()){
58+
q.push(NULL);
59+
}
60+
}else{
61+
cout << temp -> data << " ";
62+
if(temp -> left){
63+
q.push(temp -> left);
64+
}
65+
if(temp -> right){
66+
q.push(temp -> right);
67+
}
68+
}
69+
}
70+
}
71+
72+
void inOrderTraversal(Node* root){
73+
if(root == NULL) return;
74+
75+
inOrderTraversal(root -> left);
76+
cout << root -> data << " ";
77+
inOrderTraversal(root -> right);
78+
}
79+
80+
void preOrderTraversal(Node* root){
81+
if(root == NULL) return;
82+
83+
cout << root -> data << " ";
84+
preOrderTraversal(root -> left);
85+
preOrderTraversal(root -> right);
86+
}
87+
88+
void postOrderTraversal(Node* root){
89+
if(root == NULL) return;
90+
91+
postOrderTraversal(root -> left);
92+
postOrderTraversal(root -> right);
93+
cout << root -> data << " ";
94+
}
95+
96+
void SearchBST(Node* root, int value){
97+
if(root == NULL) {
98+
cout << "Value not Present" << endl;
99+
return;
100+
}
101+
102+
if(root -> data == value) {
103+
cout << "Value is Present" << endl;
104+
return;
105+
}
106+
107+
if(root -> data < value) return SearchBST(root -> right, value);
108+
else return SearchBST(root -> left, value);
109+
}
110+
111+
Node* minValue(Node* root){
112+
if(root == NULL) {
113+
cout << "Tree is Empty" << endl;
114+
return NULL;
115+
}
116+
while(root -> left != NULL){
117+
root = root -> left;
118+
}
119+
120+
cout << "Minimum Value is : " << root -> data << endl;
121+
return root;
122+
}
123+
124+
Node* maxValue(Node* root){
125+
if(root == NULL){
126+
cout << "Tree is Empty" << endl;
127+
return NULL;
128+
}
129+
130+
while(root -> right != NULL){
131+
root = root -> right;
132+
}
133+
134+
cout << "Maximum value is : "<< root -> data << endl;
135+
return root;
136+
}
137+
138+
Node* inOrderPredecessor(Node* root, Node* target){
139+
Node* predecessor = NULL;
140+
141+
while(root != NULL){
142+
if(target -> data > root -> data){
143+
predecessor = root;
144+
root = root -> right;
145+
}else{
146+
root = root -> left;
147+
}
148+
}
149+
150+
return predecessor;
151+
}
152+
153+
Node* inOrderSuccessor(Node* root, Node* target){
154+
if(target->right != NULL) {
155+
Node* current = target->right;
156+
while(current->left != NULL) {
157+
current = current->left;
158+
}
159+
return current;
160+
} else {
161+
Node* successor = NULL;
162+
Node* ancestor = root;
163+
164+
while(ancestor != target) {
165+
if(target->data < ancestor->data) {
166+
successor = ancestor;
167+
ancestor = ancestor->left;
168+
} else {
169+
ancestor = ancestor->right;
170+
}
171+
}
172+
return successor;
173+
}
174+
}
175+
176+
Node* deleteFromBST(Node* root, int value) {
177+
// Base case: If the root is NULL, return NULL
178+
if (root == NULL) return root;
179+
180+
// If the node to be deleted is found
181+
if (root->data == value) {
182+
// Case 1: Node with 0 children (leaf node)
183+
if (root->left == NULL && root->right == NULL) {
184+
delete root;
185+
return NULL;
186+
}
187+
188+
// Case 2: Node with 1 child
189+
if (root->left != NULL && root->right == NULL) {
190+
Node* temp = root->left;
191+
delete root;
192+
return temp;
193+
}
194+
if (root->left == NULL && root->right != NULL) {
195+
Node* temp = root->right;
196+
delete root;
197+
return temp;
198+
}
199+
200+
// Case 3: Node with 2 children
201+
if (root->left != NULL && root->right != NULL) {
202+
// Find the minimum value in the right subtree
203+
int mini = minValue(root->right)->data;
204+
// Replace root's data with the minimum value
205+
root->data = mini;
206+
// Recursively delete the minimum node in the right subtree
207+
root->right = deleteFromBST(root->right, mini);
208+
}
209+
}
210+
// If the value is less than root's data, go to the left subtree
211+
else if (root->data > value) {
212+
root->left = deleteFromBST(root->left, value);
213+
}
214+
// If the value is greater than root's data, go to the right subtree
215+
else {
216+
root->right = deleteFromBST(root->right, value);
217+
}
218+
219+
return root;
220+
}
221+
222+
223+
int main() {
224+
Node* root = NULL;
225+
226+
cout << endl << "Enter data to create BST" <<endl;
227+
takeInput(root);
228+
229+
cout << endl << "Printing the BST" << endl;
230+
levelOrderTraversal(root);
231+
232+
cout << endl << "Printing In-Order Traversal" << endl;
233+
inOrderTraversal(root);
234+
235+
cout << endl << "Printing Pre-Order Traversal" << endl;
236+
preOrderTraversal(root);
237+
238+
cout << endl << "Printing Post-Order Traversal" << endl;
239+
postOrderTraversal(root);
240+
241+
cout << endl << "Searching in BST" << endl;
242+
SearchBST(root, 2);
243+
SearchBST(root, 50);
244+
245+
cout << endl << "Searching minimum value" << endl;
246+
minValue(root);
247+
248+
cout << endl << "Searching maximum value" << endl;
249+
maxValue(root);
250+
251+
cout << endl << "Searching the Predecessor" << endl;
252+
int targetValue = 5;
253+
Node* targetNode = root;
254+
255+
// Find the target node in the BST
256+
while (targetNode != NULL && targetNode->data != targetValue) {
257+
if (targetValue < targetNode->data) {
258+
targetNode = targetNode->left;
259+
} else {
260+
targetNode = targetNode->right;
261+
}
262+
}
263+
264+
if (targetNode != NULL) {
265+
Node* predecessor = inOrderPredecessor(root, targetNode);
266+
if (predecessor != NULL) {
267+
cout << "Predecessor of " << targetValue << " is : " << predecessor->data << endl;
268+
} else {
269+
cout << "No predecessor found for " << targetValue << endl;
270+
}
271+
} else {
272+
cout << "Target node not found in the tree." << endl;
273+
}
274+
275+
cout << endl << "Searching the Successor" << endl;
276+
int value = 5;
277+
Node* target = root;
278+
279+
// Find the target node in the BST
280+
while(target != NULL && target->data != value) {
281+
if(value < target->data) {
282+
target = target->left;
283+
} else {
284+
target = target->right;
285+
}
286+
}
287+
288+
if(target != NULL) {
289+
Node* successor = inOrderSuccessor(root, target);
290+
if(successor != NULL) {
291+
cout << "Successor of " << value << " is : " << successor->data << endl;
292+
} else {
293+
cout << "No Successor found for " << value << endl;
294+
}
295+
} else {
296+
cout << "Target node not found in the tree." << endl;
297+
}
298+
299+
root = deleteFromBST(root, 90);
300+
301+
cout << endl << "Printing the BST" << endl;
302+
levelOrderTraversal(root);
303+
304+
return 0;
305+
}

0 commit comments

Comments
 (0)