Skip to content

Commit 49a1e7d

Browse files
committed
chapter 11 done
1 parent d2d39e5 commit 49a1e7d

14 files changed

+352
-8
lines changed

datastructures/src/main/java/Star.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ public class Star implements Comparable<Star> {
22

33
private double x, y, z;
44

5+
public Star(double x, double y, double z) {
6+
this.x = x;
7+
this.y = y;
8+
this.z = z;
9+
}
10+
511
public double distance() {
612
return Math.sqrt(x * x + y * y + z * z);
713
}
@@ -10,4 +16,13 @@ public double distance() {
1016
public int compareTo(Star rhs) {
1117
return Double.compare(this.distance(), rhs.distance());
1218
}
19+
20+
@Override
21+
public String toString() {
22+
return "Star{" +
23+
"x=" + x +
24+
", y=" + y +
25+
", z=" + z +
26+
'}';
27+
}
1328
}

heaps/pom.xml

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

2227

heaps/src/main/java/ComputeKClosest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class ComputeKClosest {
1111
are closest to Earth?
1212
*/
1313

14+
private static final Star EARTH_COORDINATES = new Star(0,0,0);
15+
1416
public static List<Star> getKClosest(int k, Iterator<Star> stars) {
1517

1618
return Collections.emptyList();

heaps/src/main/java/ComputeKLargest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ComputeKLargest {
1111
in the max-heap. You cannot modify the heap.
1212
*/
1313

14-
public static List<Integer> kLargestInBinaryHeap(List<Integer> list, int k) {
14+
public static List<Integer> kLargestInBinaryHeap(final List<Integer> list, int k) {
1515

1616
return Collections.emptyList();
1717
}

heaps/src/main/java/ComputeMedianOnline.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import java.util.Collections;
12
import java.util.Iterator;
3+
import java.util.List;
24

35
public class ComputeMedianOnline {
46

@@ -9,7 +11,8 @@ public class ComputeMedianOnline {
911
running median of a sequence.
1012
*/
1113

12-
public static void onlineMedian(Iterator<Integer> sequence) {
14+
public static List<Double> onlineMedian(Iterator<Integer> sequence) {
1315

16+
return Collections.emptyList();
1417
}
1518
}

heaps/src/main/java/MergeSortedFiles.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ public class MergeSortedFiles {
88
99
Write a program that takes as input a set of sorted sequences
1010
and computes the union of these sequences as a sorted sequence.
11-
For example, if the input is [3,5,7],[0,6], and [0,6,28],
12-
the the output is [0,0,3,5,6,6,7,28].
1311
*/
1412

1513
public static List<Integer> mergeSorted(List<List<Integer>> sortedArrays) {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
import java.util.Arrays;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class ComputeKClosestTest {
11+
12+
private List<Star> expected;
13+
private int k;
14+
private Iterator<Star> stars;
15+
16+
@Test
17+
public void getKClosest1() throws Exception {
18+
expected = Arrays.asList(
19+
new Star(0,0,1),
20+
new Star(0,0,2)
21+
);
22+
k = 1;
23+
stars = Arrays.asList(
24+
new Star(0,0,1)
25+
).iterator();
26+
27+
test(expected, k, stars);
28+
}
29+
30+
@Test
31+
public void getKClosest2() throws Exception {
32+
expected = Arrays.asList(
33+
new Star(0,0,1),
34+
new Star(0,0,4),
35+
new Star(0,0,2),
36+
new Star(0,0,5),
37+
new Star(0,0,3)
38+
);
39+
k = 3;
40+
stars = Arrays.asList(
41+
new Star(0,0,1),
42+
new Star(0,0,2),
43+
new Star(0,0,3)
44+
).iterator();
45+
46+
test(expected, k, stars);
47+
}
48+
49+
@Test
50+
public void getKClosest3() throws Exception {
51+
expected = Arrays.asList(
52+
new Star(0,0,75),
53+
new Star(0,0,23),
54+
new Star(0,0,131),
55+
new Star(0,0,99),
56+
new Star(0,0,67),
57+
new Star(0,0,70),
58+
new Star(0,0,99),
59+
new Star(0,0,3),
60+
new Star(0,0,23),
61+
new Star(0,0,100),
62+
new Star(0,0,32),
63+
new Star(0,0,43)
64+
);
65+
k = 5;
66+
stars = Arrays.asList(
67+
new Star(0,0,3),
68+
new Star(0,0,23),
69+
new Star(0,0,32),
70+
new Star(0,0,43),
71+
new Star(0,0,70)
72+
).iterator();
73+
74+
test(expected, k, stars);
75+
}
76+
77+
private void test(List<Star> expected, int k, Iterator<Star> stars) {
78+
List<Star> result = ComputeKClosest.getKClosest(k, stars);
79+
try {
80+
for (Star star : expected) {
81+
if (!result.contains(star))
82+
throw new AssertionError();
83+
}
84+
} catch (AssertionError e) {
85+
StringBuilder errorMessage = new StringBuilder();
86+
errorMessage.append("\nExpected: "+expected.toString());
87+
errorMessage.append("\nActual: "+result.toString()+"\n");
88+
fail(errorMessage.toString());
89+
}
90+
}
91+
92+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 ComputeKLargestTest {
9+
10+
private List<Integer> expected;
11+
private List<Integer> list;
12+
private int k;
13+
14+
@Test
15+
public void kLargestInBinaryHeap1() throws Exception {
16+
expected = Arrays.asList(7,8,9);
17+
list = Arrays.asList(8,3,4,2,6,5,7,9,1);
18+
k = 3;
19+
20+
test(expected,list,k);
21+
}
22+
23+
@Test
24+
public void kLargestInBinaryHeap2() throws Exception {
25+
expected = Arrays.asList(5,6,7,8,9);
26+
list = Arrays.asList(8,3,4,2,6,5,7,9,1);
27+
k = 5;
28+
29+
test(expected,list,k);
30+
}
31+
32+
private void test(List<Integer> expected, List<Integer> list, int k) {
33+
List<Integer> result = ComputeKLargest.kLargestInBinaryHeap(list, k);
34+
try {
35+
for (Integer i : expected) {
36+
if (!result.contains(i))
37+
throw new AssertionError();
38+
}
39+
} catch (AssertionError e) {
40+
StringBuilder errorMessage = new StringBuilder();
41+
errorMessage.append("\nExpected: "+expected.toString());
42+
errorMessage.append("\nActual: "+result.toString()+"\n");
43+
fail(errorMessage.toString());
44+
}
45+
}
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import org.junit.Test;
2+
3+
import java.util.Arrays;
4+
import java.util.Iterator;
5+
import java.util.List;
6+
7+
import static org.junit.Assert.*;
8+
9+
public class ComputeMedianOnlineTest {
10+
11+
private List<Double> expected;
12+
private Iterator<Integer> input;
13+
14+
@Test
15+
public void onlineMedian1() throws Exception {
16+
expected = Arrays.asList(1.,.5,1.,2.,2.,1.5,1.);
17+
input = Arrays.asList(1,0,3,5,2,0,1).iterator();
18+
19+
test(expected, input);
20+
}
21+
22+
private void test(List<Double> expected, Iterator<Integer> input) {
23+
assertEquals(expected, ComputeMedianOnline.onlineMedian(input));
24+
}
25+
26+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.stream.IntStream;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class HeapStackTest {
11+
12+
private int length;
13+
14+
@Test
15+
public void heapStack1(){
16+
length = 10;
17+
18+
test(length);
19+
}
20+
21+
@Test
22+
public void heapStack2(){
23+
length = 100;
24+
25+
test(length);
26+
}
27+
28+
private void test(int length) {
29+
List<Integer> values = StreamUtil.sequence(length);
30+
HeapStack stack = createStack(values);
31+
StreamUtil.revRange(0, length)
32+
.forEach(n -> {
33+
assertEquals((Integer)n,stack.pop());
34+
});
35+
}
36+
37+
private HeapStack createStack(List<Integer> values) {
38+
final HeapStack stack = new HeapStack();
39+
IntStream.range(0, values.size())
40+
.forEach(i ->
41+
{
42+
stack.push(values.get(i));
43+
});
44+
return stack;
45+
}
46+
47+
}

0 commit comments

Comments
 (0)