Skip to content

Commit 6f3fb36

Browse files
resolved spotbugs
1 parent 7eeb225 commit 6f3fb36

File tree

2 files changed

+73
-29
lines changed

2 files changed

+73
-29
lines changed

src/main/java/com/thealgorithms/datastructures/trees/BinaryTree.java

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*
1515
* @author Unknown
1616
*/
17-
public class BinaryTree {
17+
public
18+
class BinaryTree {
1819

1920
/**
2021
* This class implements the nodes that will go on the Binary Tree. They
@@ -28,19 +29,23 @@ static class Node {
2829
/**
2930
* Data for the node
3031
*/
31-
public int data;
32+
public
33+
int data;
3234
/**
3335
* The Node to the left of this one
3436
*/
35-
public Node left;
37+
public
38+
Node left;
3639
/**
3740
* The Node to the right of this one
3841
*/
39-
public Node right;
42+
public
43+
Node right;
4044
/**
4145
* The parent of this node
4246
*/
43-
public Node parent;
47+
public
48+
Node parent;
4449

4550
/**
4651
* Constructor of Node
@@ -59,19 +64,25 @@ static class Node {
5964
* The root of the Binary Tree
6065
*/
6166
private Node root;
67+
private
68+
int size; // Variable to keep track of the number of nodes
6269

6370
/**
6471
* Constructor
6572
*/
66-
public BinaryTree() {
73+
public
74+
BinaryTree() {
6775
root = null;
76+
size = 0; // Initialize size to 0
6877
}
6978

7079
/**
7180
* Parameterized Constructor
7281
*/
73-
public BinaryTree(Node root) {
82+
public
83+
BinaryTree(Node root) {
7484
this.root = root;
85+
this.size = (root != null) ? 1 : 0; // Initialize size based on root
7586
}
7687

7788
/**
@@ -80,7 +91,8 @@ public BinaryTree(Node root) {
8091
* @param key Value being looked for
8192
* @return The node if it finds it, otherwise returns the parent
8293
*/
83-
public Node find(int key) {
94+
public
95+
Node find(int key) {
8496
Node current = root;
8597
while (current != null) {
8698
if (key < current.data) {
@@ -101,26 +113,30 @@ public Node find(int key) {
101113
}
102114

103115
/**
104-
* Inserts certain value into the Binary Tree
116+
* Inserts a certain value into the Binary Tree
105117
*
106118
* @param value Value to be inserted
107119
*/
108-
public void put(int value) {
120+
public
121+
void put(int value) {
109122
Node newNode = new Node(value);
110123
if (root == null) {
111124
root = newNode;
125+
size++; // Increment size when inserting the first node
112126
} else {
113127
// This will return the soon to be parent of the value you're inserting
114128
Node parent = find(value);
115129

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
117132
if (value < parent.data) {
118133
parent.left = newNode;
119134
parent.left.parent = parent;
120135
} else {
121136
parent.right = newNode;
122137
parent.right.parent = parent;
123138
}
139+
size++; // Increment size on insertion
124140
}
125141
}
126142

@@ -130,26 +146,29 @@ public void put(int value) {
130146
* @param value Value to be deleted
131147
* @return If the value was deleted
132148
*/
133-
public boolean remove(int value) {
149+
public
150+
boolean remove(int value) {
134151
// temp is the node to be deleted
135152
Node temp = find(value);
136153

137154
// If the value doesn't exist
138-
if (temp.data != value) {
155+
if (temp == null || temp.data != value) {
139156
return false;
140157
}
141158

142159
// No children
143160
if (temp.right == null && temp.left == null) {
144161
if (temp == root) {
145162
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
148166
else if (temp.parent.data < temp.data) {
149167
temp.parent.right = null;
150168
} else {
151169
temp.parent.left = null;
152170
}
171+
size--; // Decrement size on removal
153172
return true;
154173
} // Two children
155174
else if (temp.left != null && temp.right != null) {
@@ -159,7 +178,8 @@ else if (temp.left != null && temp.right != null) {
159178
successor.left = temp.left;
160179
successor.left.parent = successor;
161180

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
163183
if (successor.parent != temp) {
164184
if (successor.right != null) {
165185
successor.right.parent = successor.parent;
@@ -178,21 +198,24 @@ else if (temp.left != null && temp.right != null) {
178198
else {
179199
successor.parent = temp.parent;
180200

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
183203
if (temp.parent.data < temp.data) {
184204
temp.parent.right = successor;
185205
} else {
186206
temp.parent.left = successor;
187207
}
188208
}
209+
size--; // Decrement size on removal
189210
return true;
190211
} // One child
191212
else {
192213
// If it has a right child
193214
if (temp.right != null) {
194215
if (temp == root) {
195216
root = temp.right;
217+
root.parent = null; // Update parent reference
218+
size--; // Decrement size on removal
196219
return true;
197220
}
198221

@@ -208,6 +231,8 @@ else if (temp.left != null && temp.right != null) {
208231
else {
209232
if (temp == root) {
210233
root = temp.left;
234+
root.parent = null; // Update parent reference
235+
size--; // Decrement size on removal
211236
return true;
212237
}
213238

@@ -220,6 +245,7 @@ else if (temp.left != null && temp.right != null) {
220245
temp.parent.right = temp.left;
221246
}
222247
}
248+
size--; // Decrement size on removal
223249
return true;
224250
}
225251
}
@@ -231,7 +257,8 @@ else if (temp.left != null && temp.right != null) {
231257
* @param n Node that you want to find the Successor of
232258
* @return The Successor of the node
233259
*/
234-
public Node findSuccessor(Node n) {
260+
public
261+
Node findSuccessor(Node n) {
235262
if (n.right == null) {
236263
return n;
237264
}
@@ -249,8 +276,17 @@ public Node findSuccessor(Node n) {
249276
*
250277
* @return the root of the Binary Tree
251278
*/
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
254290
}
255291

256292
/**
@@ -259,7 +295,8 @@ public Node getRoot() {
259295
*
260296
* @param localRoot The local root of the binary tree
261297
*/
262-
public void inOrder(Node localRoot) {
298+
public
299+
void inOrder(Node localRoot) {
263300
if (localRoot != null) {
264301
inOrder(localRoot.left);
265302
System.out.print(localRoot.data + " ");
@@ -272,7 +309,8 @@ public void inOrder(Node localRoot) {
272309
*
273310
* @param localRoot The local root of the binary tree
274311
*/
275-
public void preOrder(Node localRoot) {
312+
public
313+
void preOrder(Node localRoot) {
276314
if (localRoot != null) {
277315
System.out.print(localRoot.data + " ");
278316
preOrder(localRoot.left);
@@ -285,7 +323,8 @@ public void preOrder(Node localRoot) {
285323
*
286324
* @param localRoot The local root of the binary tree
287325
*/
288-
public void postOrder(Node localRoot) {
326+
public
327+
void postOrder(Node localRoot) {
289328
if (localRoot != null) {
290329
postOrder(localRoot.left);
291330
postOrder(localRoot.right);
@@ -300,11 +339,12 @@ public void postOrder(Node localRoot) {
300339
*
301340
* @param localRoot The local root of the binary tree
302341
*/
303-
public void bfs(Node localRoot) {
342+
public
343+
void bfs(Node localRoot) {
304344
// Create a queue for the order of the nodes
305345
Queue<Node> queue = new LinkedList<>();
306346

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
308348
// and won't do anything
309349
if (localRoot != null) {
310350
queue.add(localRoot);

src/test/java/com/thealgorithms/datastructures/trees/BinaryTreeTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ void test1() {
2222
Assertions.assertEquals(7, t.find(7).data);
2323
}
2424

25-
// Test for removing data and checking the new root
2625
@Test
2726
void test2() {
2827
BinaryTree t = new BinaryTree();
@@ -39,10 +38,15 @@ void test2() {
3938

4039
BinaryTree.Node root = t.getRoot();
4140
Assertions.assertNotNull(root, "Root should not be null after removals.");
42-
// Removed redundant null check
43-
Assertions.assertEquals(9, root.data); // Check if new root is correct
41+
42+
// Check the size of the tree to confirm it has remaining nodes
43+
Assertions.assertEquals(1, t.size(), "Tree should have 1 node left after removals.");
44+
45+
// Check if new root is correct
46+
Assertions.assertEquals(9, root.data);
4447
}
4548

49+
4650
// Test for attempting to remove a nonexistent node
4751
@Test
4852
void test3() {

0 commit comments

Comments
 (0)