-
Notifications
You must be signed in to change notification settings - Fork 1
/
BST.java
280 lines (228 loc) · 6.57 KB
/
BST.java
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
/**
* Node for Binary Tree
*
* @author Huiping Cao
*/
class BSTNode{
private int data; //the element value for this node
private BST left; //the left child of this node
private BST right; //the right child of this node
/**
* No-argument constructor
*/
public BSTNode(){}
/**
* Constructor with the initial element
* @param initData: the initial element
*/
public BSTNode(int initData){
data = initData;
left = new BST();
right = new BST();
}
/**
* Constructor with the initial element, initial left and right children
* @param initData: the initial element
* @param initParent: the initial parent tree
* @param initLeft: left child
* @param initRight: right child
*/
public BSTNode(int initData, BST initLeft, BST initRight){
data = initData;
left = initLeft;
right = initRight;
}
/**
* Evaluate whether this node is a leaf node or not
* @return true if it is a leaf node; otherwise, return false.
*/
public boolean isLeaf()
{
return (((left==null)||(left!=null && left.root==null))
&&((right==null)||(right!=null && right.root==null)));
}
/**
* @return the data
*/
public int getData() {
return data;
}
/**
* @param set the element in this node
*/
public void setData(int data) {
this.data = data;
}
/**
* @return the left child
*/
public BST getLeft() {
return left;
}
/**
* @param the left child to be set
*/
public void setLeft(BST left) {
this.left = left;
}
/**
* @return the right child
*/
public BST getRight() {
return right;
}
/**
* @param the right child to be set
*/
public void setRight(BST right) {
this.right = right;
}
/**
* Convert this BTNode to a string
*/
public String toString()
{
String str="";
if((left==null)||(left!=null && left.root==null)) str +="(null)";
else str +="("+left.root.getData()+")";
str += data;
if((right==null)||(right!=null&&right.root==null)) str +="(null)";
else str +="("+right.root.getData()+")";
return str;
}
}
/**
* The class for Binary Search Tree
* @author Huiping Cao
*
*/
public class BST {
protected BSTNode root; //the tree root
/**
* Get the left subtree of this tree
*
* @return the left subtree of this tree
*/
private BST getLeftSubtree() {
return root.getLeft();
}
/**
* Get the right subtree of this tree
*
* @return the right subtree of this tree
*/
private BST getRightSubtree() {
return root.getRight();
}
/**
* Print the tree using in-order traversal strategy
*/
public void inOrderPrt() {
inOrderPrt(0);
}
/**
* private, recursive function
* I slightly changed the method to print right subtree first
* It is to make the display more easier to read
*
* @param node
* @param indentation
* @param branch
*/
private void inOrderPrt(int indentation) {
if (root != null) {
if (getRightSubtree() != null) getRightSubtree().inOrderPrt(indentation + 1);
for (int i = 0; i < indentation * 4; i++) System.out.print(" ");
System.out.println(root.getData());
if (getLeftSubtree() != null) getLeftSubtree().inOrderPrt(indentation + 1);
}
}
/**
* Debug function, print the tree for debugging purpose
*/
public String toString() {
if (root != null) return root.toString();
else return null;
}
///////////////////////////////////////
///////////////////////////////////////
// Please add the functions required for your lab homework here.
public BSTNode searchNonRecursion(int e) {
if (root == null)
return null;
BST rover = this;
while (rover.root != null) {
if (e == rover.root.getData())
return rover.root;
else if (e < rover.root.getData())
rover = rover.root.getLeft();
else
rover = rover.root.getRight();
}
return null;
}
public BSTNode searchRecursion(int e) {
if (root == null) return null;
if (e == root.getData())
return root;
else if (e < root.getData())
return getLeftSubtree().searchRecursion(e);
else
return getRightSubtree().searchRecursion(e);
}
public boolean insert(int e) {
if (searchRecursion(e) != null)
return false;
if (root == null) {
root = new BSTNode(e);
return true;
}
if (e == root.getData())
return false;
if (e < root.getData())
return getLeftSubtree().insert(e);
else
return getRightSubtree().insert(e);
}
private int getMaxDataLeft() {
BST roverPrev = this;
BST rover = this.getLeftSubtree();
int max = root.getData();
while (rover.root != null) {
max = rover.root.getData();
roverPrev = rover;
rover = rover.getRightSubtree();
}
roverPrev.root = null;
return max;
}
public boolean remove(int e) {
if (searchRecursion(e) == null)
return false;
BST rover = this;
while (rover.root != null) {
if (e == rover.root.getData())
break;
else if (e < rover.root.getData())
rover = rover.getLeftSubtree();
else
rover = rover.getRightSubtree();
}
if (rover.getLeftSubtree().root == null && rover.getRightSubtree().root == null) {
rover.root = null;
} else if (rover.getLeftSubtree().root == null && rover.getRightSubtree().root != null) {
rover.root = rover.getRightSubtree().root;
} else if (rover.getLeftSubtree().root != null && rover.getRightSubtree().root == null) {
rover.root = rover.getLeftSubtree().root;
} else if (rover.getLeftSubtree().root != null && rover.getRightSubtree().root != null) {
int maxDataLeft = rover.getMaxDataLeft();
rover.root.setData(maxDataLeft);
}
return true;
}
public int sum() {
if (root == null)
return 0;
return root.getData() + getLeftSubtree().sum() + getRightSubtree().sum();
}
}