Skip to content

Commit 1d8fa2c

Browse files
committed
Merged with upstream and resolved conflicts in config.json
2 parents e8b4dff + edba888 commit 1d8fa2c

File tree

7 files changed

+310
-1
lines changed

7 files changed

+310
-1
lines changed

config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"robot-simulator",
5353
"bracket-push",
5454
"pythagorean-triplet",
55+
"binary-search-tree",
5556
"binary-search"
5657
],
5758
"exercises": [
@@ -295,6 +296,11 @@
295296
"difficulty": 1,
296297
"topics": []
297298
},
299+
{
300+
"slug": "binary-search-tree",
301+
"difficulty": 1,
302+
"topics": []
303+
},
298304
{
299305
"slug": "binary-search",
300306
"difficulty": 1,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.12"
11+
}
12+
13+
test {
14+
testLogging {
15+
exceptionFormat = 'full'
16+
events = ["passed", "failed", "skipped"]
17+
}
18+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
import java.util.ArrayList;
3+
import java.util.Collections;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
8+
public class BST<T extends Comparable<T>> {
9+
10+
public static class Node<T> {
11+
12+
private T data;
13+
private Node<T> left = null;
14+
private Node<T> right = null;
15+
16+
public Node(T data) {
17+
this.data = data;
18+
}
19+
20+
public Node<T> getLeft() {
21+
return left;
22+
}
23+
24+
public void setLeft(Node<T> left) {
25+
this.left = left;
26+
}
27+
28+
public Node<T> getRight() {
29+
return right;
30+
}
31+
32+
public void setRight(Node<T> right) {
33+
this.right = right;
34+
}
35+
36+
public T getData() {
37+
return data;
38+
}
39+
40+
}
41+
42+
private Node<T> root;
43+
44+
private int nodeCount = 0;
45+
46+
public void insert(T value) {
47+
if (root == null) {
48+
root = new Node<>(value);
49+
} else {
50+
this.insert(this.root, value);
51+
}
52+
this.nodeCount++;
53+
}
54+
55+
public List<T> getAsSortedList() {
56+
List<T> result = new ArrayList<>(this.nodeCount);
57+
this.putInSortedOrderToList(this.root, result);
58+
return Collections.unmodifiableList(result);
59+
}
60+
61+
public List<T> getAsLevelOrderList() {
62+
List<T> result = new ArrayList<>(this.nodeCount);
63+
this.putInLevelOrderToList(this.root, result);
64+
return Collections.unmodifiableList(result);
65+
}
66+
67+
public Node<T> getRoot() {
68+
return root;
69+
}
70+
71+
private void insert(Node<T> node, T value) {
72+
if (value.compareTo(node.getData()) <= 0) {
73+
if (node.getLeft() == null) {
74+
node.setLeft(new Node<T>(value));
75+
} else {
76+
insert(node.getLeft(), value);
77+
}
78+
} else {
79+
if (node.getRight() == null) {
80+
node.setRight(new Node<T>(value));
81+
} else {
82+
insert(node.getRight(), value);
83+
}
84+
}
85+
}
86+
87+
private void putInSortedOrderToList(Node<T> node, List<T> list) {
88+
if (node == null || list == null) {
89+
return;
90+
}
91+
if (node.getLeft() != null) {
92+
putInSortedOrderToList(node.getLeft(), list);
93+
}
94+
list.add(node.getData());
95+
if (node.getRight() != null) {
96+
putInSortedOrderToList(node.getRight(), list);
97+
}
98+
}
99+
100+
private void putInLevelOrderToList(Node<T> node, List<T> list) {
101+
if (node == null || list == null) {
102+
return;
103+
}
104+
final Queue<Node<T>> queue = new LinkedList<>();
105+
Node<T> myNode;
106+
Node<T> left;
107+
Node<T> right;
108+
queue.add(node);
109+
while (!queue.isEmpty()) {
110+
myNode = queue.poll();
111+
list.add(myNode.getData());
112+
left = myNode.getLeft();
113+
right = myNode.getRight();
114+
if (left != null) {
115+
queue.add(left);
116+
}
117+
if (right != null) {
118+
queue.add(right);
119+
}
120+
}
121+
}
122+
}

exercises/binary-search-tree/src/main/java/.keep

Whitespace-only changes.

exercises/binary-search-tree/src/test/java/.keep

Whitespace-only changes.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
2+
import java.util.Arrays;
3+
import java.util.Collections;
4+
import java.util.List;
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertNotNull;
7+
import org.junit.Ignore;
8+
import org.junit.Test;
9+
10+
public class BSTTest {
11+
12+
@Test
13+
public void dataIsRetained() {
14+
BST<Integer> sut = new BST();
15+
final int actual = 4;
16+
sut.insert(actual);
17+
final BST.Node<Integer> root = sut.getRoot();
18+
assertNotNull(root);
19+
final int expected = root.getData();
20+
assertEquals(expected, actual);
21+
}
22+
23+
@Test
24+
@Ignore
25+
public void insertsLess() {
26+
BST<Integer> sut = new BST();
27+
final int expectedRoot = 4;
28+
final int expectedLeft = 2;
29+
30+
sut.insert(expectedRoot);
31+
sut.insert(expectedLeft);
32+
33+
final BST.Node<Integer> root = sut.getRoot();
34+
assertNotNull(root);
35+
final BST.Node<Integer> left = root.getLeft();
36+
assertNotNull(left);
37+
38+
final int actualRoot = root.getData();
39+
final int actualLeft = left.getData();
40+
assertEquals(expectedLeft, actualLeft);
41+
assertEquals(expectedRoot, actualRoot);
42+
}
43+
44+
@Test
45+
@Ignore
46+
public void insertsSame() {
47+
BST<Integer> sut = new BST();
48+
final int expectedRoot = 4;
49+
final int expectedLeft = 4;
50+
51+
sut.insert(expectedRoot);
52+
sut.insert(expectedLeft);
53+
54+
final BST.Node<Integer> root = sut.getRoot();
55+
assertNotNull(root);
56+
final BST.Node<Integer> left = root.getLeft();
57+
assertNotNull(left);
58+
59+
final int actualRoot = root.getData();
60+
final int actualLeft = left.getData();
61+
assertEquals(expectedLeft, actualLeft);
62+
assertEquals(expectedRoot, actualRoot);
63+
}
64+
65+
@Test
66+
@Ignore
67+
public void insertsRight() {
68+
BST<Integer> sut = new BST();
69+
final int expectedRoot = 4;
70+
final int expectedRight = 5;
71+
72+
sut.insert(expectedRoot);
73+
sut.insert(expectedRight);
74+
75+
final BST.Node<Integer> root = sut.getRoot();
76+
assertNotNull(root);
77+
final BST.Node<Integer> right = root.getRight();
78+
assertNotNull(right);
79+
80+
final int actualRoot = root.getData();
81+
final int actualRight = right.getData();
82+
assertEquals(expectedRight, actualRight);
83+
assertEquals(expectedRoot, actualRoot);
84+
}
85+
86+
@Test
87+
@Ignore
88+
public void createsComplexTree() {
89+
BST<Integer> sut = new BST<>();
90+
List<Integer> expected = Collections.unmodifiableList(
91+
Arrays.asList(4, 2, 6, 1, 3, 5, 7)
92+
);
93+
94+
List<Integer> treeData = Collections.unmodifiableList(
95+
Arrays.asList(4, 2, 6, 1, 3, 7, 5)
96+
);
97+
treeData.forEach(value -> sut.insert(value));
98+
99+
List<Integer> actual = sut.getAsLevelOrderList();
100+
assertEquals(expected, actual);
101+
}
102+
103+
@Test
104+
@Ignore
105+
public void sortsSingleElement() {
106+
BST<Integer> sut = new BST<>();
107+
List<Integer> expected = Collections.unmodifiableList(
108+
Arrays.asList(4)
109+
);
110+
111+
sut.insert(4);
112+
113+
List<Integer> actual = sut.getAsSortedList();
114+
assertEquals(expected, actual);
115+
}
116+
117+
@Test
118+
@Ignore
119+
public void sortsCollectionOfTwoIfSecondInsertedIsSmallerThanFirst() {
120+
BST<Integer> sut = new BST<>();
121+
List<Integer> expected = Collections.unmodifiableList(
122+
Arrays.asList(2, 4)
123+
);
124+
125+
sut.insert(4);
126+
sut.insert(2);
127+
128+
List<Integer> actual = sut.getAsSortedList();
129+
assertEquals(expected, actual);
130+
}
131+
132+
@Test
133+
@Ignore
134+
public void sortsCollectionOfTwoIfSecondInsertedIsBiggerThanFirst() {
135+
BST<Integer> sut = new BST<>();
136+
List<Integer> expected = Collections.unmodifiableList(
137+
Arrays.asList(4, 5)
138+
);
139+
140+
sut.insert(4);
141+
sut.insert(5);
142+
143+
List<Integer> actual = sut.getAsSortedList();
144+
assertEquals(expected, actual);
145+
}
146+
147+
@Test
148+
@Ignore
149+
public void iteratesOverComplexTree() {
150+
BST<Integer> sut = new BST<>();
151+
List<Integer> expected = Collections.unmodifiableList(
152+
Arrays.asList(1, 2, 3, 4, 5, 6, 7)
153+
);
154+
155+
List<Integer> treeData = Collections.unmodifiableList(
156+
Arrays.asList(4, 2, 1, 3, 6, 7, 5)
157+
);
158+
treeData.forEach(value -> sut.insert(value));
159+
160+
List<Integer> actual = sut.getAsSortedList();
161+
assertEquals(expected, actual);
162+
}
163+
}

exercises/settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ include 'anagram'
55
include 'atbash-cipher'
66
include 'beer-song'
77
include 'binary'
8-
include 'binary-search'
8+
include 'binary-search-tree'
99
include 'bob'
1010
include 'bracket-push'
1111
include 'crypto-square'

0 commit comments

Comments
 (0)