-
Notifications
You must be signed in to change notification settings - Fork 13
/
1_BSTImpl.cpp
194 lines (181 loc) · 4.23 KB
/
1_BSTImpl.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
#include <bits/stdc++.h>
#include "BinaryTreeNode.cpp"
using namespace std;
class BST
{
private:
BinaryTreeNode<int> *root;
bool search(BinaryTreeNode<int> *root, int data)
{
if (root == NULL)
{
return false;
}
if (root->data == data)
{
return true;
}
else if (root->data > data)
{
return search(root->left, data);
}
else
{
return search(root->right, data);
}
}
BinaryTreeNode<int> *insert(BinaryTreeNode<int> *root, int data)
{
if (root == NULL)
{
BinaryTreeNode<int> *node = new BinaryTreeNode<int>(data);
return node;
}
if (data <= root->data)
{
BinaryTreeNode<int> *left = insert(root->left, data);
root->left = left;
}
else
{
BinaryTreeNode<int> *right = insert(root->right, data);
root->right = right;
}
return root;
}
int minimum(BinaryTreeNode<int> *root)
{
if (root == NULL)
{
return INT_MAX;
}
return min(root->data, min(minimum(root->left), minimum(root->right)));
}
BinaryTreeNode<int> *remove(BinaryTreeNode<int> *root, int data)
{
if (root == NULL)
{
return NULL;
}
if (root->data == data)
{
if (root->left == NULL && root->right == NULL)
{
return NULL;
}
else if (root->left != NULL && root->right == NULL)
{
BinaryTreeNode<int> *left = root->left;
delete root;
return left;
}
else if (root->right != NULL && root->left == NULL)
{
BinaryTreeNode<int> *right = root->right;
delete root;
return right;
}
else if (root->left != NULL && root->right != NULL)
{
// calculate min BST iterative.
BinaryTreeNode<int> *curr = root->right;
while (curr->left != NULL)
{
curr = curr->left;
}
int minRight = curr->data;
// int minRight = minimum(root->right);
root->data = minRight;
BinaryTreeNode<int> *newRight = remove(root->right, minRight);
root->right = newRight;
return root;
}
}
else if (root->data > data)
{
BinaryTreeNode<int> *left = remove(root->left, data);
root->left = left;
return root;
}
else
{
BinaryTreeNode<int> *right = remove(root->right, data);
root->right = right;
return root;
}
return root;
}
void print(BinaryTreeNode<int> *root)
{
if (root == NULL)
{
return;
}
cout << root->data << ":";
if (root->left)
{
cout << "L:" << root->left->data << ",";
}
if (root->right)
{
cout << "R:" << root->right->data;
}
cout << endl;
print(root->left);
print(root->right);
}
public:
BST()
{
root = NULL;
}
~BST()
{
delete root;
}
void remove(int data)
{
this->root = remove(root, data);
}
void print()
{
print(root);
}
void insert(int data)
{
this->root = insert(root, data);
}
bool search(int data)
{
return search(root, data);
}
};
int main()
{
BST *tree = new BST();
int choice, input, q;
cin >> q;
while (q--)
{
cin >> choice;
switch (choice)
{
case 1:
cin >> input;
tree->insert(input);
break;
case 2:
cin >> input;
tree->remove(input);
break;
case 3:
cin >> input;
cout << ((tree->search(input)) ? "true\n" : "false\n");
break;
default:
tree->print();
break;
}
}
return 0;
}