14
14
*
15
15
* @author Unknown
16
16
*/
17
- public class BinaryTree {
17
+ public
18
+ class BinaryTree {
18
19
19
20
/**
20
21
* This class implements the nodes that will go on the Binary Tree. They
@@ -28,19 +29,23 @@ static class Node {
28
29
/**
29
30
* Data for the node
30
31
*/
31
- public int data ;
32
+ public
33
+ int data ;
32
34
/**
33
35
* The Node to the left of this one
34
36
*/
35
- public Node left ;
37
+ public
38
+ Node left ;
36
39
/**
37
40
* The Node to the right of this one
38
41
*/
39
- public Node right ;
42
+ public
43
+ Node right ;
40
44
/**
41
45
* The parent of this node
42
46
*/
43
- public Node parent ;
47
+ public
48
+ Node parent ;
44
49
45
50
/**
46
51
* Constructor of Node
@@ -59,19 +64,25 @@ static class Node {
59
64
* The root of the Binary Tree
60
65
*/
61
66
private Node root ;
67
+ private
68
+ int size ; // Variable to keep track of the number of nodes
62
69
63
70
/**
64
71
* Constructor
65
72
*/
66
- public BinaryTree () {
73
+ public
74
+ BinaryTree () {
67
75
root = null ;
76
+ size = 0 ; // Initialize size to 0
68
77
}
69
78
70
79
/**
71
80
* Parameterized Constructor
72
81
*/
73
- public BinaryTree (Node root ) {
82
+ public
83
+ BinaryTree (Node root ) {
74
84
this .root = root ;
85
+ this .size = (root != null ) ? 1 : 0 ; // Initialize size based on root
75
86
}
76
87
77
88
/**
@@ -80,7 +91,8 @@ public BinaryTree(Node root) {
80
91
* @param key Value being looked for
81
92
* @return The node if it finds it, otherwise returns the parent
82
93
*/
83
- public Node find (int key ) {
94
+ public
95
+ Node find (int key ) {
84
96
Node current = root ;
85
97
while (current != null ) {
86
98
if (key < current .data ) {
@@ -101,26 +113,30 @@ public Node find(int key) {
101
113
}
102
114
103
115
/**
104
- * Inserts certain value into the Binary Tree
116
+ * Inserts a certain value into the Binary Tree
105
117
*
106
118
* @param value Value to be inserted
107
119
*/
108
- public void put (int value ) {
120
+ public
121
+ void put (int value ) {
109
122
Node newNode = new Node (value );
110
123
if (root == null ) {
111
124
root = newNode ;
125
+ size ++; // Increment size when inserting the first node
112
126
} else {
113
127
// This will return the soon to be parent of the value you're inserting
114
128
Node parent = find (value );
115
129
116
- // This if/else assigns the new node to be either the left or right child of the parent
130
+ // This if/else assigns the new node to be either the left or right child
131
+ // of the parent
117
132
if (value < parent .data ) {
118
133
parent .left = newNode ;
119
134
parent .left .parent = parent ;
120
135
} else {
121
136
parent .right = newNode ;
122
137
parent .right .parent = parent ;
123
138
}
139
+ size ++; // Increment size on insertion
124
140
}
125
141
}
126
142
@@ -130,26 +146,29 @@ public void put(int value) {
130
146
* @param value Value to be deleted
131
147
* @return If the value was deleted
132
148
*/
133
- public boolean remove (int value ) {
149
+ public
150
+ boolean remove (int value ) {
134
151
// temp is the node to be deleted
135
152
Node temp = find (value );
136
153
137
154
// If the value doesn't exist
138
- if (temp .data != value ) {
155
+ if (temp == null || temp .data != value ) {
139
156
return false ;
140
157
}
141
158
142
159
// No children
143
160
if (temp .right == null && temp .left == null ) {
144
161
if (temp == root ) {
145
162
root = null ;
146
- } // This if/else assigns the new node to be either the left or right child of the
147
- // parent
163
+ } // This if/else assigns the new node to be either the left or right
164
+ // child of the
165
+ // parent
148
166
else if (temp .parent .data < temp .data ) {
149
167
temp .parent .right = null ;
150
168
} else {
151
169
temp .parent .left = null ;
152
170
}
171
+ size --; // Decrement size on removal
153
172
return true ;
154
173
} // Two children
155
174
else if (temp .left != null && temp .right != null ) {
@@ -159,7 +178,8 @@ else if (temp.left != null && temp.right != null) {
159
178
successor .left = temp .left ;
160
179
successor .left .parent = successor ;
161
180
162
- // If the successor has a right child, the child's grandparent is it's new parent
181
+ // If the successor has a right child, the child's grandparent is its new
182
+ // parent
163
183
if (successor .parent != temp ) {
164
184
if (successor .right != null ) {
165
185
successor .right .parent = successor .parent ;
@@ -178,21 +198,24 @@ else if (temp.left != null && temp.right != null) {
178
198
else {
179
199
successor .parent = temp .parent ;
180
200
181
- // This if/else assigns the new node to be either the left or right child of the
182
- // parent
201
+ // This if/else assigns the new node to be either the left or right
202
+ // child of the parent
183
203
if (temp .parent .data < temp .data ) {
184
204
temp .parent .right = successor ;
185
205
} else {
186
206
temp .parent .left = successor ;
187
207
}
188
208
}
209
+ size --; // Decrement size on removal
189
210
return true ;
190
211
} // One child
191
212
else {
192
213
// If it has a right child
193
214
if (temp .right != null ) {
194
215
if (temp == root ) {
195
216
root = temp .right ;
217
+ root .parent = null ; // Update parent reference
218
+ size --; // Decrement size on removal
196
219
return true ;
197
220
}
198
221
@@ -208,6 +231,8 @@ else if (temp.left != null && temp.right != null) {
208
231
else {
209
232
if (temp == root ) {
210
233
root = temp .left ;
234
+ root .parent = null ; // Update parent reference
235
+ size --; // Decrement size on removal
211
236
return true ;
212
237
}
213
238
@@ -220,6 +245,7 @@ else if (temp.left != null && temp.right != null) {
220
245
temp .parent .right = temp .left ;
221
246
}
222
247
}
248
+ size --; // Decrement size on removal
223
249
return true ;
224
250
}
225
251
}
@@ -231,7 +257,8 @@ else if (temp.left != null && temp.right != null) {
231
257
* @param n Node that you want to find the Successor of
232
258
* @return The Successor of the node
233
259
*/
234
- public Node findSuccessor (Node n ) {
260
+ public
261
+ Node findSuccessor (Node n ) {
235
262
if (n .right == null ) {
236
263
return n ;
237
264
}
@@ -249,8 +276,17 @@ public Node findSuccessor(Node n) {
249
276
*
250
277
* @return the root of the Binary Tree
251
278
*/
252
- public Node getRoot () {
253
- return root ;
279
+ public
280
+ Node getRoot () { return root ; }
281
+
282
+ /**
283
+ * Returns the size of the Binary Tree
284
+ *
285
+ * @return the size of the Binary Tree
286
+ */
287
+ public
288
+ int size () {
289
+ return size ; // Return the current size of the tree
254
290
}
255
291
256
292
/**
@@ -259,7 +295,8 @@ public Node getRoot() {
259
295
*
260
296
* @param localRoot The local root of the binary tree
261
297
*/
262
- public void inOrder (Node localRoot ) {
298
+ public
299
+ void inOrder (Node localRoot ) {
263
300
if (localRoot != null ) {
264
301
inOrder (localRoot .left );
265
302
System .out .print (localRoot .data + " " );
@@ -272,7 +309,8 @@ public void inOrder(Node localRoot) {
272
309
*
273
310
* @param localRoot The local root of the binary tree
274
311
*/
275
- public void preOrder (Node localRoot ) {
312
+ public
313
+ void preOrder (Node localRoot ) {
276
314
if (localRoot != null ) {
277
315
System .out .print (localRoot .data + " " );
278
316
preOrder (localRoot .left );
@@ -285,7 +323,8 @@ public void preOrder(Node localRoot) {
285
323
*
286
324
* @param localRoot The local root of the binary tree
287
325
*/
288
- public void postOrder (Node localRoot ) {
326
+ public
327
+ void postOrder (Node localRoot ) {
289
328
if (localRoot != null ) {
290
329
postOrder (localRoot .left );
291
330
postOrder (localRoot .right );
@@ -300,11 +339,12 @@ public void postOrder(Node localRoot) {
300
339
*
301
340
* @param localRoot The local root of the binary tree
302
341
*/
303
- public void bfs (Node localRoot ) {
342
+ public
343
+ void bfs (Node localRoot ) {
304
344
// Create a queue for the order of the nodes
305
345
Queue <Node > queue = new LinkedList <>();
306
346
307
- // If the give root is null, then we don't add to the queue
347
+ // If the given root is null, then we don't add to the queue
308
348
// and won't do anything
309
349
if (localRoot != null ) {
310
350
queue .add (localRoot );
0 commit comments