-
Notifications
You must be signed in to change notification settings - Fork 139
/
If Tree is BST
177 lines (144 loc) · 2.75 KB
/
If Tree is BST
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
#include<iostream>
#include<queue>
#include<climits>
using namespace std;
class node {
public:
int data = 0;
node* left = NULL;
node* right = NULL;
node(int d) {
data = d;
}
};
void printIn(node* root) {
if (root == NULL) {
return;
}
printIn(root->left);
cout << root->data << " ";
printIn(root->right);
}
void bfs(node* root) {
queue<node*> q;
q.push(root);
q.push(NULL);
while (!q.empty()) {
node* f = q.front();
if (f == NULL) {
cout << endl;
q.pop();
if (!q.empty()) {
q.push(NULL);
}
} else {
cout << f->data << " ";
q.pop();
if (f->left != NULL) q.push(f->left);
if (f->right != NULL) q.push(f->right);
}
}
}
node* insertInBST(node* root, int d) {
// Base Case
if (root == NULL) {
node* n = new node(d);
return n;
}
if (d <= root->data) {
root->left = insertInBST(root->left, d);
} else {
root->right = insertInBST(root->right, d);
}
return root;
}
node* buildBST() {
int d; cin >> d;
node* root = NULL;
while (d != -1) {
root = insertInBST(root, d);
cin >> d;
}
return root;
}
node* minimum(node* root) {
if (root->left) {
return minimum(root->left);
} else {
return root;
}
}
node* maximum(node* root) {
if (root->right) {
return maximum(root->right);
} else {
return root;
}
}
node* deleteInBST(node* root, int key) {
// Base Case
if (root->data == key) {
// no children
if (root->left == NULL && root->right == NULL) {
delete root;
return NULL;
}
// 1 child
if (root->left && root->right == NULL) {
node* temp = root->left;
delete root;
return temp;
}
else if (root->right && root->left == NULL) {
node* temp = root->right;
delete root;
return temp;
}
// 2 children
else {
// Find min of right subtree
node* min = minimum(root->right);
// node *min = root->right;
// while(min->left){
// min = min->left;
// }
root->data = min->data;
// we know that min->left is already NULL (cz it is the smallest)
root->right = deleteInBST(root->right, min->data);
return root;
}
}
if (key < root->data) {
root->left = deleteInBST(root->left, key);
return root;
} else {
root->right = deleteInBST(root->right, key);
return root;
}
}
bool isBST(node *root, int minV = INT_MIN, int maxV = INT_MAX) {
// Base Case
if (root == NULL) {
return 1;
}
if (root->data >= minV && root->data < maxV && isBST(root->left, minV, root->data) && isBST(root-> right, root->data, maxV)) {
return 1;
}
return 0;
}
node* buildTree() {
int d; cin >> d;
if (d == -1) {
return NULL;
}
node *root = new node(d);
root->left = buildTree();
root->right = buildTree();
return root;
}
int main() {
node* root = buildTree();
bfs(root); cout << endl;
printIn(root); cout << endl;
cout << isBST(root) << endl;
}