Skip to content

Commit 3aa2ffb

Browse files
committed
chapter 16
1 parent 4677daf commit 3aa2ffb

16 files changed

+666
-6
lines changed

arrays/src/test/java/SudokuCheckerTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public void isValidSudoku2() {
4040
Arrays.asList(7,0,0,0,2,0,0,0,6),
4141
Arrays.asList(0,6,0,0,0,0,2,8,0),
4242
Arrays.asList(0,0,0,4,1,9,0,0,5),
43-
Arrays.asList(0,0,0,0,8,0,0,7,9));
43+
Arrays.asList(0,0,0,0,8,0,0,7,9)
44+
);
4445

4546
test(expected, board);
4647
}
@@ -57,7 +58,8 @@ public void isValidSudoku3() {
5758
Arrays.asList(7,0,0,0,2,0,0,0,6),
5859
Arrays.asList(0,6,0,0,0,0,2,8,0),
5960
Arrays.asList(6,0,0,4,1,9,0,0,5),
60-
Arrays.asList(0,0,0,0,8,0,0,7,9));
61+
Arrays.asList(0,0,0,0,8,0,0,7,9)
62+
);
6163

6264
test(expected, board);
6365
}
@@ -74,7 +76,8 @@ public void isValidSudoku4() {
7476
Arrays.asList(7,0,0,0,2,0,0,0,6),
7577
Arrays.asList(0,6,0,0,0,0,2,8,0),
7678
Arrays.asList(0,0,0,4,1,9,0,0,5),
77-
Arrays.asList(0,0,0,0,8,0,0,7,9));
79+
Arrays.asList(0,0,0,0,8,0,0,7,9)
80+
);
7881

7982
test(expected, board);
8083
}

datastructures/src/main/java/TreeNode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
import java.util.List;
33

44
public class TreeNode {
5-
List<Edge> edges = new ArrayList<Edge>;
5+
public List<Edge> edges = new ArrayList<>();
66

77
public static class Edge {
88
public TreeNode root;
99
public double length;
1010

11+
public Edge(double length) {
12+
this.length = length;
13+
root = new TreeNode();
14+
}
15+
1116
public Edge(TreeNode root, double length) {
1217
this.root = root;
1318
this.length = length;

recursion/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
<artifactId>datastructures</artifactId>
1818
<version>1.0</version>
1919
</dependency>
20+
<dependency>
21+
<groupId>gardncl</groupId>
22+
<artifactId>utils</artifactId>
23+
<version>1.0</version>
24+
</dependency>
2025
</dependencies>
2126

2227

28+
2329
</project>

recursion/src/main/java/ComputeDiameter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class ComputeDiameter {
88
algorithm to compute the diameter of a tree.
99
*/
1010

11-
public static double computeDiameter(TreeNode T) {
11+
public static int computeDiameter(TreeNode T) {
1212

1313
return 0;
1414
}

recursion/src/main/java/GenerateBinaryTrees.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ public class GenerateBinaryTrees {
77
16.8
88
99
Write a program that returns all distinct binary
10-
trees with a specified number of nodes.
10+
trees with a specified number of nodes. All node
11+
data must be 0.
1112
*/
1213

1314
public static List<BinaryTree<Integer>> generateAllBinaryTrees(int numNodes) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import org.junit.Test;
2+
3+
import static org.junit.Assert.*;
4+
5+
public class ComputeDiameterTest {
6+
7+
private int expected;
8+
private TreeNode T;
9+
10+
@Test
11+
public void computeDiameter1() throws Exception {
12+
expected = 31;
13+
T = TreeNodeUtil.getTreeNode();
14+
15+
test(expected, T);
16+
}
17+
18+
private void test(int expected, TreeNode T) {
19+
assertEquals(expected, ComputeDiameter.computeDiameter(T));
20+
}
21+
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class ComputeGrayCodeTest {
9+
10+
private List<Integer> expected;
11+
private int numBits;
12+
13+
@Test
14+
public void grayCode1() throws Exception {
15+
expected = Arrays.asList(0,1,2,3);
16+
numBits = 2;
17+
18+
test(expected, numBits);
19+
}
20+
21+
@Test
22+
public void grayCode2() throws Exception {
23+
expected = Arrays.asList(0,1,3,2,6,7,5,4);
24+
numBits = 3;
25+
26+
test(expected, numBits);
27+
}
28+
29+
private void test(List<Integer> expected, int numBits) {
30+
AssertUtils.assertSameContentsInt(expected, ComputeGrayCode.grayCode(numBits));
31+
}
32+
33+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.assertEquals;
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class GenerateBinaryTreesTest {
10+
11+
private List<BinaryTree<Integer>> expected;
12+
private int numNodes;
13+
14+
@Test
15+
public void generateAllBinaryTrees1() throws Exception {
16+
expected = Arrays.asList(
17+
new BinaryTree<>(0)
18+
);
19+
numNodes = 1;
20+
21+
test(expected, numNodes);
22+
}
23+
24+
@Test
25+
public void generateAllBinaryTrees2() throws Exception {
26+
expected = Arrays.asList(
27+
new BinaryTree<>(0,null,new BinaryTree<>(0)),
28+
new BinaryTree<>(0,new BinaryTree<>(0),null)
29+
);
30+
numNodes = 2;
31+
32+
test(expected, numNodes);
33+
}
34+
35+
@Test
36+
public void generateAllBinaryTrees3() throws Exception {
37+
expected = Arrays.asList(
38+
new BinaryTree<>(0,null,new BinaryTree<>(0,new BinaryTree<>(0),null)),
39+
new BinaryTree<>(0,null,new BinaryTree<>(0,null, new BinaryTree<>(0))),
40+
new BinaryTree<>(0,new BinaryTree<>(0,new BinaryTree<>(0),null), null),
41+
new BinaryTree<>(0,new BinaryTree<>(0,null, new BinaryTree<>(0)), null),
42+
new BinaryTree<>(0,new BinaryTree<>(0),new BinaryTree<>(0))
43+
);
44+
numNodes = 3;
45+
46+
test(expected, numNodes);
47+
}
48+
49+
private void test(List<BinaryTree<Integer>> expected, int numNodes) {
50+
List<BinaryTree<Integer>> result = GenerateBinaryTrees.generateAllBinaryTrees(numNodes);
51+
assertEquals(expected.size(), result.size());
52+
expected.forEach( i -> {
53+
assertTrue(result.remove(i));
54+
});
55+
}
56+
57+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class GeneratePalindromicDecompositionsTest {
11+
12+
private List<List<String>> expected;
13+
private String input;
14+
15+
@Test
16+
public void palindromicPartitioning1() throws Exception {
17+
expected = Arrays.asList(
18+
Arrays.asList("6","1","1","1","1","6"),
19+
Arrays.asList("6","11","1","1","6"),
20+
Arrays.asList("6","1","1","11","6"),
21+
Arrays.asList("6","11","11","6"),
22+
Arrays.asList("6","1111","6")
23+
);
24+
input = "611116";
25+
26+
test(expected, input);
27+
}
28+
29+
@Test
30+
public void palindromicPartitioning2() throws Exception {
31+
expected = Arrays.asList(
32+
Arrays.asList("0","2","0","4","4","5","1","8","8","1"),
33+
Arrays.asList("0","2","0","44","5","1","8","8","1"),
34+
Arrays.asList("0","2","0","4","4","5","1","88","1"),
35+
Arrays.asList("0","2","0","4","4","5","1881"),
36+
Arrays.asList("0","2","0","44","5","1","88","1"),
37+
Arrays.asList("0","2","0","44","5","1881"),
38+
Arrays.asList("020","4","4","5","1","8","8","1"),
39+
Arrays.asList("020","44","5","1","8","8","1"),
40+
Arrays.asList("020","4","4","5","1","88","1"),
41+
Arrays.asList("020","4","4","5","1881"),
42+
Arrays.asList("020","44","5","1","88","1"),
43+
Arrays.asList("020","44","5","1881")
44+
);
45+
input = "0204451881";
46+
47+
test(expected, input);
48+
}
49+
50+
private void test(List<List<String>> expected, String input) {
51+
expected.forEach(Collections::sort);
52+
List<List<String>> result = GeneratePalindromicDecompositions.palindromicPartitioning(input);
53+
assertEquals(expected.size(), result.size());
54+
result.forEach( i -> {
55+
Collections.sort(i);
56+
assertTrue(expected.remove(i));
57+
});
58+
}
59+
60+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class GeneratePermutationsTest {
9+
10+
private List<List<Integer>> expected;
11+
private List<Integer> A;
12+
13+
@Test
14+
public void permutations1() throws Exception {
15+
expected = Arrays.asList(
16+
Arrays.asList(2)
17+
);
18+
A = Arrays.asList(2);
19+
20+
test(expected, A);
21+
}
22+
23+
@Test
24+
public void permutations2() throws Exception {
25+
expected = Arrays.asList(
26+
Arrays.asList(2,3),
27+
Arrays.asList(3,2)
28+
);
29+
A = Arrays.asList(2,3);
30+
31+
test(expected, A);
32+
}
33+
34+
@Test
35+
public void permutations3() throws Exception {
36+
expected = Arrays.asList(
37+
Arrays.asList(2,3,5),
38+
Arrays.asList(2,5,3),
39+
40+
Arrays.asList(3,2,5),
41+
Arrays.asList(3,5,2),
42+
43+
Arrays.asList(5,2,3),
44+
Arrays.asList(5,3,2)
45+
);
46+
A = Arrays.asList(2,3,5);
47+
48+
test(expected, A);
49+
}
50+
51+
@Test
52+
public void permutations4() throws Exception {
53+
expected = Arrays.asList(
54+
Arrays.asList(2,3,5,7),
55+
Arrays.asList(2,3,7,5),
56+
Arrays.asList(2,5,3,7),
57+
Arrays.asList(2,5,7,3),
58+
Arrays.asList(2,7,3,5),
59+
Arrays.asList(2,7,5,3),
60+
61+
Arrays.asList(3,2,5,7),
62+
Arrays.asList(3,2,7,5),
63+
Arrays.asList(3,5,2,7),
64+
Arrays.asList(3,5,7,2),
65+
Arrays.asList(3,7,2,5),
66+
Arrays.asList(3,7,5,2),
67+
68+
Arrays.asList(5,2,3,7),
69+
Arrays.asList(5,2,7,3),
70+
Arrays.asList(5,3,2,7),
71+
Arrays.asList(5,3,7,2),
72+
Arrays.asList(5,7,2,5),
73+
Arrays.asList(5,7,5,2),
74+
75+
Arrays.asList(7,2,5,3),
76+
Arrays.asList(7,2,3,5),
77+
Arrays.asList(7,3,2,7),
78+
Arrays.asList(7,3,7,2),
79+
Arrays.asList(7,5,3,2),
80+
Arrays.asList(7,5,2,3)
81+
);
82+
A = Arrays.asList(2,3,5,7);
83+
84+
test(expected, A);
85+
}
86+
87+
private void test(List<List<Integer>> expected, List<Integer> A) {
88+
List<List<Integer>> result = GeneratePermutations.permutations(A);
89+
assertEquals(expected.size(), result.size());
90+
expected.forEach(i -> {
91+
assertTrue(result.remove(i));
92+
});
93+
}
94+
95+
}

0 commit comments

Comments
 (0)