Skip to content

Commit d499389

Browse files
committed
BST
1 parent 96d17f3 commit d499389

21 files changed

+1282
-176
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"C_Cpp.errorSquiggles": "disabled",
4848
"terminal.integrated.defaultProfile.windows": "Git Bash",
4949
"cSpell.words": [
50-
"nottake"
50+
"nottake",
51+
"sucessor"
5152
]
5253
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
struct TreeNode {
5+
int val;
6+
TreeNode* left;
7+
TreeNode* right;
8+
TreeNode(int x) {
9+
val = x;
10+
left = right = NULL;
11+
}
12+
};
13+
14+
class Solution {
15+
public:
16+
TreeNode* findLastRight(TreeNode* root) {
17+
while (root->right != NULL) {
18+
root = root->right;
19+
}
20+
return root;
21+
}
22+
23+
TreeNode* helper(TreeNode* root) {
24+
if (root->left == NULL) return root->right;
25+
if (root->right == NULL) return root->left;
26+
27+
TreeNode* rightChild = root->right;
28+
TreeNode* lastRight = findLastRight(root->left);
29+
lastRight->right = rightChild;
30+
return root->left;
31+
}
32+
33+
TreeNode* deleteNode(TreeNode* root, int key) {
34+
if (root == NULL) return NULL;
35+
36+
if (root->val == key) return helper(root);
37+
38+
TreeNode* dummy = root;
39+
40+
while (root != NULL) {
41+
if (key < root->val) {
42+
if (root->left != NULL && root->left->val == key) {
43+
root->left = helper(root->left);
44+
break;
45+
} else {
46+
root = root->left;
47+
}
48+
} else {
49+
if (root->right != NULL && root->right->val == key) {
50+
root->right = helper(root->right);
51+
break;
52+
} else {
53+
root = root->right; // **Fixed: Continue moving right**
54+
}
55+
}
56+
}
57+
58+
return dummy;
59+
}
60+
};
61+
62+
// Function to insert nodes into BST
63+
TreeNode* insertIntoBST(TreeNode* root, int val) {
64+
if (!root) return new TreeNode(val);
65+
if (val < root->val) root->left = insertIntoBST(root->left, val);
66+
else root->right = insertIntoBST(root->right, val);
67+
return root;
68+
}
69+
70+
// Function to print inorder traversal
71+
void inorder(TreeNode* root) {
72+
if (!root) return;
73+
inorder(root->left);
74+
cout << root->val << " ";
75+
inorder(root->right);
76+
}
77+
78+
int main() {
79+
Solution sol;
80+
TreeNode* root = NULL;
81+
82+
vector<int> values = {5, 3, 7, 2, 4, 6, 8};
83+
for (int val : values) {
84+
root = insertIntoBST(root, val);
85+
}
86+
87+
int key;
88+
cin >> key;
89+
90+
cout << "Before Deletion: ";
91+
inorder(root);
92+
cout << endl;
93+
94+
root = sol.deleteNode(root, key);
95+
96+
cout << "After Deletion: ";
97+
inorder(root);
98+
cout << endl;
99+
100+
return 0;
101+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Node {
5+
int data;
6+
Node* left;
7+
Node* right;
8+
Node(int val) {
9+
data = val;
10+
left = right = NULL;
11+
}
12+
};
13+
14+
class Solution {
15+
public:
16+
int floor(Node* root, int x) {
17+
int floor = -1;
18+
while (root) {
19+
if (root->data == x) {
20+
return root->data;
21+
}
22+
if (root->data > x) {
23+
root = root->left;
24+
} else {
25+
floor = root->data;
26+
root = root->right;
27+
}
28+
}
29+
return floor;
30+
}
31+
32+
int ceil(Node* root, int x) {
33+
int ceil = -1;
34+
while (root) {
35+
if (root->data == x) {
36+
return root->data;
37+
}
38+
if (root->data < x) {
39+
root = root->right;
40+
} else {
41+
ceil = root->data;
42+
root = root->left;
43+
}
44+
}
45+
return ceil;
46+
}
47+
};
48+
49+
// Function to insert a node into BST
50+
Node* insertIntoBST(Node* root, int val) {
51+
if (!root) return new Node(val);
52+
if (val < root->data) root->left = insertIntoBST(root->left, val);
53+
else root->right = insertIntoBST(root->right, val);
54+
return root;
55+
}
56+
57+
int main() {
58+
Solution sol;
59+
Node* root = NULL;
60+
vector<int> values = {10, 5, 15, 3, 7, 18};
61+
62+
for (int val : values) {
63+
root = insertIntoBST(root, val);
64+
}
65+
66+
int x;
67+
cin >> x;
68+
69+
cout << "Floor: " << sol.floor(root, x) << endl;
70+
cout << "Ceil: " << sol.ceil(root, x) << endl;
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)