-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTMain.java
More file actions
264 lines (248 loc) · 6.36 KB
/
BSTMain.java
File metadata and controls
264 lines (248 loc) · 6.36 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
package core_problem;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
class BSTNode {
BSTNode left;
BSTNode right;
int val;
BSTNode(int val) {
left = right = null;
this.val = val;
}
}
class BinarySerachTree {
private BSTNode root;
public BinarySerachTree() {
root = null;
}
public void add(int val) {
//(1)번 구현
BSTNode current = root;
if(root == null) {
root = new BSTNode(val);
// System.out.print(root.val + " ");
}
else {
while(true) {
if(current == null)
return;
else {
if(val < current.val) {
if(current.left == null) {
current.left = new BSTNode(val);
// System.out.print(current.val + " ");
break;
} else
current = current.left;
} else if(val > current.val) {
if(current.right == null) {
current.right = new BSTNode(val);
// System.out.print(current.val + " ");
break;
} else
current = current.right;
} else
return;
}
}
}
}
public Boolean search(int val) {
//(2)번 구현
BSTNode current = root;
while(true) {
if(current == null)
return false;
else {
if(val > current.val) {
current = current.right;
} else if(val < current.val) {
current = current.left;
} else {
return true;
}
}
}
}
public Boolean remove(int val) {
//(4)번 구현
BSTNode current = root;
BSTNode pre = null;
if(root == null)
return false;
else { // 제거하려는 노드를 찾는 과정
if(root.val != val) { // 루트가 삭제할 노드가 아닐때
pre = root; // 이전 노드를 루트로 설정
if(val < pre.val) // 값이 루트노드보다 값이 작을 경우 왼쪽 방향으로
current = pre.left; // 왼쪽 노드를 현재 노드로 설정
else if(val > pre.val)
current = pre.right;
while(true) {
if(current.val == val) // 이동한 현재노드가 제거하려는 노드일때 반복 종료
break;
else {
pre = current; // 현재 노드를 이전 노드로 설정
if(val < current.val) // 현재 노드의 값과 제거하려는 값을 비교하여 현재 노드 이동
current = current.left;
else
current = current.right;
}
}
}
else
current = root;
// 값을 찾았는지에 따른 값 제거
if(current == null)
return false;
else {
if(current.left==null && current.right==null) {
if(pre == null) // 현재 노드가 root
root = null;
else { // 이전 노드가 root가 아닌 노드
if(current == pre.left) // 현재 노드가 이전 노드의 왼쪽일때
pre.left = null;
else
pre.right = null;
}
} else if(current.left==null || current.right==null) { // 노드가 하나만 있을때
if(current == pre.left) { // 현재 노드가 이전 노드의 왼쪽일때
if(current.left == null)
pre.left = current.right;
else
pre.left = current.left;
} else { // 현재 노드가 이전 노드의 우측일때
if(current.left == null)
pre.right = current.right;
else
pre.right = current.left;
}
} else if(current.left != null && current.right != null) { // 자식노드가 둘다 있을때
BSTNode chg = current.right;
BSTNode preChg = current;
while(true) {
if(chg.left == null)
break;
else {
preChg = chg;
chg = chg.left;
}
}
if(preChg.left == chg)
preChg.left = chg.right;
else
preChg.right = chg.right;
current.val = chg.val;
}
return true;
}
}
}
private int getkMinVal() {
//(3)번 구현
BSTNode current = root;
while(true) {
if(current.left == null)
break;
else
current = current.left;
}
return current.val;
}
private int getkMaxVal() {
//(3)번 구현
BSTNode current = root;
while(true) {
if(current.right == null)
break;
else
current = current.right;
}
return current.val;
}
public void printTree() {
System.out.print("[Print tree]");
System.out.printf("\nMin value: %d", getkMinVal());
System.out.printf("\nMax value: %d", getkMaxVal());
System.out.print("\nIn-order : ");
printInOrder(this.root);
System.out.print("\nPre-order : ");
printPreOrder(this.root);
System.out.print("\nPost-order : ");
printPostOrder(this.root);
System.out.print("\nLevel-order : ");
printLevelOrder(this.root);
}
private void printInOrder(BSTNode current) {
//(5)번 구현
if(current != null) {
System.out.print(current.val + " ");
printInOrder(current.left);
printInOrder(current.right);
}
}
private void printPreOrder(BSTNode current) {
//(5)번 구현
if(current != null) {
printPreOrder(current.left);
System.out.print(current.val + " ");
printPreOrder(current.right);
}
}
private void printPostOrder(BSTNode current) {
//(5)번 구현
if(current != null) {
printPostOrder(current.right);
System.out.print(current.val + " ");
printPostOrder(current.left);
}
}
private void printLevelOrder(BSTNode current) {
//(5)번 구현
List<Integer> list = new ArrayList<>();
Queue<BSTNode> q = new LinkedList<>();
BSTNode delete=null;
q.add(current);
while(!q.isEmpty()) {
delete = q.poll();
list.add(delete.val);
if(delete.left != null) {
q.add(delete.left);
}
if(delete.right != null) {
q.add(delete.right);
}
}
System.out.println(list); // 레벨 구간마다 큰괄호 주는 것이 필수는 아니지만 구현이 안됨...
}
}
public class BSTMain {
public static void main(String[] args) {
int arr[] = { 8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15 };
BinarySerachTree bst = new BinarySerachTree();
for (int i = 0; i < arr.length; i++) {
bst.add(arr[i]);
}
bst.printTree();
System.out.println();
System.out.print("\n--------------------");
System.out.print("\nremove data : " + 4);
bst.remove(4);
System.out.print("\nremove data : " + 15);
bst.remove(15);
System.out.print("\nremove data : " + 2);
bst.remove(2);
// bst.remove(2);
// bst.remove(3);
// bst.remove(5);
// bst.remove(6);
// bst.remove(7);
// bst.remove(8); // 삭제하는 노드가 root일
// bst.remove(10);
// bst.remove(11);
// bst.remove(12);
System.out.println("\n-------------------\n");
bst.printTree();
}
}