-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
321 lines (293 loc) · 8.88 KB
/
BinaryTree.java
File metadata and controls
321 lines (293 loc) · 8.88 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
public class BinaryTree<E extends Comparable<E>>{
private int size;
private Node<E> root;
/**
* Creates a BinaryTree with size = 0 and
* a null root
*/
public BinaryTree(){
size = 0;
root = null;
}
public BinaryTree(E val){
add(val);
}
private static class Node<E>{
private E value;
private Node<E> leftChild;
private Node<E> rightChild;
/**
* Creates a node with a left child, value, and right child
* @param left the left child of this node
* @param val the value of this node
* @param right the right child of this node
*/
private Node(Node<E> left, E val, Node<E> right){
value = val;
leftChild = left;
rightChild = right;
}
/**
* Creates a node with a given value and sets the
* children to null
* @param val the value to be contained in the node
*/
private Node(E val){
this(null, val, null);
}
/**
* Returns the value of this node
*/
private E getValue(){
return value;
}
/**
* Sets the nodes value to the specified value
*/
private void setValue(E newVal){
value = newVal;
}
/**
* Returns the left child of this node
*/
private Node<E> getLeft(){
return leftChild;
}
/**
* Returns the right child of this node
*/
private Node<E> getRight(){
return rightChild;
}
/**
* Sets this node's left child to left
* @param left the new left child
*/
private void setLeft(Node<E> left){
leftChild = left;
}
/**
* Sets this node's right child to right
* @param right the new right child of this node
*/
private void setRight(Node<E> right){
rightChild = right;
}
private int numChildren(){
final int ZERO_CHILDREN = 0;
final int ONE_CHILD = 1;
final int TWO_CHILDREN = 2;
if(this.getRight() == null && this.getLeft() == null){
//no children
return ZERO_CHILDREN;
}
else if(this.getRight() == null ^ this.getLeft() == null){
//one child
return ONE_CHILD;
}
else{
//two children
return TWO_CHILDREN;
}
}
}
/**
*
* @return the number of nodes in the tree
*/
int size(){
return size;
}
/**
* creates a node and inserts it into the binary tree
* @param val the value to be added to the tree
*/
public void add(E val){
checkValNull(val);
root = insert(root, val);
size++;
}
/**
* A function which inserts the given value into the
* tree
* @param root begins as root of tree, becomes nodes along the path to val
* @param val the value to be inserted into the tree
* @return
*/
private Node<E> insert(Node<E> node, E val){
if(node == null){
//base case, make the new node
node = new Node<E>(val);
}
else{
//recursive
int comp = node.getValue().compareTo(val);
if(comp > 0){
//val < root.val, go left
node.setLeft(insert(node.getLeft(), val));
}
else{
//val > root.val, go right
node.setRight(insert(node.getRight(), val));
}
//node is duplicate
}
return node;
}
/**
* Removes a given value from the tree
* @param val the val to be removed from the tree
*/
public void remove(E val){
checkValNull(val);
root = removeRecur(root, val);
size--;
}
private Node<E> removeRecur(Node<E> current, E val){
if(current == null){
//root is null, tree is empty
return current;
}
else{
//recur down tree
int comp = current.getValue().compareTo(val);
if(comp > 0){
//val < current.val, go left
current.setLeft(removeRecur(current.getLeft(), val));
}
else if(comp < 0){
//val > current.val, go right
current.setRight(removeRecur(current.getRight(), val));
}
else{
//val == current.val, remove node
switch(current.numChildren()){
case 0:
//no children; make this node empty
current = null;
break;
case 1:
//one child; make this node the child node
current = current.getLeft() == null ? current.getRight() : current.getLeft();
break;
case 2:
//two children; change this node to next in order
Node<E> temp = current.getRight();
while(temp.getLeft() != null){
//until leftmost leaf is found
temp = temp.getLeft();
}
//set
current.setValue(temp.getValue());
//remove extraneous node
current.setRight(removeRecur(current.getRight(), temp.getValue()));
break;
}
}
return current;
}
}
/**
* Finds the given node with the given val in the tree
* @param val the value being searched for
* @return the node with the given val or
* null if the that val is not in the tree
*/
public E get(E val){
checkValNull(val);
Node<E> temp = root;
while (temp != null && !temp.getValue().equals(val)){
// until the proper child is found or fell out of tree
int comp = temp.getValue().compareTo(val);
if(comp > 0){
// temp.val > val, go left
temp = temp.getLeft();
}
else if(comp < 0){
// temp.val < val, go right
temp = temp.getRight();
}
}
return temp.getValue();
}
/**
* Creates a preOrder string of the elements in the tree
*/
public String preOrder(){
StringBuilder str = new StringBuilder();
recurPre(str, root);
return str.toString();
}
private void recurPre(StringBuilder str, Node<E> current){
if(current == null){
//base case, do not add anything to string
}
else{
//recursive
str.append(current.getValue().toString());
str.append(", ");
recurPre(str, current.getLeft());
recurPre(str, current.getRight());
}
}
/**
*
* @return a string of the elements in the tree inorder
*/
public String inOrder(){
StringBuilder str = new StringBuilder();
recurIn(str, root);
return str.toString();
}
private void recurIn(StringBuilder str, Node<E> current){
if(current == null){
//base case, do not add anything
}
else{
//recursive
recurIn(str, current.getLeft());
str.append(current.getValue().toString());
str.append(", ");
recurIn(str, current.getRight());
}
}
/**
*
* @return a string of the values in this tree in postorder
*/
public String postOrder(){
StringBuilder str = new StringBuilder();
recurPost(str, root);
return str.toString();
}
private void recurPost(StringBuilder str, Node<E> current){
if(current == null){
//base case, do not add anything
}
else{
//recursive
recurPost(str, current.getLeft());
recurPost(str, current.getRight());
str.append(current.getValue().toString());
str.append(", ");
}
}
@Override
public String toString(){
StringBuilder ret = new StringBuilder();
ret.append("Pre: " + preOrder() + "\n");
ret.append("In: " + inOrder() + "\n");
ret.append("Post: " + postOrder());
return ret.toString();
}
/**
* Checks to see if the given val is null and throws
* IAE if it is
* @param val the value to be checked
*/
private void checkValNull(E val){
if(val == null){
throw new IllegalArgumentException("This tree can not store null values!");
}
}
}