-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktrackingBST.java
More file actions
337 lines (308 loc) · 11.2 KB
/
BacktrackingBST.java
File metadata and controls
337 lines (308 loc) · 11.2 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import java.util.NoSuchElementException;
public class BacktrackingBST implements Backtrack, ADTSet<BacktrackingBST.Node> {
private Stack stack;
private Stack redoStack;
private Node root = null;
private int ZERO = 0;
private int ONE = 1;
private int TWO = 2;
private int THREE = 3;
// Do not change the constructor's signature
public BacktrackingBST(Stack stack, Stack redoStack) {
this.stack = stack;
this.redoStack = redoStack;
}
public Node getRoot() {
if (root == null) {
throw new NoSuchElementException("empty tree has no root");
}
return root;
}
public Node search(int k) {
Node output = root;
boolean found = false;
while (output != null & !found){
int currKey = output.key;
if (currKey == k){
found = true;
} else if (currKey > k){
output = output.left;
} else {
output = output.right;
}
}
return output;
}
public void insert(Node node) {
if (node == null) //**********
throw new IllegalArgumentException("can not input null node");
if (!redoStack.isEmpty()){ //clear retrack stack if insert is not called from retrack.
if ((int)redoStack.pop() != 2){
redoStack.clear();
}
}
node.parent = null;
node.right = null;
node.left = null;
if (root == null){ //insert as node
this.root = node;
} else {
Node temp = root;
boolean found = false;
while (!found){
if (temp.key > node.key){ //find correct node to input new node as child
if (temp.left == null){
found = true;
temp.left = node;
node.parent = temp;
} else {
temp = temp.left;
}
} else {
if (temp.right == null){
found = true;
temp.right = node;
node.parent = temp;
} else {
temp = temp.right;
}
}
}
}
stack.push(node);
stack.push(ZERO); //sign for insert operation
}
public void delete(Node node) { //check case if node has same key and different name
if (!redoStack.isEmpty()){ //clear retrack stack if delete is not called from retrack.
if ((int)redoStack.pop() != 2){
redoStack.clear();
}
}
if (node != null && search(node.getKey()) != null ){
if (node.left == null & node.right==null){
if (node.parent == null){
root = null;
} else {
if (node.parent.getKey() > node.getKey()){
node.parent.left = null;
} else {
node.parent.right = null;
}
}
stack.push(node);
stack.push(ONE); //signing for leaf deletion.
} else if (node.left != null & node.right != null){ //delete node with 2 children
Node suc = successor(node);
delete(suc);
stack.push(suc.parent);
stack.push(suc.right);
stack.push(suc.left);
stack.push(suc);
suc.left = node.left; //gives suc the pointers that node had.
suc.right = node.right;
suc.parent = node.parent;
if (suc.left != null){
suc.left.parent = suc;
}
if (suc.right != null){
suc.right.parent = suc;
}
if(suc.parent==null){ //in case 2 child node was the root.
root=suc;
}
else { //in case 2 child node was not the root, need to change nodes parent to point at successor(new node).
if (suc.key > suc.parent.key)
suc.parent.right = suc;
else
suc.parent.left = suc;
}
stack.push(node);
stack.push(THREE); //signing for 2 child node deletion
} else { //delete node with 1 son.
Node temp = node.right; //assigns nodes right/left son to temp.
if(temp==null) {
temp = node.left;
}
temp.parent = node.parent; //assign temp parent field as nodes parent
if (node.parent == null){ // in case node 1 son was root
root = temp;
} else { //delete 1 son not root
if (node.getKey() > node.parent.key){
node.parent.right = temp;
} else {
node.parent.left = temp;
}
}
stack.push(node);
stack.push(TWO); //sign for 1 son node deletion
}
stack.push(ONE); //signing for deletion operation
}
else
throw new IllegalArgumentException("node not found");
}
public Node minimum() {
Node output = getRoot();
if (output == null){
throw new IllegalArgumentException("tree is empty - no minimum available.");
}
while (output.left != null){
output = output.left;
}
return output;
}
public Node maximum() {
Node output = getRoot();
if (output == null){
throw new IllegalArgumentException("tree is empty - no maximum available.");
}
while (output.right != null){
output = output.right;
}
return output;
}
public Node successor(Node node) {
if (node == null)
throw new IllegalArgumentException("can not find successor for null node");
Node output = node.right;
if (output == null){
throw new IllegalArgumentException("no successor for input node");
}
while (output.left != null){
output = output.left;
}
return output;
}
public Node predecessor(Node node) {
if (node == null)
throw new IllegalArgumentException("can not find successor for null node");
Node output = node.left;
if (output == null){
throw new IllegalArgumentException("no predecessor for input node");
}
while (output.right != null){
output = output.right;
}
return output;
}
@Override
public void backtrack() {
if (!stack.isEmpty()){
int condition = (int)stack.pop();
if(condition == 0){ //backtrack insert operation
Node temp = (Node)stack.pop();
redoStack.push(temp);
if (temp.parent == null){
this.root = null;
} else {
if (temp.parent.getKey() > temp.getKey()){
temp.parent.left = null;
} else {
temp.parent.right = null;
}
}
redoStack.push(ZERO);
} else { //backtrack delete operation
int ident = (int)stack.pop();
Node temp = (Node)stack.pop();
redoStack.push(temp);
if (ident == 1){ //backtrack deleted leaf
if (temp.parent == null){ // return leaf that was root
root = temp;
} else {
if (temp.parent.getKey() > temp.getKey()){
temp.parent.left = temp;
} else {
temp.parent.right = temp;
}
}
} else if (ident == 2){ //backtrack deleted 1 son node
if (temp.right==null){
temp.left.parent = temp;
} else {
temp.right.parent = temp;
}
if (temp.parent != null){
if (temp.getKey() > temp.parent.getKey()){
temp.parent.right = temp;
} else {
temp.parent.left = temp;
}
} else {
root = temp;
}
} else {
Node successor = (Node)stack.pop();
successor.left=(Node)stack.pop();
successor.right=(Node)stack.pop();
successor.parent=(Node)stack.pop();
backtrack();
redoStack.pop();
redoStack.pop();
temp.left.parent=temp;
temp.right.parent=temp;
if(temp.parent==null)
root=temp;
else{
if(temp.parent.key>temp.key)
temp.parent.left=temp;
else
temp.parent.right=temp;
}
}
redoStack.push(ONE);
}
}
}
@Override
public void retrack() {
if (!redoStack.isEmpty()){
int condition = (int)redoStack.pop();
Node temp = (Node)redoStack.pop();
redoStack.push(TWO);
if (condition == 0){
insert(temp);
} else {
delete(temp);
}
}
}
public void printPreOrder(){
String output = "";
output = printer(root, output);
if (output.length() > 1){
output = output.substring(0, output.length()-1);
}
System.out.println(output);
}
private String printer (Node node, String output){
if (node != null){
output = output + node.key + " ";
output = printer(node.left, output);
output = printer(node.right, output);
}
return output;
}
@Override
public void print() {
printPreOrder();
}
public static class Node {
// These fields are public for grading purposes. By coding conventions and best practice they should be private.
public Node left;
public Node right;
private Node parent;
private int key;
private Object value;
public Node(int key, Object value) {
this.key = key;
this.value = value;
}
public int getKey() {
return key;
}
public Object getValue() {
return value;
}
}
}