Skip to content

Commit 5e25e2c

Browse files
marcos-lgpivovarit
authored andcommitted
BAEL -1339 Implement a binary tree in Java (eugenp#3301)
* edge cases binary tree * code cleanup
1 parent 0a76081 commit 5e25e2c

2 files changed

Lines changed: 55 additions & 12 deletions

File tree

‎core-java/src/main/java/com/baeldung/tree/BinaryTree.java‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@ public class BinaryTree {
88
Node root;
99

1010
public void add(int value) {
11-
12-
Node newNode = new Node(value);
13-
14-
if (root == null) {
15-
root = newNode;
16-
return;
17-
}
18-
19-
addRecursive(root, value);
11+
root = addRecursive(root, value);
2012
}
2113

2214
private Node addRecursive(Node current, int value) {
@@ -27,18 +19,32 @@ private Node addRecursive(Node current, int value) {
2719

2820
if (value < current.value) {
2921
current.left = addRecursive(current.left, value);
30-
} else {
22+
} else if (value > current.value) {
3123
current.right = addRecursive(current.right, value);
24+
} else {
25+
// value already exists
26+
return current;
3227
}
3328

3429
return current;
35-
3630
}
3731

3832
public boolean isEmpty() {
3933
return root == null;
4034
}
4135

36+
public int getSize() {
37+
return getSizeRecursive(root);
38+
}
39+
40+
private int getSizeRecursive(Node current) {
41+
if (current == null) {
42+
return 0;
43+
}
44+
45+
return getSizeRecursive(current.left) + 1 + getSizeRecursive(current.right);
46+
}
47+
4248
public boolean containsNode(int value) {
4349
return containsNodeRecursive(root, value);
4450
}
@@ -125,12 +131,15 @@ public void traversePostOrder(Node node) {
125131
if (node != null) {
126132
traversePostOrder(node.left);
127133
traversePostOrder(node.right);
128-
129134
System.out.print(" " + node.value);
130135
}
131136
}
132137

133138
public void traverseLevelOrder() {
139+
if (root == null) {
140+
return;
141+
}
142+
134143
Queue<Node> nodes = new LinkedList<>();
135144
nodes.add(root);
136145

‎core-java/src/test/java/com/baeldung/tree/BinaryTreeTest.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.tree;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertFalse;
45
import static org.junit.Assert.assertTrue;
56

@@ -26,6 +27,26 @@ public void givenABinaryTree_WhenAddingElements_ThenTreeContainsThoseElements()
2627
assertFalse(bt.containsNode(1));
2728
}
2829

30+
@Test
31+
public void givenABinaryTree_WhenAddingExistingElement_ThenElementIsNotAdded() {
32+
33+
BinaryTree bt = createBinaryTree();
34+
35+
int initialSize = bt.getSize();
36+
37+
assertTrue(bt.containsNode(3));
38+
bt.add(3);
39+
assertEquals(initialSize, bt.getSize());
40+
}
41+
42+
@Test
43+
public void givenABinaryTree_WhenLookingForNonExistingElement_ThenReturnsFalse() {
44+
45+
BinaryTree bt = createBinaryTree();
46+
47+
assertFalse(bt.containsNode(99));
48+
}
49+
2950
@Test
3051
public void givenABinaryTree_WhenDeletingElements_ThenTreeDoesNotContainThoseElements() {
3152

@@ -36,6 +57,19 @@ public void givenABinaryTree_WhenDeletingElements_ThenTreeDoesNotContainThoseEle
3657
assertFalse(bt.containsNode(9));
3758
}
3859

60+
@Test
61+
public void givenABinaryTree_WhenDeletingNonExistingElement_ThenTreeDoesNotDelete() {
62+
63+
BinaryTree bt = createBinaryTree();
64+
65+
int initialSize = bt.getSize();
66+
67+
assertFalse(bt.containsNode(99));
68+
bt.delete(99);
69+
assertFalse(bt.containsNode(99));
70+
assertEquals(initialSize, bt.getSize());
71+
}
72+
3973
@Test
4074
public void givenABinaryTree_WhenTraversingInOrder_ThenPrintValues() {
4175

0 commit comments

Comments
 (0)