3
3
import java .util .LinkedList ;
4
4
import java .util .Queue ;
5
5
6
- /*
7
- * This entire class is used to build a Binary Tree data structure. There is the
8
- * Node Class and the Tree Class, both explained below.
9
- */
10
6
/**
11
- * A binary tree is a data structure in which an element has two
12
- * successors(children). The left child is usually smaller than the parent, and
13
- * the right child is usually bigger.
7
+ * A binary tree data structure where elements have two successors (children).
8
+ * The left child is smaller than the parent, and the right child is larger.
14
9
*
15
10
* @author Unknown
16
11
*/
17
12
public class BinaryTree {
18
13
19
14
/**
20
- * This class implements the nodes that will go on the Binary Tree. They
21
- * consist of the data in them, the node to the left, the node to the right,
22
- * and the parent from which they came from.
23
- *
24
- * @author Unknown
15
+ * Node class represents elements in the Binary Tree, with pointers to
16
+ * the left and right children and a reference to its parent node.
25
17
*/
26
18
static class Node {
27
-
28
- /**
29
- * Data for the node
30
- */
31
19
public int data ;
32
- /**
33
- * The Node to the left of this one
34
- */
35
- public Node left ;
36
- /**
37
- * The Node to the right of this one
38
- */
39
- public Node right ;
40
- /**
41
- * The parent of this node
42
- */
43
- public Node parent ;
20
+ public Node left , right , parent ;
44
21
45
- /**
46
- * Constructor of Node
47
- *
48
- * @param value Value to put in the node
49
- */
50
- Node (int value ) {
22
+ public Node (int value ) {
51
23
data = value ;
52
- left = null ;
53
- right = null ;
54
- parent = null ;
24
+ left = right = parent = null ;
55
25
}
56
26
}
57
27
58
- /**
59
- * The root of the Binary Tree
60
- */
61
28
private Node root ;
62
29
63
- /**
64
- * Constructor
65
- */
66
30
public BinaryTree () {
67
31
root = null ;
68
32
}
69
33
70
- /**
71
- * Parameterized Constructor
72
- */
73
34
public BinaryTree (Node root ) {
74
35
this .root = root ;
75
36
}
76
37
77
- /**
78
- * Method to find a Node with a certain value
79
- *
80
- * @param key Value being looked for
81
- * @return The node if it finds it, otherwise returns the parent
82
- */
83
38
public Node find (int key ) {
84
39
Node current = root ;
85
40
while (current != null ) {
86
41
if (key < current .data ) {
87
- if (current .left == null ) {
88
- return current ; // The key isn't exist, returns the parent
89
- }
42
+ if (current .left == null ) return current ;
90
43
current = current .left ;
91
44
} else if (key > current .data ) {
92
- if (current .right == null ) {
93
- return current ;
94
- }
45
+ if (current .right == null ) return current ;
95
46
current = current .right ;
96
- } else { // If you find the value return it
47
+ } else {
97
48
return current ;
98
49
}
99
50
}
100
51
return null ;
101
52
}
102
53
103
- /**
104
- * Inserts certain value into the Binary Tree
105
- *
106
- * @param value Value to be inserted
107
- */
108
54
public void put (int value ) {
109
55
Node newNode = new Node (value );
110
56
if (root == null ) {
111
57
root = newNode ;
112
58
} else {
113
- // This will return the soon to be parent of the value you're inserting
114
59
Node parent = find (value );
115
-
116
- // This if/else assigns the new node to be either the left or right child of the parent
117
60
if (value < parent .data ) {
118
61
parent .left = newNode ;
119
62
parent .left .parent = parent ;
@@ -124,141 +67,59 @@ public void put(int value) {
124
67
}
125
68
}
126
69
127
- /**
128
- * Deletes a given value from the Binary Tree
129
- *
130
- * @param value Value to be deleted
131
- * @return If the value was deleted
132
- */
133
70
public boolean remove (int value ) {
134
- // temp is the node to be deleted
135
71
Node temp = find (value );
72
+ if (temp == null || temp .data != value ) return false ;
136
73
137
- // If the value doesn't exist
138
- if (temp .data != value ) {
139
- return false ;
140
- }
141
-
142
- // No children
143
- if (temp .right == null && temp .left == null ) {
144
- if (temp == root ) {
145
- root = null ;
146
- } // This if/else assigns the new node to be either the left or right child of the
147
- // parent
148
- else if (temp .parent .data < temp .data ) {
149
- temp .parent .right = null ;
150
- } else {
151
- temp .parent .left = null ;
152
- }
74
+ if (temp .left == null && temp .right == null ) {
75
+ if (temp == root ) root = null ;
76
+ else if (temp .parent .data < temp .data ) temp .parent .right = null ;
77
+ else temp .parent .left = null ;
153
78
return true ;
154
- } // Two children
155
- else if (temp .left != null && temp .right != null ) {
79
+ } else if (temp .left != null && temp .right != null ) {
156
80
Node successor = findSuccessor (temp );
157
-
158
- // The left tree of temp is made the left tree of the successor
159
81
successor .left = temp .left ;
160
82
successor .left .parent = successor ;
161
-
162
- // If the successor has a right child, the child's grandparent is it's new parent
163
83
if (successor .parent != temp ) {
164
84
if (successor .right != null ) {
165
85
successor .right .parent = successor .parent ;
166
86
successor .parent .left = successor .right ;
167
- } else {
168
- successor .parent .left = null ;
169
- }
87
+ } else successor .parent .left = null ;
170
88
successor .right = temp .right ;
171
89
successor .right .parent = successor ;
172
90
}
173
-
174
91
if (temp == root ) {
175
- successor .parent = null ;
176
92
root = successor ;
177
- } // If you're not deleting the root
178
- else {
93
+ successor . parent = null ;
94
+ } else {
179
95
successor .parent = temp .parent ;
180
-
181
- // This if/else assigns the new node to be either the left or right child of the
182
- // parent
183
- if (temp .parent .data < temp .data ) {
184
- temp .parent .right = successor ;
185
- } else {
186
- temp .parent .left = successor ;
187
- }
96
+ if (temp .parent .data < temp .data ) temp .parent .right = successor ;
97
+ else temp .parent .left = successor ;
188
98
}
189
99
return true ;
190
- } // One child
191
- else {
192
- // If it has a right child
193
- if (temp .right != null ) {
194
- if (temp == root ) {
195
- root = temp .right ;
196
- return true ;
197
- }
198
-
199
- temp .right .parent = temp .parent ;
200
-
201
- // Assigns temp to left or right child
202
- if (temp .data < temp .parent .data ) {
203
- temp .parent .left = temp .right ;
204
- } else {
205
- temp .parent .right = temp .right ;
206
- }
207
- } // If it has a left child
100
+ } else {
101
+ Node child = (temp .right != null ) ? temp .right : temp .left ;
102
+ if (temp == root ) root = child ;
208
103
else {
209
- if (temp == root ) {
210
- root = temp .left ;
211
- return true ;
212
- }
213
-
214
- temp .left .parent = temp .parent ;
215
-
216
- // Assigns temp to left or right side
217
- if (temp .data < temp .parent .data ) {
218
- temp .parent .left = temp .left ;
219
- } else {
220
- temp .parent .right = temp .left ;
221
- }
104
+ child .parent = temp .parent ;
105
+ if (temp .data < temp .parent .data ) temp .parent .left = child ;
106
+ else temp .parent .right = child ;
222
107
}
223
108
return true ;
224
109
}
225
110
}
226
111
227
- /**
228
- * This method finds the Successor to the Node given. Move right once and go
229
- * left down the tree as far as you can
230
- *
231
- * @param n Node that you want to find the Successor of
232
- * @return The Successor of the node
233
- */
234
112
public Node findSuccessor (Node n ) {
235
- if (n .right == null ) {
236
- return n ;
237
- }
113
+ if (n .right == null ) return n ;
238
114
Node current = n .right ;
239
- Node parent = n .right ;
240
- while (current != null ) {
241
- parent = current ;
242
- current = current .left ;
243
- }
244
- return parent ;
115
+ while (current .left != null ) current = current .left ;
116
+ return current ;
245
117
}
246
118
247
- /**
248
- * Returns the root of the Binary Tree
249
- *
250
- * @return the root of the Binary Tree
251
- */
252
119
public Node getRoot () {
253
120
return root ;
254
121
}
255
122
256
- /**
257
- * Prints leftChild - root - rightChild This is the equivalent of a depth
258
- * first search
259
- *
260
- * @param localRoot The local root of the binary tree
261
- */
262
123
public void inOrder (Node localRoot ) {
263
124
if (localRoot != null ) {
264
125
inOrder (localRoot .left );
@@ -267,11 +128,6 @@ public void inOrder(Node localRoot) {
267
128
}
268
129
}
269
130
270
- /**
271
- * Prints root - leftChild - rightChild
272
- *
273
- * @param localRoot The local root of the binary tree
274
- */
275
131
public void preOrder (Node localRoot ) {
276
132
if (localRoot != null ) {
277
133
System .out .print (localRoot .data + " " );
@@ -280,11 +136,6 @@ public void preOrder(Node localRoot) {
280
136
}
281
137
}
282
138
283
- /**
284
- * Prints leftChild - rightChild - root
285
- *
286
- * @param localRoot The local root of the binary tree
287
- */
288
139
public void postOrder (Node localRoot ) {
289
140
if (localRoot != null ) {
290
141
postOrder (localRoot .left );
@@ -293,38 +144,15 @@ public void postOrder(Node localRoot) {
293
144
}
294
145
}
295
146
296
- /**
297
- * Prints the tree in a breadth first search order This is similar to
298
- * pre-order traversal, but instead of being implemented with a stack (or
299
- * recursion), it is implemented with a queue
300
- *
301
- * @param localRoot The local root of the binary tree
302
- */
303
147
public void bfs (Node localRoot ) {
304
- // Create a queue for the order of the nodes
148
+ if ( localRoot == null ) return ;
305
149
Queue <Node > queue = new LinkedList <>();
306
-
307
- // If the give root is null, then we don't add to the queue
308
- // and won't do anything
309
- if (localRoot != null ) {
310
- queue .add (localRoot );
311
- }
312
-
313
- // Continue until the queue is empty
150
+ queue .add (localRoot );
314
151
while (!queue .isEmpty ()) {
315
- // Get the next node on the queue to visit
316
- localRoot = queue .remove ();
317
-
318
- // Print the data from the node we are visiting
319
- System .out .print (localRoot .data + " " );
320
-
321
- // Add the children to the queue if not null
322
- if (localRoot .right != null ) {
323
- queue .add (localRoot .right );
324
- }
325
- if (localRoot .left != null ) {
326
- queue .add (localRoot .left );
327
- }
152
+ Node node = queue .remove ();
153
+ System .out .print (node .data + " " );
154
+ if (node .left != null ) queue .add (node .left );
155
+ if (node .right != null ) queue .add (node .right );
328
156
}
329
157
}
330
158
}
0 commit comments