From 0863f2493ce5e2c7d90f7396ff48cb66a6a9ac13 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Wed, 20 Sep 2017 10:01:10 -0400 Subject: [PATCH 01/41] Update .travis.yml Add open jdk 6 & 7 --- .travis.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 58364da4..337f7d7e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,13 +9,15 @@ before_script: language: java jdk: - - oraclejdk8 +# - oraclejdk9 +# - oraclejdk8 # - oraclejdk7 # - oraclejdk6 -# - openjdk8 +# - openjdk9 + - openjdk8 - openjdk7 -# - openjdk6 + - openjdk6 env: - TEST_SUITE=run_tests From b6464f5def13cca8ea3a86f277fb00e06a1a59d0 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Wed, 20 Sep 2017 10:07:15 -0400 Subject: [PATCH 02/41] Update .travis.yml --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 337f7d7e..59d39462 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,11 @@ before_script: - echo $JAVA_OPTS - echo $ANT_OPTS +addons: + apt: + packages: + - openjdk-6-jdk + language: java jdk: From 75661cb0fddf827d3e454139abf07a6051ea6caf Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Wed, 20 Sep 2017 10:19:40 -0400 Subject: [PATCH 03/41] Code cleanup in Interval sum and rooted tree --- .../data_structures/IntervalSumArray.java | 33 ++++++------ .../data_structures/RootedTree.java | 50 +++++++++++-------- .../data_structures/SuffixArray.java | 1 + 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java b/src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java index 4f567ef5..cbbd65e0 100644 --- a/src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java +++ b/src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java @@ -1,15 +1,19 @@ package com.jwetherell.algorithms.data_structures; +import java.util.List; +import java.util.ArrayList; /** * Implementation of array holding integer values which allows to count sum of elements in given * interval in O(log n) complexity. - * + *
* @author Szymon Stankiewicz + * @author Justin Wetherell */ public class IntervalSumArray { - private List.ArrayList values = new List.ArrayList<>(); - private List.ArrayList prefSums = new List.ArrayList<>(); + + private List values = new ArrayList(); + private List prefSums = new ArrayList(); /** * Creates empty IntervalSumArray @@ -27,8 +31,7 @@ public IntervalSumArray() { * @param size size of IntervalSumArray */ public IntervalSumArray(int size) { - this(); - for(int i = 0; i values) { - this(); - for(Integer v: values) + for (Integer v: values) add(v); } private static int greatestPowerOfTwoDividing(int x) { - return x&(-x); + return x & (-x); } private static int successor(int x) { @@ -75,7 +77,7 @@ public int size() { */ public void add(int val) { values.add(val); - for(int i = 1; i= size()) throw new IndexOutOfBoundsException(); + if (index < 0 || index >= size()) + throw new IndexOutOfBoundsException(); index++; int diff = val - values.get(index); values.set(index, val); - while(index <= size()) { + while (index <= size()) { int oldPrefSum = prefSums.get(index); prefSums.set(index, oldPrefSum + diff); index = successor(index); @@ -121,10 +124,11 @@ public int get(int index) { * @return sum of values in interval */ public int sum(int end) { - if(end < 0 || end >= size()) throw new IndexOutOfBoundsException(); + if (end < 0 || end >= size()) + throw new IndexOutOfBoundsException(); end++; int s = 0; - while(end > 0) { + while (end > 0) { s += prefSums.get(end); end = predecessor(end); } @@ -152,7 +156,8 @@ public int sum() { * @return sum of values in interval */ public int sum(int start, int end) { - if(start > end) throw new IllegalArgumentException("Start must be less then end"); + if (start > end) + throw new IllegalArgumentException("Start must be less then end"); int startPrefSum = start == 0 ? 0 : sum(start-1); int endPrefSum = sum(end); return endPrefSum - startPrefSum; diff --git a/src/com/jwetherell/algorithms/data_structures/RootedTree.java b/src/com/jwetherell/algorithms/data_structures/RootedTree.java index 76ec95d2..3008d14e 100644 --- a/src/com/jwetherell/algorithms/data_structures/RootedTree.java +++ b/src/com/jwetherell/algorithms/data_structures/RootedTree.java @@ -1,24 +1,32 @@ package com.jwetherell.algorithms.data_structures; +import java.util.List; import java.util.ArrayList; /** * Structure for storing rooted tree which allows to find lowest common ancestor. - * + *

* @param type of value stored in nodes. + *
+ * @author Szymon Stankiewicz + * @author Justin Wetherell */ public class RootedTree { + + private final List> ancestors = new ArrayList>(); + private final List> children = new ArrayList>(); + private T value = null; private int depth = 0; - private final ArrayList> ancestors = new ArrayList<>(); - private final ArrayList> children = new ArrayList<>(); /** * Exception which can be thrown by lowestCommonAncestor function if two * nodes are in different trees. * */ - public static class NodesNotInSameTreeException extends Exception {} + public static class NodesNotInSameTreeException extends Exception { + private static final long serialVersionUID = -5366787886097250564L; + } /** * Finds lower common ancestor of two nodes. @@ -31,9 +39,11 @@ public static class NodesNotInSameTreeException extends Exception {} * @throws NodesNotInSameTreeException if nodes don't have common root */ public static RootedTree lowestCommonAncestor(RootedTree node1, RootedTree node2) throws NodesNotInSameTreeException { - if(node1 == node2) return node1; - else if(node1.depth < node2.depth) return lowestCommonAncestor(node2, node1); - else if(node1.depth > node2.depth) { + if (node1 == node2) + return node1; + else if (node1.depth < node2.depth) + return lowestCommonAncestor(node2, node1); + else if (node1.depth > node2.depth) { int diff = node1.depth - node2.depth; int jump = 0; while(diff > 0) { @@ -43,12 +53,12 @@ else if(node1.depth > node2.depth) { diff /= 2; } return lowestCommonAncestor(node1, node2); - } - else { + } else { try { int step = 0; - while(1<<(step+1) <= node1.depth) step++; - while(step >= 0) { + while (1<<(step+1) <= node1.depth) + step++; + while (step >= 0) { if(step < node1.ancestors.size() && node1.ancestors.get(step) != node2.ancestors.get(step)) { node1 = node1.ancestors.get(step); node2 = node2.ancestors.get(step); @@ -61,16 +71,13 @@ else if(node1.depth > node2.depth) { } } - } /** * Creates tree with root only. * */ - public RootedTree() { - - } + public RootedTree() { } /** * Cretes tree with root (storing value) only. @@ -109,7 +116,7 @@ public RootedTree setValue(T value) { * @return added child */ public RootedTree addChild() { - return new RootedTree<>(this); + return new RootedTree(this); } /** @@ -140,15 +147,15 @@ public T getValue() { * @return subtree with given value in the root */ public RootedTree find(T value) { - if(this.value == null) { - if(value == null) + if (this.value == null) { + if (value == null) return this; } - else if(this.value.equals(value)) + else if (this.value.equals(value)) return this; - for(RootedTree child: children) { + for (RootedTree child: children) { RootedTree res = child.find(value); - if(res != null) + if (res != null) return res; } return null; @@ -163,5 +170,4 @@ else if(this.value.equals(value)) public boolean contains(T value) { return find(value) != null; } - } diff --git a/src/com/jwetherell/algorithms/data_structures/SuffixArray.java b/src/com/jwetherell/algorithms/data_structures/SuffixArray.java index 2d01ed9f..640754f0 100644 --- a/src/com/jwetherell/algorithms/data_structures/SuffixArray.java +++ b/src/com/jwetherell/algorithms/data_structures/SuffixArray.java @@ -165,6 +165,7 @@ private String buildStringWithEndChar(CharSequence sequence) { } private class KMRsWithIndex{ + Integer beginKMR; Integer endKMR; Integer index; From 98c93943ef57fb24b3fd820503ddc6b535318bad Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Wed, 20 Sep 2017 10:19:59 -0400 Subject: [PATCH 04/41] Code cleanup in Interval sum and rooted tree --- .../data_structures/IntervalSumArrayTest.java | 38 +++++++++---------- .../data_structures/RootedTreeTest.java | 21 +++++----- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java b/test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java index 0af23a19..1a5de6f8 100644 --- a/test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java +++ b/test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java @@ -1,44 +1,45 @@ package com.jwetherell.algorithms.data_structures; -import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; import java.util.Random; -import static org.junit.Assert.*; +import org.junit.Test; public class IntervalSumArrayTest { @Test public void properSumAllElementsTest() { - IntervalSumArray sub = new IntervalSumArray(); - for(int i = 0; i<=100; i++) + final IntervalSumArray sub = new IntervalSumArray(); + for (int i = 0; i<=100; i++) sub.add(i); - for(int i = 0; i<=100; i++) + for (int i = 0; i<=100; i++) assertEquals(i*(i+1)/2, sub.sum(i)); assertEquals(100*101/2, sub.sum()); } @Test public void randomGeneratedTest() { - Random generator = new Random(42); - List list = new ArrayList<>(); - for(int i = 0; i<=100; i++) + final Random generator = new Random(42); + final List list = new ArrayList(); + for (int i = 0; i<=100; i++) list.add(i); - IntervalSumArray sum = new IntervalSumArray(list); - for(int i = 0; i<1000000; i++) { - int pos = generator.nextInt(100); - int val = generator.nextInt(2000000) - 1000000; + final IntervalSumArray sum = new IntervalSumArray(list); + for (int i = 0; i<1000000; i++) { + final int pos = generator.nextInt(100); + final int val = generator.nextInt(2000000) - 1000000; sum.set(pos, val); list.set(pos, val); assertEquals(val, sum.get(pos)); } int s = 0; - List prefSum = new ArrayList<>(); + final List prefSum = new ArrayList(); prefSum.add(s); - for(Integer val: list) { + for (Integer val: list) { s += val; prefSum.add(s); } @@ -52,7 +53,7 @@ public void randomGeneratedTest() { @Test public void setIndexOutOfRangeTest() { - IntervalSumArray sum = new IntervalSumArray(100); + final IntervalSumArray sum = new IntervalSumArray(100); boolean thrown = false; try { sum.set(101, 10); @@ -64,7 +65,7 @@ public void setIndexOutOfRangeTest() { @Test public void sumIndexOutOfRangeTest() { - IntervalSumArray sum = new IntervalSumArray(100); + final IntervalSumArray sum = new IntervalSumArray(100); boolean thrown = false; try { sum.sum(101); @@ -76,7 +77,7 @@ public void sumIndexOutOfRangeTest() { @Test public void endBeforeStartTest() { - IntervalSumArray sum = new IntervalSumArray(100); + final IntervalSumArray sum = new IntervalSumArray(100); boolean thrown = false; try { sum.sum(101, 100); @@ -85,5 +86,4 @@ public void endBeforeStartTest() { } assertTrue(thrown); } - -} \ No newline at end of file +} diff --git a/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java b/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java index bfed080e..cd210dc7 100644 --- a/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java +++ b/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java @@ -8,22 +8,21 @@ public class RootedTreeTest { @Test public void largeTreeTest() throws RootedTree.NodesNotInSameTreeException { - RootedTree root = new RootedTree<>(); - RootedTree left = root.addChild(); - RootedTree middle = root.addChild(); - RootedTree right = root.addChild(); + + final RootedTree root = new RootedTree(); + final RootedTree left = root.addChild(); + final RootedTree middle = root.addChild(); + final RootedTree right = root.addChild(); //long path RootedTree v = left; - for(int i = 0; i<1000; i++) + for (int i = 0; i<1000; i++) v = v.addChild(); RootedTree leftRight = left.addChild(); - assertEquals(RootedTree.lowestCommonAncestor(v, leftRight), left); - for(int i = 0; i<2000; i++) { + for (int i = 0; i<2000; i++) { leftRight = leftRight.addChild(); - assertEquals(RootedTree.lowestCommonAncestor(v, leftRight), left); } @@ -31,7 +30,7 @@ public void largeTreeTest() throws RootedTree.NodesNotInSameTreeException { assertEquals(RootedTree.lowestCommonAncestor(root, right), root); assertEquals(RootedTree.lowestCommonAncestor(root, root), root); - RootedTree root2 = new RootedTree<>(); + final RootedTree root2 = new RootedTree(); boolean thrownException = false; try { RootedTree.lowestCommonAncestor(v, root2); @@ -40,11 +39,11 @@ public void largeTreeTest() throws RootedTree.NodesNotInSameTreeException { } assertTrue(thrownException); - RootedTree deepChild = v.addChild(101); + final RootedTree deepChild = v.addChild(101); assertEquals(deepChild, root.find(101)); assertTrue(root.contains(101)); assertNull(root.find(102)); assertFalse(root.contains(102)); } -} \ No newline at end of file +} From c8680af4ccb2fe322ebbbe20983c41d56806fd58 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Wed, 20 Sep 2017 10:25:52 -0400 Subject: [PATCH 05/41] Update .travis.yml --- .travis.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 59d39462..bf141759 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,12 +14,12 @@ addons: language: java jdk: -# - oraclejdk9 -# - oraclejdk8 -# - oraclejdk7 -# - oraclejdk6 + - oraclejdk9 + - oraclejdk8 + - oraclejdk7 + - oraclejdk6 -# - openjdk9 + - openjdk9 - openjdk8 - openjdk7 - openjdk6 From 39d4f2d41db044f9037703c5b9ec34f97dc54598 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Wed, 20 Sep 2017 10:29:53 -0400 Subject: [PATCH 06/41] Update .travis.yml --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index bf141759..e2324a51 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,10 +16,10 @@ language: java jdk: - oraclejdk9 - oraclejdk8 - - oraclejdk7 - - oraclejdk6 +# - oraclejdk7 +# - oraclejdk6 - - openjdk9 +# - openjdk9 - openjdk8 - openjdk7 - openjdk6 From c00a62e1742e726017da617e577f824f5f261c9a Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Wed, 20 Sep 2017 10:55:54 -0400 Subject: [PATCH 07/41] Code cleanup in Lowest Common Ancestor --- .../data_structures/LowestCommonAncestor.java | 175 ++++++++++++++++++ .../data_structures/RootedTree.java | 173 ----------------- .../data_structures/RootedTreeTest.java | 49 ----- .../{ => test}/IntervalSumArrayTest.java | 8 +- .../test/LowestCommonAncestorTest.java | 56 ++++++ 5 files changed, 236 insertions(+), 225 deletions(-) create mode 100644 src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java delete mode 100644 src/com/jwetherell/algorithms/data_structures/RootedTree.java delete mode 100644 test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java rename test/com/jwetherell/algorithms/data_structures/{ => test}/IntervalSumArrayTest.java (92%) create mode 100644 test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java diff --git a/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java b/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java new file mode 100644 index 00000000..f6b4ef66 --- /dev/null +++ b/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java @@ -0,0 +1,175 @@ +package com.jwetherell.algorithms.data_structures; + +import java.util.ArrayList; +import java.util.List; + +/** + * Structure for storing rooted tree which allows to find lowest common ancestor. + *

+ * @param type of value stored in nodes. + *
+ * @author Szymon Stankiewicz + * @author Justin Wetherell + */ +public class LowestCommonAncestor { + + /** + * Exception which can be thrown by lowestCommonAncestor function if two + * nodes are in different trees. + * + */ + public static class NodesNotInSameTreeException extends Exception { + private static final long serialVersionUID = -5366787886097250564L; + } + + /** + * Finds lower common ancestor of two nodes. + * + * Complexity O(log n) where n is the height of the tree. + * + * @param node1 first node + * @param node2 second node + * @return lower common ancestor + * @throws NodesNotInSameTreeException if nodes don't have common root + */ + public static RootedTree lowestCommonAncestor(RootedTree node1, RootedTree node2) throws NodesNotInSameTreeException { + if (node1 == node2) + return node1; + else if (node1.depth < node2.depth) + return lowestCommonAncestor(node2, node1); + else if (node1.depth > node2.depth) { + int diff = node1.depth - node2.depth; + int jump = 0; + while (diff > 0) { + if (diff % 2 == 1) + node1 = node1.ancestors.get(jump); + jump++; + diff /= 2; + } + return lowestCommonAncestor(node1, node2); + } else { + try { + int step = 0; + while (1<<(step+1) <= node1.depth) + step++; + while (step >= 0) { + if(step < node1.ancestors.size() && node1.ancestors.get(step) != node2.ancestors.get(step)) { + node1 = node1.ancestors.get(step); + node2 = node2.ancestors.get(step); + } + step--; + } + return node1.ancestors.get(0); + } catch (Exception e) { + throw new NodesNotInSameTreeException(); + } + + } + } + + public static final class RootedTree { + + private final List> ancestors = new ArrayList>(); + private final List> children = new ArrayList>(); + + private T value = null; + private int depth = 0; + + /** + * Creates tree with root only. + * + */ + public RootedTree() { } + + /** + * Creates tree with root (storing value) only. + * + * @param value value to be stored in root + */ + public RootedTree(T value) { + this.value = value; + } + + private RootedTree(RootedTree parent) { + parent.children.add(this); + this.ancestors.add(parent); + this.depth = parent.depth + 1; + int dist = 0; + while (true) { + try { + this.ancestors.add(this.ancestors.get(dist).ancestors.get(dist)); + dist++; + } catch (Exception e){ + break; + } + } + } + + public RootedTree setValue(T value) { + this.value = value; + return this; + } + + /** + * Creates new child for this node and returns it. + * + * Complexity O(log depth) + * + * @return added child + */ + public RootedTree addChild() { + return new RootedTree(this); + } + + /** + * Creates new child (storing value) for this node and returns it. + * + * Complexity O(log depth) + * + * @param value value to be stored in new child + * @return added child + */ + public RootedTree addChild(T value) { + return addChild().setValue(value); + } + + /** + * Returns value stored in node. + * + * @return node's value. + */ + public T getValue() { + return value; + } + + /** + * Finds subtree with given value in the root. + * + * @param value value to be find + * @return subtree with given value in the root + */ + public RootedTree find(T value) { + if (this.value == null) { + if (value == null) + return this; + } else if (this.value.equals(value)) + return this; + for (RootedTree child: children) { + final RootedTree res = child.find(value); + if (res != null) + return res; + } + return null; + } + + /** + * Returns true if tree contains a node with given value + * + * @param value to be checked + * @return true if tree contains node with given value, false otherwise + */ + public boolean contains(T value) { + return find(value) != null; + } + } +} diff --git a/src/com/jwetherell/algorithms/data_structures/RootedTree.java b/src/com/jwetherell/algorithms/data_structures/RootedTree.java deleted file mode 100644 index 3008d14e..00000000 --- a/src/com/jwetherell/algorithms/data_structures/RootedTree.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.jwetherell.algorithms.data_structures; - -import java.util.List; -import java.util.ArrayList; - -/** - * Structure for storing rooted tree which allows to find lowest common ancestor. - *

- * @param type of value stored in nodes. - *
- * @author Szymon Stankiewicz - * @author Justin Wetherell - */ -public class RootedTree { - - private final List> ancestors = new ArrayList>(); - private final List> children = new ArrayList>(); - - private T value = null; - private int depth = 0; - - /** - * Exception which can be thrown by lowestCommonAncestor function if two - * nodes are in different trees. - * - */ - public static class NodesNotInSameTreeException extends Exception { - private static final long serialVersionUID = -5366787886097250564L; - } - - /** - * Finds lower common ancestor of two nodes. - * - * Complexity O(log n) where n is the height of the tree. - * - * @param node1 first node - * @param node2 second node - * @return lower common ancestor - * @throws NodesNotInSameTreeException if nodes don't have common root - */ - public static RootedTree lowestCommonAncestor(RootedTree node1, RootedTree node2) throws NodesNotInSameTreeException { - if (node1 == node2) - return node1; - else if (node1.depth < node2.depth) - return lowestCommonAncestor(node2, node1); - else if (node1.depth > node2.depth) { - int diff = node1.depth - node2.depth; - int jump = 0; - while(diff > 0) { - if(diff % 2 == 1) - node1 = node1.ancestors.get(jump); - jump++; - diff /= 2; - } - return lowestCommonAncestor(node1, node2); - } else { - try { - int step = 0; - while (1<<(step+1) <= node1.depth) - step++; - while (step >= 0) { - if(step < node1.ancestors.size() && node1.ancestors.get(step) != node2.ancestors.get(step)) { - node1 = node1.ancestors.get(step); - node2 = node2.ancestors.get(step); - } - step--; - } - return node1.ancestors.get(0); - } catch (Exception e) { - throw new NodesNotInSameTreeException(); - } - - } - } - - /** - * Creates tree with root only. - * - */ - public RootedTree() { } - - /** - * Cretes tree with root (storing value) only. - * - * @param value value to be stored in root - */ - public RootedTree(T value) { - this.value = value; - } - - private RootedTree(RootedTree parent) { - parent.children.add(this); - this.ancestors.add(parent); - this.depth = parent.depth + 1; - int dist = 0; - while(true) { - try { - this.ancestors.add(this.ancestors.get(dist).ancestors.get(dist)); - dist++; - } catch (Exception e){ - break; - } - } - } - - public RootedTree setValue(T value) { - this.value = value; - return this; - } - - /** - * Creates new child for this node and returns it. - * - * Complexity O(log depth) - * - * @return added child - */ - public RootedTree addChild() { - return new RootedTree(this); - } - - /** - * Creates new child (storing value) for this node and returns it. - * - * Complexity O(log depth) - * - * @param value value to be stored in new child - * @return added child - */ - public RootedTree addChild(T value) { - return addChild().setValue(value); - } - - /** - * Returns value stored in node. - * - * @return node's value. - */ - public T getValue() { - return value; - } - - /** - * Finds subtree with given value in the root. - * - * @param value value to be find - * @return subtree with given value in the root - */ - public RootedTree find(T value) { - if (this.value == null) { - if (value == null) - return this; - } - else if (this.value.equals(value)) - return this; - for (RootedTree child: children) { - RootedTree res = child.find(value); - if (res != null) - return res; - } - return null; - } - - /** - * Returns true if tree contains a node with given value - * - * @param value to be checked - * @return true if tree contains node with given value, false otherwise - */ - public boolean contains(T value) { - return find(value) != null; - } -} diff --git a/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java b/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java deleted file mode 100644 index cd210dc7..00000000 --- a/test/com/jwetherell/algorithms/data_structures/RootedTreeTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.jwetherell.algorithms.data_structures; - -import org.junit.Test; - -import static org.junit.Assert.*; - -public class RootedTreeTest { - - @Test - public void largeTreeTest() throws RootedTree.NodesNotInSameTreeException { - - final RootedTree root = new RootedTree(); - final RootedTree left = root.addChild(); - final RootedTree middle = root.addChild(); - final RootedTree right = root.addChild(); - - //long path - RootedTree v = left; - for (int i = 0; i<1000; i++) - v = v.addChild(); - RootedTree leftRight = left.addChild(); - assertEquals(RootedTree.lowestCommonAncestor(v, leftRight), left); - - for (int i = 0; i<2000; i++) { - leftRight = leftRight.addChild(); - assertEquals(RootedTree.lowestCommonAncestor(v, leftRight), left); - } - - assertEquals(RootedTree.lowestCommonAncestor(middle, right), root); - assertEquals(RootedTree.lowestCommonAncestor(root, right), root); - assertEquals(RootedTree.lowestCommonAncestor(root, root), root); - - final RootedTree root2 = new RootedTree(); - boolean thrownException = false; - try { - RootedTree.lowestCommonAncestor(v, root2); - } catch (RootedTree.NodesNotInSameTreeException e) { - thrownException = true; - } - assertTrue(thrownException); - - final RootedTree deepChild = v.addChild(101); - assertEquals(deepChild, root.find(101)); - assertTrue(root.contains(101)); - - assertNull(root.find(102)); - assertFalse(root.contains(102)); - } -} diff --git a/test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java b/test/com/jwetherell/algorithms/data_structures/test/IntervalSumArrayTest.java similarity index 92% rename from test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java rename to test/com/jwetherell/algorithms/data_structures/test/IntervalSumArrayTest.java index 1a5de6f8..35770271 100644 --- a/test/com/jwetherell/algorithms/data_structures/IntervalSumArrayTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/IntervalSumArrayTest.java @@ -1,4 +1,4 @@ -package com.jwetherell.algorithms.data_structures; +package com.jwetherell.algorithms.data_structures.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -9,6 +9,8 @@ import org.junit.Test; +import com.jwetherell.algorithms.data_structures.IntervalSumArray; + public class IntervalSumArrayTest { @Test @@ -44,8 +46,8 @@ public void randomGeneratedTest() { prefSum.add(s); } - for(int i = 0; i<=100; i++) { - for(int j = i; j<=100; j++) { + for (int i = 0; i<100; i++) { + for (int j = i; j<100; j++) { assertEquals(prefSum.get(j+1) - prefSum.get(i), sum.sum(i, j)); } } diff --git a/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java b/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java new file mode 100644 index 00000000..b8e43f5f --- /dev/null +++ b/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java @@ -0,0 +1,56 @@ +package com.jwetherell.algorithms.data_structures.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +import com.jwetherell.algorithms.data_structures.LowestCommonAncestor; +import com.jwetherell.algorithms.data_structures.LowestCommonAncestor.NodesNotInSameTreeException; +import com.jwetherell.algorithms.data_structures.LowestCommonAncestor.RootedTree; + +public class LowestCommonAncestorTest { + + @Test + public void largeTreeTest() throws NodesNotInSameTreeException { + + final RootedTree root = new RootedTree(); + final RootedTree left = root.addChild(); + final RootedTree middle = root.addChild(); + final RootedTree right = root.addChild(); + + //long path + RootedTree v = left; + for (int i = 0; i<1000; i++) + v = v.addChild(); + RootedTree leftRight = left.addChild(); + assertEquals(LowestCommonAncestor.lowestCommonAncestor(v, leftRight), left); + + for (int i = 0; i<2000; i++) { + leftRight = leftRight.addChild(); + assertEquals(LowestCommonAncestor.lowestCommonAncestor(v, leftRight), left); + } + + assertEquals(LowestCommonAncestor.lowestCommonAncestor(middle, right), root); + assertEquals(LowestCommonAncestor.lowestCommonAncestor(root, right), root); + assertEquals(LowestCommonAncestor.lowestCommonAncestor(root, root), root); + + final RootedTree root2 = new RootedTree(); + boolean thrownException = false; + try { + LowestCommonAncestor.lowestCommonAncestor(v, root2); + } catch (NodesNotInSameTreeException e) { + thrownException = true; + } + assertTrue(thrownException); + + final RootedTree deepChild = v.addChild(101); + assertEquals(deepChild, root.find(101)); + assertTrue(root.contains(101)); + + assertNull(root.find(102)); + assertFalse(root.contains(102)); + } +} From b23f385b6531e0047dc4e91573939a6a799b439d Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Wed, 20 Sep 2017 14:10:49 -0400 Subject: [PATCH 08/41] Updated IntervalSum and LowestCommonAncestor --- ...IntervalSumArray.java => IntervalSum.java} | 8 ++--- .../data_structures/LowestCommonAncestor.java | 32 +++++++++---------- ...SumArrayTest.java => IntervalSumTest.java} | 14 ++++---- .../test/LowestCommonAncestorTest.java | 18 +++++------ 4 files changed, 36 insertions(+), 36 deletions(-) rename src/com/jwetherell/algorithms/data_structures/{IntervalSumArray.java => IntervalSum.java} (96%) rename test/com/jwetherell/algorithms/data_structures/test/{IntervalSumArrayTest.java => IntervalSumTest.java} (83%) diff --git a/src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java b/src/com/jwetherell/algorithms/data_structures/IntervalSum.java similarity index 96% rename from src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java rename to src/com/jwetherell/algorithms/data_structures/IntervalSum.java index cbbd65e0..b4c62f57 100644 --- a/src/com/jwetherell/algorithms/data_structures/IntervalSumArray.java +++ b/src/com/jwetherell/algorithms/data_structures/IntervalSum.java @@ -10,7 +10,7 @@ * @author Szymon Stankiewicz * @author Justin Wetherell */ -public class IntervalSumArray { +public class IntervalSum { private List values = new ArrayList(); private List prefSums = new ArrayList(); @@ -18,7 +18,7 @@ public class IntervalSumArray { /** * Creates empty IntervalSumArray */ - public IntervalSumArray() { + public IntervalSum() { values.add(0); prefSums.add(0); } @@ -30,7 +30,7 @@ public IntervalSumArray() { * * @param size size of IntervalSumArray */ - public IntervalSumArray(int size) { + public IntervalSum(int size) { for (int i = 0; i values) { + public IntervalSum(Iterable values) { for (Integer v: values) add(v); } diff --git a/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java b/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java index f6b4ef66..5d9f0bca 100644 --- a/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java +++ b/src/com/jwetherell/algorithms/data_structures/LowestCommonAncestor.java @@ -32,7 +32,7 @@ public static class NodesNotInSameTreeException extends Exception { * @return lower common ancestor * @throws NodesNotInSameTreeException if nodes don't have common root */ - public static RootedTree lowestCommonAncestor(RootedTree node1, RootedTree node2) throws NodesNotInSameTreeException { + public static TreeNode lowestCommonAncestor(TreeNode node1, TreeNode node2) throws NodesNotInSameTreeException { if (node1 == node2) return node1; else if (node1.depth < node2.depth) @@ -67,30 +67,30 @@ else if (node1.depth > node2.depth) { } } - public static final class RootedTree { + public static final class TreeNode { - private final List> ancestors = new ArrayList>(); - private final List> children = new ArrayList>(); + private final List> ancestors = new ArrayList>(); + private final List> children = new ArrayList>(); - private T value = null; - private int depth = 0; + private T value = null; + private int depth = 0; /** * Creates tree with root only. * */ - public RootedTree() { } + public TreeNode() { } /** * Creates tree with root (storing value) only. * * @param value value to be stored in root */ - public RootedTree(T value) { + public TreeNode(T value) { this.value = value; } - private RootedTree(RootedTree parent) { + private TreeNode(TreeNode parent) { parent.children.add(this); this.ancestors.add(parent); this.depth = parent.depth + 1; @@ -105,7 +105,7 @@ private RootedTree(RootedTree parent) { } } - public RootedTree setValue(T value) { + public TreeNode setValue(T value) { this.value = value; return this; } @@ -117,8 +117,8 @@ public RootedTree setValue(T value) { * * @return added child */ - public RootedTree addChild() { - return new RootedTree(this); + public TreeNode addChild() { + return new TreeNode(this); } /** @@ -129,7 +129,7 @@ public RootedTree addChild() { * @param value value to be stored in new child * @return added child */ - public RootedTree addChild(T value) { + public TreeNode addChild(T value) { return addChild().setValue(value); } @@ -148,14 +148,14 @@ public T getValue() { * @param value value to be find * @return subtree with given value in the root */ - public RootedTree find(T value) { + public TreeNode find(T value) { if (this.value == null) { if (value == null) return this; } else if (this.value.equals(value)) return this; - for (RootedTree child: children) { - final RootedTree res = child.find(value); + for (TreeNode child: children) { + final TreeNode res = child.find(value); if (res != null) return res; } diff --git a/test/com/jwetherell/algorithms/data_structures/test/IntervalSumArrayTest.java b/test/com/jwetherell/algorithms/data_structures/test/IntervalSumTest.java similarity index 83% rename from test/com/jwetherell/algorithms/data_structures/test/IntervalSumArrayTest.java rename to test/com/jwetherell/algorithms/data_structures/test/IntervalSumTest.java index 35770271..2203ef02 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/IntervalSumArrayTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/IntervalSumTest.java @@ -9,13 +9,13 @@ import org.junit.Test; -import com.jwetherell.algorithms.data_structures.IntervalSumArray; +import com.jwetherell.algorithms.data_structures.IntervalSum; -public class IntervalSumArrayTest { +public class IntervalSumTest { @Test public void properSumAllElementsTest() { - final IntervalSumArray sub = new IntervalSumArray(); + final IntervalSum sub = new IntervalSum(); for (int i = 0; i<=100; i++) sub.add(i); for (int i = 0; i<=100; i++) @@ -29,7 +29,7 @@ public void randomGeneratedTest() { final List list = new ArrayList(); for (int i = 0; i<=100; i++) list.add(i); - final IntervalSumArray sum = new IntervalSumArray(list); + final IntervalSum sum = new IntervalSum(list); for (int i = 0; i<1000000; i++) { final int pos = generator.nextInt(100); final int val = generator.nextInt(2000000) - 1000000; @@ -55,7 +55,7 @@ public void randomGeneratedTest() { @Test public void setIndexOutOfRangeTest() { - final IntervalSumArray sum = new IntervalSumArray(100); + final IntervalSum sum = new IntervalSum(100); boolean thrown = false; try { sum.set(101, 10); @@ -67,7 +67,7 @@ public void setIndexOutOfRangeTest() { @Test public void sumIndexOutOfRangeTest() { - final IntervalSumArray sum = new IntervalSumArray(100); + final IntervalSum sum = new IntervalSum(100); boolean thrown = false; try { sum.sum(101); @@ -79,7 +79,7 @@ public void sumIndexOutOfRangeTest() { @Test public void endBeforeStartTest() { - final IntervalSumArray sum = new IntervalSumArray(100); + final IntervalSum sum = new IntervalSum(100); boolean thrown = false; try { sum.sum(101, 100); diff --git a/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java b/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java index b8e43f5f..ae78eabe 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/LowestCommonAncestorTest.java @@ -9,23 +9,23 @@ import com.jwetherell.algorithms.data_structures.LowestCommonAncestor; import com.jwetherell.algorithms.data_structures.LowestCommonAncestor.NodesNotInSameTreeException; -import com.jwetherell.algorithms.data_structures.LowestCommonAncestor.RootedTree; +import com.jwetherell.algorithms.data_structures.LowestCommonAncestor.TreeNode; public class LowestCommonAncestorTest { @Test public void largeTreeTest() throws NodesNotInSameTreeException { - final RootedTree root = new RootedTree(); - final RootedTree left = root.addChild(); - final RootedTree middle = root.addChild(); - final RootedTree right = root.addChild(); + final TreeNode root = new TreeNode(); + final TreeNode left = root.addChild(); + final TreeNode middle = root.addChild(); + final TreeNode right = root.addChild(); //long path - RootedTree v = left; + TreeNode v = left; for (int i = 0; i<1000; i++) v = v.addChild(); - RootedTree leftRight = left.addChild(); + TreeNode leftRight = left.addChild(); assertEquals(LowestCommonAncestor.lowestCommonAncestor(v, leftRight), left); for (int i = 0; i<2000; i++) { @@ -37,7 +37,7 @@ public void largeTreeTest() throws NodesNotInSameTreeException { assertEquals(LowestCommonAncestor.lowestCommonAncestor(root, right), root); assertEquals(LowestCommonAncestor.lowestCommonAncestor(root, root), root); - final RootedTree root2 = new RootedTree(); + final TreeNode root2 = new TreeNode(); boolean thrownException = false; try { LowestCommonAncestor.lowestCommonAncestor(v, root2); @@ -46,7 +46,7 @@ public void largeTreeTest() throws NodesNotInSameTreeException { } assertTrue(thrownException); - final RootedTree deepChild = v.addChild(101); + final TreeNode deepChild = v.addChild(101); assertEquals(deepChild, root.find(101)); assertTrue(root.contains(101)); From f067a8955ff2e2fd239e698d03d6c7f66a48392a Mon Sep 17 00:00:00 2001 From: geosmart Date: Wed, 11 Oct 2017 19:16:39 +0800 Subject: [PATCH 09/41] Update BinarySearchTree.java comment rotateRight is not right --- .../jwetherell/algorithms/data_structures/BinarySearchTree.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java b/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java index d2b832a5..f956090c 100644 --- a/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java +++ b/src/com/jwetherell/algorithms/data_structures/BinarySearchTree.java @@ -185,7 +185,7 @@ protected void rotateLeft(Node node) { * Rotate tree right at sub-tree rooted at node. * * @param node - * Root of tree to rotate left. + * Root of tree to rotate right. */ protected void rotateRight(Node node) { Node parent = node.parent; From c42708bda0dbf68cb0f13e5a74358e4233842f66 Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Sun, 15 Oct 2017 14:15:02 +0900 Subject: [PATCH 10/41] fix typo --- src/com/jwetherell/algorithms/numbers/Integers.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/jwetherell/algorithms/numbers/Integers.java b/src/com/jwetherell/algorithms/numbers/Integers.java index 809ac8a6..963f585e 100644 --- a/src/com/jwetherell/algorithms/numbers/Integers.java +++ b/src/com/jwetherell/algorithms/numbers/Integers.java @@ -119,7 +119,7 @@ public static final boolean powerOfTwoUsingBits(int numberToCheck) { singleDigits.put(14,"fourteen"); singleDigits.put(15,"fifteen"); singleDigits.put(16,"sixteen"); - singleDigits.put(17,"seventee"); + singleDigits.put(17,"seventeen"); singleDigits.put(18,"eighteen"); singleDigits.put(19,"nineteen"); } From fbf24fa83ba002ff28761f6692f00ae0b5fcdff5 Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Mon, 13 Nov 2017 13:54:58 +0900 Subject: [PATCH 11/41] Remove obsolete code --- src/com/jwetherell/algorithms/data_structures/List.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/jwetherell/algorithms/data_structures/List.java b/src/com/jwetherell/algorithms/data_structures/List.java index 37eb1db8..09d03a8c 100644 --- a/src/com/jwetherell/algorithms/data_structures/List.java +++ b/src/com/jwetherell/algorithms/data_structures/List.java @@ -67,8 +67,8 @@ public boolean remove(T value) { for (int i = 0; i < size; i++) { T obj = array[i]; if (obj.equals(value)) { - if (remove(i)!=null) return true; - return false; + remove(i); + return true; } } return false; From 724ef3c7daa90e8868110041e153c4e77aa5937d Mon Sep 17 00:00:00 2001 From: SungminKim Date: Mon, 20 Nov 2017 15:18:08 +0900 Subject: [PATCH 12/41] Modified Dijkstra test typo --- test/com/jwetherell/algorithms/graph/test/Graphs.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/com/jwetherell/algorithms/graph/test/Graphs.java b/test/com/jwetherell/algorithms/graph/test/Graphs.java index e63e5602..1908497a 100644 --- a/test/com/jwetherell/algorithms/graph/test/Graphs.java +++ b/test/com/jwetherell/algorithms/graph/test/Graphs.java @@ -372,13 +372,13 @@ public void testDijstraUndirected() { for (Graph.Vertex v : map1.keySet()) { final Graph.CostPathPair path1 = map1.get(v); final Graph.CostPathPair path2 = getIdealUndirectedPath(undirected).get(v); - assertTrue("Dijstra's shortest path error. path1="+path1+" path2="+path2, path1.equals(path2)); + assertTrue("Dijkstra's shortest path error. path1="+path1+" path2="+path2, path1.equals(path2)); } final Graph.CostPathPair pair1 = Dijkstra.getShortestPath(undirected.graph, start, end); assertTrue("No path from " + start.getValue() + " to " + end.getValue(), (pair1 != null)); - assertTrue("Dijstra's shortest path error. pair="+pair1+" pair="+getIdealUndirectedPathPair(undirected), pair1.equals(getIdealUndirectedPathPair(undirected))); + assertTrue("Dijkstra's shortest path error. pair="+pair1+" pair="+getIdealUndirectedPathPair(undirected), pair1.equals(getIdealUndirectedPathPair(undirected))); } } From d428afeffd5edc58134b7409dc3acba3fd663237 Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Mon, 20 Nov 2017 15:21:27 +0900 Subject: [PATCH 13/41] Fixed Param of Comment --- src/com/jwetherell/algorithms/data_structures/BTree.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/jwetherell/algorithms/data_structures/BTree.java b/src/com/jwetherell/algorithms/data_structures/BTree.java index d15d398c..d930076b 100644 --- a/src/com/jwetherell/algorithms/data_structures/BTree.java +++ b/src/com/jwetherell/algorithms/data_structures/BTree.java @@ -109,7 +109,7 @@ public boolean add(T value) { /** * The node's key size is greater than maxKeySize, split down the middle. * - * @param node + * @param nodeToSplit * to split. */ private void split(Node nodeToSplit) { From 6596bbe775a316cbb489837d6c2ebd6ae739f566 Mon Sep 17 00:00:00 2001 From: SungminKim Date: Mon, 20 Nov 2017 15:24:23 +0900 Subject: [PATCH 14/41] Fixed Dijkstra test typo --- test/com/jwetherell/algorithms/graph/test/Graphs.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/com/jwetherell/algorithms/graph/test/Graphs.java b/test/com/jwetherell/algorithms/graph/test/Graphs.java index 1908497a..6a17dcd0 100644 --- a/test/com/jwetherell/algorithms/graph/test/Graphs.java +++ b/test/com/jwetherell/algorithms/graph/test/Graphs.java @@ -361,7 +361,7 @@ private Graph.CostPathPair getIdealDirectedWithNegWeightsPathPair(Direc } @Test - public void testDijstraUndirected() { + public void testDijkstraUndirected() { final UndirectedGraph undirected = new UndirectedGraph(); final Graph.Vertex start = undirected.v1; final Graph.Vertex end = undirected.v5; @@ -546,14 +546,14 @@ public void testDijkstraDirected() { for (Graph.Vertex v : map1.keySet()) { final Graph.CostPathPair path1 = map1.get(v); final Graph.CostPathPair path2 = getIdealDirectedPath(directed).get(v); - assertTrue("Dijstra's shortest path error. path1="+path1+" path2="+path2, path1.equals(path2)); + assertTrue("Dijkstra's shortest path error. path1="+path1+" path2="+path2, path1.equals(path2)); } final Graph.CostPathPair pair1 = Dijkstra.getShortestPath(directed.graph, start, end); assertTrue("No path from "+start.getValue()+" to "+end.getValue(), (pair1!=null)); // Compare pair - assertTrue("Dijstra's shortest path error. pair1="+pair1+" idealPathPair="+getIdealPathPair(directed), pair1.equals(getIdealPathPair(directed))); + assertTrue("Dijkstra's shortest path error. pair1="+pair1+" idealPathPair="+getIdealPathPair(directed), pair1.equals(getIdealPathPair(directed))); } @Test @@ -579,7 +579,7 @@ public void testBellmanFordDirected() { } @Test - public void testDijstraDirectedWihtNegativeWeights() { + public void testDijkstraDirectedWihtNegativeWeights() { final DirectedWithNegativeWeights directedWithNegWeights = new DirectedWithNegativeWeights(); { // DIRECTED GRAPH (WITH NEGATIVE WEIGHTS) final Graph.Vertex start = directedWithNegWeights.v1; From 3126082cae851e3a685fc315947f2af033c89444 Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Mon, 20 Nov 2017 15:50:11 +0900 Subject: [PATCH 15/41] Fixed param of Comment in getDirenctions --- src/com/jwetherell/algorithms/data_structures/BinaryHeap.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java index 61f38a16..1b876f36 100644 --- a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java +++ b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java @@ -449,8 +449,8 @@ public int size() { /** * Get the navigation directions through the tree to the index. * - * @param index - * of the Node to get directions for. + * @param idx + * index of the Node to get directions for. * @return Integer array representing the directions to the index. */ private static int[] getDirections(int idx) { From 5dc01d3cbe888b9257d34e33daa1c179bec159b6 Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Mon, 20 Nov 2017 15:50:43 +0900 Subject: [PATCH 16/41] Fixed param of Comment in validateNode --- src/com/jwetherell/algorithms/data_structures/BinaryHeap.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java index 1b876f36..81932349 100644 --- a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java +++ b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java @@ -289,8 +289,8 @@ public boolean validate() { /** * Validate the node for the heap invariants. * - * @param node - * to validate for. + * @param index + * of node to validate for. * @return True if node is valid. */ private boolean validateNode(int index) { From ed54292376c43ca30243407f37604d665de0bd94 Mon Sep 17 00:00:00 2001 From: SungminKim Date: Mon, 20 Nov 2017 16:03:47 +0900 Subject: [PATCH 17/41] Fix incorrected spelling --- src/com/jwetherell/algorithms/numbers/Integers.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/jwetherell/algorithms/numbers/Integers.java b/src/com/jwetherell/algorithms/numbers/Integers.java index 963f585e..fb86fd3d 100644 --- a/src/com/jwetherell/algorithms/numbers/Integers.java +++ b/src/com/jwetherell/algorithms/numbers/Integers.java @@ -129,12 +129,12 @@ public static final boolean powerOfTwoUsingBits(int numberToCheck) { multiDigits.put(10,"ten"); multiDigits.put(20,"twenty"); multiDigits.put(30,"thirty"); - multiDigits.put(40,"fourty"); + multiDigits.put(40,"forty"); multiDigits.put(50,"fifty"); multiDigits.put(60,"sixty"); multiDigits.put(70,"seventy"); multiDigits.put(80,"eighty"); - multiDigits.put(90,"ninty"); + multiDigits.put(90,"ninety"); } private static final int BILLION = 1000000000; From c38908c3151c24ea2eacf04dec80ac7e09a90e84 Mon Sep 17 00:00:00 2001 From: SungminKim Date: Mon, 20 Nov 2017 16:04:40 +0900 Subject: [PATCH 18/41] Fix incorrected spelling --- test/com/jwetherell/algorithms/numbers/test/Numbers.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/com/jwetherell/algorithms/numbers/test/Numbers.java b/test/com/jwetherell/algorithms/numbers/test/Numbers.java index dfe714cd..dcf087ab 100644 --- a/test/com/jwetherell/algorithms/numbers/test/Numbers.java +++ b/test/com/jwetherell/algorithms/numbers/test/Numbers.java @@ -91,17 +91,17 @@ public void testToEnglish() { assertTrue("toEnglish error. a="+a+" expected="+check+" got="+english, (check.equals(english))); a = 199; - check = "one-hundred ninty-nine"; + check = "one-hundred ninety-nine"; english = Integers.toEnglish(a); assertTrue("toEnglish error. a="+a+" expected="+check+" got="+english, (check.equals(english))); a = Integer.MAX_VALUE; // 2,147,483,647 - check = "two-billion one-hundred fourty-seven-million four-hundred eighty-three-thousand six-hundred fourty-seven"; + check = "two-billion one-hundred forty-seven-million four-hundred eighty-three-thousand six-hundred forty-seven"; english = Integers.toEnglish(a); assertTrue("toEnglish error. a="+a+" expected="+check+" got="+english, (check.equals(english))); a = Integer.MIN_VALUE+1; // -2,147,483,647 - check = "negative two-billion one-hundred fourty-seven-million four-hundred eighty-three-thousand six-hundred fourty-seven"; + check = "negative two-billion one-hundred forty-seven-million four-hundred eighty-three-thousand six-hundred forty-seven"; english = Integers.toEnglish(a); assertTrue("toEnglish error. a="+a+" expected="+check+" got="+english, (check.equals(english))); } From 616213e71c4b8416764a6f32cda9396c85c2f5b0 Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Mon, 20 Nov 2017 16:17:53 +0900 Subject: [PATCH 19/41] Test unused Method --- .../data_structures/CompactSuffixTrie.java | 5 +++++ .../test/CompactSuffixTrieTests.java | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java b/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java index bc64b598..b2796543 100644 --- a/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java +++ b/src/com/jwetherell/algorithms/data_structures/CompactSuffixTrie.java @@ -141,4 +141,9 @@ private Set getSuffixes(PatriciaTrie.Node node, String prefix) { public String toString() { return PatriciaTrie.PatriciaTriePrinter.getString(tree); } + + public boolean equals(CompactSuffixTrie trie){ + if(this.getSuffixes().equals(trie.getSuffixes())) return true; + return false; + } } diff --git a/test/com/jwetherell/algorithms/data_structures/test/CompactSuffixTrieTests.java b/test/com/jwetherell/algorithms/data_structures/test/CompactSuffixTrieTests.java index 8b6bce27..f32f3f64 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/CompactSuffixTrieTests.java +++ b/test/com/jwetherell/algorithms/data_structures/test/CompactSuffixTrieTests.java @@ -25,4 +25,26 @@ public void testCompactSuffixTrie() { exists = trie.doesSubStringExist(pass); assertTrue("YIKES!! " + pass + " doesn't exists.", exists); } + + @Test + public void testCompactSuffixTrie_equals() { + String bookkeeper = "bookkeeper"; + CompactSuffixTrie trie = new CompactSuffixTrie(bookkeeper); + + String bookkeeper_1 = "bookkeeper"; + CompactSuffixTrie trie_1 = new CompactSuffixTrie(bookkeeper_1); + + boolean equal = trie.equals(trie_1); + assertTrue("YIKES!! " + bookkeeper + " and " + bookkeeper_1 + " are not equal.", equal); + + + String failed = "failed"; + trie = new CompactSuffixTrie(failed); + + String failed_1 = "failet"; + trie_1 = new CompactSuffixTrie(failed_1); + + equal = trie.equals(trie_1); + assertFalse("YIKES!! " + failed + " and " + failed_1 + " are equal.", equal); + } } From aac1f8b1a704140283e8ca99f762aa5b1d3c95b4 Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Mon, 27 Nov 2017 16:29:45 +0900 Subject: [PATCH 20/41] Fixed Param comment --- src/com/jwetherell/algorithms/data_structures/HashMap.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/com/jwetherell/algorithms/data_structures/HashMap.java b/src/com/jwetherell/algorithms/data_structures/HashMap.java index 76c669de..6556d4b9 100644 --- a/src/com/jwetherell/algorithms/data_structures/HashMap.java +++ b/src/com/jwetherell/algorithms/data_structures/HashMap.java @@ -212,9 +212,6 @@ private void initializeMap(int length) { * * @param h * hash to get index of. - * @param length - * length of array - * * @return Integer which represents the key. */ private int indexOf(int h) { From ac23dab6c7d6663808d28e2fc920d43cbe02da5d Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Mon, 27 Nov 2017 16:34:11 +0900 Subject: [PATCH 21/41] Fixed Param comment in InterverTree --- .../algorithms/data_structures/IntervalTree.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/com/jwetherell/algorithms/data_structures/IntervalTree.java b/src/com/jwetherell/algorithms/data_structures/IntervalTree.java index a04113ba..1a82ef30 100644 --- a/src/com/jwetherell/algorithms/data_structures/IntervalTree.java +++ b/src/com/jwetherell/algorithms/data_structures/IntervalTree.java @@ -314,7 +314,7 @@ public IntervalData(long start, long end, O object) { /** * Interval data list which should all be unique * - * @param list + * @param set * of interval data objects */ public IntervalData(long start, long end, Set set) { @@ -389,10 +389,9 @@ public IntervalData copy() { /** * Query inside this data object. * - * @param start - * of range to query for. - * @param end - * of range to query for. + * @param index + * to find Data. + * * @return Data queried for or NULL if it doesn't match the query. */ public IntervalData query(long index) { From b85ef7e0cb69af9ce2e8673594a6c087ac895929 Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Mon, 27 Nov 2017 16:38:43 +0900 Subject: [PATCH 22/41] Fixed Param comment in SegmentTree --- src/com/jwetherell/algorithms/data_structures/SegmentTree.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/jwetherell/algorithms/data_structures/SegmentTree.java b/src/com/jwetherell/algorithms/data_structures/SegmentTree.java index 9a04b8e6..fb516d8e 100644 --- a/src/com/jwetherell/algorithms/data_structures/SegmentTree.java +++ b/src/com/jwetherell/algorithms/data_structures/SegmentTree.java @@ -696,7 +696,7 @@ public IntervalData(long start, long end, O object) { /** * Interval data list which should all be unique * - * @param list + * @param set * of interval data objects */ public IntervalData(long start, long end, Set set) { From 5033342d1c679beb35ece62592b80cb4c66df59a Mon Sep 17 00:00:00 2001 From: ParkChangHan Date: Mon, 27 Nov 2017 16:45:53 +0900 Subject: [PATCH 23/41] Fixed Param comment in ConnectedComponents --- src/com/jwetherell/algorithms/graph/ConnectedComponents.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/com/jwetherell/algorithms/graph/ConnectedComponents.java b/src/com/jwetherell/algorithms/graph/ConnectedComponents.java index dae44174..e096950c 100644 --- a/src/com/jwetherell/algorithms/graph/ConnectedComponents.java +++ b/src/com/jwetherell/algorithms/graph/ConnectedComponents.java @@ -25,7 +25,8 @@ private ConnectedComponents() { } /** * Finds the connected components subsets of the Graph. * - * @param g Graph to find connected components. + * @param graph + * to find connected components. * @return List of connected components in the Graph. */ public static final > List>> getConnectedComponents(Graph graph) { From 2b0a3b03a8d13ac06532c3a803ec08286eaa8bb6 Mon Sep 17 00:00:00 2001 From: aniders <33834501+aniders@users.noreply.github.com> Date: Wed, 14 Feb 2018 23:45:39 +0530 Subject: [PATCH 24/41] Update Knapsack.java Changed OR condition to &&, OR may possibly result in ArrayIndexOutOfBoundException , (as it does for following input capacity : 60, weights: [10, 20, 33], values: [10, 3, 30]) --- src/com/jwetherell/algorithms/mathematics/Knapsack.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/jwetherell/algorithms/mathematics/Knapsack.java b/src/com/jwetherell/algorithms/mathematics/Knapsack.java index 69f848c2..09079ce9 100644 --- a/src/com/jwetherell/algorithms/mathematics/Knapsack.java +++ b/src/com/jwetherell/algorithms/mathematics/Knapsack.java @@ -40,7 +40,7 @@ public static final int[] zeroOneKnapsack(int[] values, int[] weights, int capac final List list = new ArrayList(); int i = height - 1; int j = width - 1; - while (i != 0 || j != 0) { + while (i != 0 && j != 0) { int current = output[i][j]; int above = output[i - 1][j]; if (current == above) { From 7a7549d8dfbd2653839c8f39fc8eaca95f38ee2e Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Thu, 3 May 2018 10:18:30 -0400 Subject: [PATCH 25/41] Closes #91 Bug fix --- .../LongestIncreasingSubsequence.java | 50 +++++++++++++------ .../algorithms/sequence/test/Sequences.java | 2 +- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java b/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java index 4421896b..598f6885 100644 --- a/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java +++ b/src/com/jwetherell/algorithms/sequence/LongestIncreasingSubsequence.java @@ -1,9 +1,5 @@ package com.jwetherell.algorithms.sequence; -import com.jwetherell.algorithms.search.LowerBound; - -import java.util.Arrays; - /** * In computer science, the longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence's elements are in sorted order, lowest to highest, and in * which the subsequence is as long as possible. This subsequence is not necessarily contiguous, or unique. @@ -20,19 +16,43 @@ private LongestIncreasingSubsequence() { } /** * Longest increasing subsequence solved using dynamic programming. */ - public static int[] getLongestIncreasingSubsequence(int[] sequence) { - final int[] resultSequence = new int[sequence.length]; + public static int[] getLongestIncreasingSubsequence(int[] X) { + final int[] P = new int[X.length]; + final int[] M = new int[X.length+1]; + int L = 0; + for (int i=0; i L) { + // If we found a subsequence longer than any we've found yet, update L + L = newL; + } + } - int resultLength = 0; - for (int i = 0; i < sequence.length; ++i) { - // try to find the best place for new element in sorted result - final int pos = LowerBound.lowerBound(resultSequence, resultLength, sequence[i]); //O(log n) - // if the best place is at the end increase result length - if (pos >= resultLength) - resultLength++; - resultSequence[pos] = sequence[i]; + // Reconstruct the longest increasing subsequence + final int[] S = new int[L]; + int k = M[L]; + for (int i=L-1; i>=0; i--) { + S[i] = X[k]; + k = P[k]; } - return Arrays.copyOfRange(resultSequence, 0, resultLength); + return S; } } diff --git a/test/com/jwetherell/algorithms/sequence/test/Sequences.java b/test/com/jwetherell/algorithms/sequence/test/Sequences.java index 18b7d0c1..bd3fa1d8 100644 --- a/test/com/jwetherell/algorithms/sequence/test/Sequences.java +++ b/test/com/jwetherell/algorithms/sequence/test/Sequences.java @@ -141,7 +141,7 @@ public void testLongestIncreasingSubsequence() { sequencesLis.add(new int[]{1, 2, 3}); sequences.add(new int[]{0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}); - sequencesLis.add(new int[]{0, 1, 3, 7, 11, 15}); + sequencesLis.add(new int[]{0, 2, 6, 9, 11, 15}); assertTrue("Longest increasing subsequence error. Sequences size=" + sequences.size() + " SequencesList size:" + sequencesLis.size(), sequences.size() == sequencesLis.size()); From 4c8e4c42f04f1d8b827dd7196e6e0f28a5091960 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Thu, 21 Feb 2019 09:21:34 -0500 Subject: [PATCH 26/41] Updates to yaml to get jdk6 working in travis-ci --- .travis.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index e2324a51..3c8d4d3f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,15 @@ addons: - openjdk-6-jdk language: java +dist: trusty + +# for gh-1388 (workaround for supporting test using OpenJDK 6) +matrix: + include: + - env: JDK='OpenJDK 6' + before_install: + - echo "MAVEN_OPTS='-Dlicense.skip=true'" > ~/.mavenrc + - TRAVIS_JDK_VERSION=openjdk6 jdk: - oraclejdk9 @@ -22,7 +31,7 @@ jdk: # - openjdk9 - openjdk8 - openjdk7 - - openjdk6 +# - openjdk6 env: - TEST_SUITE=run_tests From 62e9f1dceb95faae646826377e569dbf7ba9f25d Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Thu, 21 Feb 2019 09:31:26 -0500 Subject: [PATCH 27/41] Updates to yaml to get jdk6 working in travis-ci --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3c8d4d3f..f34c197c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,9 @@ dist: trusty # for gh-1388 (workaround for supporting test using OpenJDK 6) matrix: include: - - env: JDK='OpenJDK 6' + env: + - JDK='OpenJDK 6' + - TEST_SUITE=run_tests before_install: - echo "MAVEN_OPTS='-Dlicense.skip=true'" > ~/.mavenrc - TRAVIS_JDK_VERSION=openjdk6 From f0639aa08edeca4781714538d027eeb51d8e4735 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Thu, 21 Feb 2019 09:50:48 -0500 Subject: [PATCH 28/41] Dropping builds for jdk6 because travis-ci no longer supports --- .travis.yml | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/.travis.yml b/.travis.yml index f34c197c..40a330e3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,23 +6,7 @@ before_script: - echo $JAVA_OPTS - echo $ANT_OPTS -addons: - apt: - packages: - - openjdk-6-jdk - language: java -dist: trusty - -# for gh-1388 (workaround for supporting test using OpenJDK 6) -matrix: - include: - env: - - JDK='OpenJDK 6' - - TEST_SUITE=run_tests - before_install: - - echo "MAVEN_OPTS='-Dlicense.skip=true'" > ~/.mavenrc - - TRAVIS_JDK_VERSION=openjdk6 jdk: - oraclejdk9 From e5966440895ebed5d9a448ad9785db090b99c2e5 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Thu, 21 Feb 2019 10:26:09 -0500 Subject: [PATCH 29/41] Fixed the Queue grow method comment about resizing --- src/com/jwetherell/algorithms/data_structures/Queue.java | 2 +- .../jwetherell/algorithms/data_structures/test/QueueTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/com/jwetherell/algorithms/data_structures/Queue.java b/src/com/jwetherell/algorithms/data_structures/Queue.java index 1a078f71..845e65e5 100644 --- a/src/com/jwetherell/algorithms/data_structures/Queue.java +++ b/src/com/jwetherell/algorithms/data_structures/Queue.java @@ -111,7 +111,7 @@ private boolean remove(int index) { return true; } - // Grow the array by 50% and rearrange to make sequential + // Triple the size of the underlying array and rearrange to make sequential private void grow(int size) { int growSize = (size + (size<<1)); T[] temp = (T[]) new Object[growSize]; diff --git a/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java b/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java index b4512e57..153efc46 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java +++ b/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java @@ -16,7 +16,7 @@ public class QueueTests { @Test public void testArrayQueue() { - TestData data = Utils.generateTestData(100); + TestData data = Utils.generateTestData(100000); String aName = "Queue [array]"; Queue.ArrayQueue aQueue = new Queue.ArrayQueue(); From cdafbb9471fd2876b0df7657be4d18a43d8a7110 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Thu, 21 Feb 2019 11:17:36 -0500 Subject: [PATCH 30/41] Found a bug in the grow method of the ArrayQueue --- .../jwetherell/algorithms/data_structures/Queue.java | 10 +++++----- .../algorithms/data_structures/test/QueueTests.java | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/com/jwetherell/algorithms/data_structures/Queue.java b/src/com/jwetherell/algorithms/data_structures/Queue.java index 845e65e5..4aa200b7 100644 --- a/src/com/jwetherell/algorithms/data_structures/Queue.java +++ b/src/com/jwetherell/algorithms/data_structures/Queue.java @@ -117,11 +117,11 @@ private void grow(int size) { T[] temp = (T[]) new Object[growSize]; // Since the array can wrap around, make sure you grab the first chunk int adjLast = lastIndex % array.length; - if (adjLast < firstIndex) { - System.arraycopy(array, 0, temp, array.length-adjLast, adjLast+1); + if (adjLast > 0 && adjLast <= firstIndex) { + System.arraycopy(array, 0, temp, array.length-adjLast, adjLast); } // Copy the remaining - System.arraycopy(array, firstIndex, temp, 0, array.length-firstIndex); + System.arraycopy(array, firstIndex, temp, 0, array.length - firstIndex); array = null; array = temp; lastIndex = (lastIndex - firstIndex); @@ -134,9 +134,9 @@ private void shrink() { T[] temp = (T[]) new Object[shrinkSize]; // Since the array can wrap around, make sure you grab the first chunk int adjLast = lastIndex % array.length; - int endIndex = (lastIndex>array.length)?array.length:lastIndex; + int endIndex = (lastIndex>array.length) ? array.length : lastIndex; if (adjLast <= firstIndex) { - System.arraycopy(array, 0, temp, array.length-firstIndex, adjLast); + System.arraycopy(array, 0, temp, array.length - firstIndex, adjLast); } // Copy the remaining System.arraycopy(array, firstIndex, temp, 0, endIndex-firstIndex); diff --git a/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java b/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java index 153efc46..9dfb6759 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java +++ b/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java @@ -16,7 +16,7 @@ public class QueueTests { @Test public void testArrayQueue() { - TestData data = Utils.generateTestData(100000); + TestData data = Utils.generateTestData(2500); String aName = "Queue [array]"; Queue.ArrayQueue aQueue = new Queue.ArrayQueue(); @@ -30,7 +30,7 @@ public void testArrayQueue() { @Test public void testLinkedQueue() { - TestData data = Utils.generateTestData(100); + TestData data = Utils.generateTestData(250); String lName = "Queue [linked]"; Queue.LinkedQueue lQueue = new Queue.LinkedQueue(); From b98248222b875c56c8caf69eec44574ee05dd1ce Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Thu, 21 Feb 2019 11:29:19 -0500 Subject: [PATCH 31/41] Fixed a bug in the getHeadValue of the Heap structures --- .../data_structures/BinaryHeap.java | 2 +- .../data_structures/test/BinaryHeapTests.java | 30 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java index 81932349..ac2ae1f9 100644 --- a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java +++ b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java @@ -341,7 +341,7 @@ public T[] getHeap() { */ @Override public T getHeadValue() { - if (array.length == 0) return null; + if (size == 0 || array.length == 0) return null; return array[0]; } diff --git a/test/com/jwetherell/algorithms/data_structures/test/BinaryHeapTests.java b/test/com/jwetherell/algorithms/data_structures/test/BinaryHeapTests.java index 865c266b..68591215 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/BinaryHeapTests.java +++ b/test/com/jwetherell/algorithms/data_structures/test/BinaryHeapTests.java @@ -1,6 +1,7 @@ package com.jwetherell.algorithms.data_structures.test; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNull; import java.util.Collection; @@ -16,7 +17,7 @@ public class BinaryHeapTests { @Test public void testMinHeap() { - TestData data = Utils.generateTestData(100); + TestData data = Utils.generateTestData(2500); String aNameMin = "Min-Heap [array]"; BinaryHeap.BinaryHeapArray aHeapMin = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MIN); @@ -26,6 +27,12 @@ public void testMinHeap() { assertTrue(JavaCollectionTest.testCollection(aCollectionMin, Integer.class, aNameMin, data.unsorted, data.sorted, data.invalid)); + BinaryHeap.BinaryHeapArray aHeapNull = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MIN); + aHeapNull.add(10); + aHeapNull.add(11); + aHeapNull.clear(); + assertNull(aHeapNull.getHeadValue()); // we expect null here + String tNameMin = "Min-Heap [tree]"; BinaryHeap.BinaryHeapTree tHeapMin = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MIN); Collection tCollectionMin = tHeapMin.toCollection(); @@ -33,11 +40,18 @@ public void testMinHeap() { data.unsorted, data.sorted, data.invalid)); assertTrue(JavaCollectionTest.testCollection(tCollectionMin, Integer.class, tNameMin, data.unsorted, data.sorted, data.invalid)); + + BinaryHeap.BinaryHeapTree tHeapNull = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MIN); + tHeapNull.add(10); + tHeapNull.add(11); + tHeapNull.clear(); + assertNull(tHeapNull.getHeadValue()); // we expect null here + } @Test public void testMaxHeap() { - TestData data = Utils.generateTestData(1000); + TestData data = Utils.generateTestData(2500); String aNameMax = "Max-Heap [array]"; BinaryHeap.BinaryHeapArray aHeapMax = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MAX); @@ -47,6 +61,12 @@ public void testMaxHeap() { assertTrue(JavaCollectionTest.testCollection(aCollectionMax, Integer.class, aNameMax, data.unsorted, data.sorted, data.invalid)); + BinaryHeap.BinaryHeapArray aHeapNull = new BinaryHeap.BinaryHeapArray(BinaryHeap.Type.MAX); + aHeapNull.add(10); + aHeapNull.add(11); + aHeapNull.clear(); + assertNull(aHeapNull.getHeadValue()); // we expect null here + String lNameMax = "Max-Heap [tree]"; BinaryHeap.BinaryHeapTree tHeapMax = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MAX); Collection tCollectionMax = tHeapMax.toCollection(); @@ -54,5 +74,11 @@ public void testMaxHeap() { data.unsorted, data.sorted, data.invalid)); assertTrue(JavaCollectionTest.testCollection(tCollectionMax, Integer.class, lNameMax, data.unsorted, data.sorted, data.invalid)); + + BinaryHeap.BinaryHeapTree tHeapNull = new BinaryHeap.BinaryHeapTree(BinaryHeap.Type.MAX); + tHeapNull.add(10); + tHeapNull.add(11); + tHeapNull.clear(); + assertNull(tHeapNull.getHeadValue()); // we expect null here } } From edf84c527578997bc5e409f4166dd68c17d05790 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Thu, 21 Feb 2019 12:11:39 -0500 Subject: [PATCH 32/41] Fixed a bug in the ArrayQueue iterator --- .../algorithms/data_structures/Queue.java | 2 +- .../data_structures/test/QueueTests.java | 22 +++++++++++++++++++ .../test/common/JavaCollectionTest.java | 6 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/com/jwetherell/algorithms/data_structures/Queue.java b/src/com/jwetherell/algorithms/data_structures/Queue.java index 4aa200b7..92962a4d 100644 --- a/src/com/jwetherell/algorithms/data_structures/Queue.java +++ b/src/com/jwetherell/algorithms/data_structures/Queue.java @@ -563,7 +563,7 @@ public boolean hasNext() { public T next() { if (queue.firstIndex+index < queue.lastIndex) { last = queue.firstIndex+index; - return queue.array[queue.firstIndex+index++]; + return queue.array[(queue.firstIndex + index++) % queue.array.length]; } return null; } diff --git a/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java b/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java index 9dfb6759..6f2be148 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java +++ b/test/com/jwetherell/algorithms/data_structures/test/QueueTests.java @@ -3,6 +3,7 @@ import static org.junit.Assert.assertTrue; import java.util.Collection; +import java.util.Iterator; import org.junit.Test; @@ -26,6 +27,17 @@ public void testArrayQueue() { data.unsorted, data.invalid)); assertTrue(JavaCollectionTest.testCollection(aCollection, Integer.class, aName, data.unsorted, data.sorted, data.invalid)); + + // Specific test based on bug + aQueue = new Queue.ArrayQueue(); + for (int i = 0; i < 1024; i++) { + aQueue.offer(i); + } + aQueue.poll(); + aQueue.offer(1024); + Iterator it = aQueue.toQueue().iterator(); + while (it.hasNext()) + it.next(); } @Test @@ -40,5 +52,15 @@ public void testLinkedQueue() { data.unsorted, data.invalid)); assertTrue(JavaCollectionTest.testCollection(lCollection, Integer.class, lName, data.unsorted, data.sorted, data.invalid)); + + lQueue = new Queue.LinkedQueue(); + for (int i = 0; i < 1024; i++) { + lQueue.offer(i); + } + lQueue.poll(); + lQueue.offer(1024); + Iterator it = lQueue.toQueue().iterator(); + while (it.hasNext()) + it.next(); } } diff --git a/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java b/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java index 12e790a7..c4338d5a 100644 --- a/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java +++ b/test/com/jwetherell/algorithms/data_structures/test/common/JavaCollectionTest.java @@ -78,6 +78,12 @@ private static > boolean addAndRemoveInOrder(Collection< return false; } + if (!IteratorTest.testIterator(collection.iterator())) { + System.err.println(name+" addAndRemoveInOrder iterator failed."); + Utils.handleError(data,collection); + return false; + } + for (int i = 0; i < data.length; i++) { Integer value = data[i]; T item = Utils.parseT(value, type); From 356dfb1daff89b983194c192c7534fa3b3a30126 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Thu, 21 Feb 2019 12:16:28 -0500 Subject: [PATCH 33/41] Fixed a bug in the getHeadValue of the Heap structures --- src/com/jwetherell/algorithms/data_structures/BinaryHeap.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java index ac2ae1f9..08855e28 100644 --- a/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java +++ b/src/com/jwetherell/algorithms/data_structures/BinaryHeap.java @@ -372,7 +372,7 @@ public String toString() { protected static class HeapPrinter { public static > String getString(BinaryHeapArray tree) { - if (tree.array.length == 0) + if (tree.size == 0 || tree.array.length == 0) return "Tree has no nodes."; T root = tree.array[0]; From d3d1d03f029f2188b98579f7278b7e1221ad47db Mon Sep 17 00:00:00 2001 From: Nishant Ingle Date: Sat, 26 Oct 2019 17:06:15 +0530 Subject: [PATCH 34/41] Added Comment on Linear Search Logic --- src/com/jwetherell/algorithms/search/BinarySearch.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/com/jwetherell/algorithms/search/BinarySearch.java b/src/com/jwetherell/algorithms/search/BinarySearch.java index 8d3f8521..b0cbe03d 100644 --- a/src/com/jwetherell/algorithms/search/BinarySearch.java +++ b/src/com/jwetherell/algorithms/search/BinarySearch.java @@ -56,6 +56,7 @@ private static int recursiveFind(int value, int start, int end, boolean optimize } private static final int linearSearch(int value, int start, int end) { + // From index i = start to i = end check if value matches sorted[i] for (int i = start; i <= end; i++) { int iValue = sorted[i]; if (value == iValue) From 16f2fc18ff5ac71ae3eed21dcc4cb8aa7e6711c5 Mon Sep 17 00:00:00 2001 From: gauravdarbhanga <57192490+gauravdarbhanga@users.noreply.github.com> Date: Wed, 30 Oct 2019 23:48:53 +0530 Subject: [PATCH 35/41] Update BubbleSort.java adding comments on the method. --- src/com/jwetherell/algorithms/sorts/BubbleSort.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/com/jwetherell/algorithms/sorts/BubbleSort.java b/src/com/jwetherell/algorithms/sorts/BubbleSort.java index 3ac320ff..824e4e17 100644 --- a/src/com/jwetherell/algorithms/sorts/BubbleSort.java +++ b/src/com/jwetherell/algorithms/sorts/BubbleSort.java @@ -21,7 +21,8 @@ public class BubbleSort> { private BubbleSort() { } - + //@param unsorted array + //@return sorted array public static > T[] sort(T[] unsorted) { boolean swapped = true; int length = unsorted.length; @@ -37,7 +38,7 @@ public static > T[] sort(T[] unsorted) { } return unsorted; } - + //swapping the value private static > void swap(int index1, int index2, T[] unsorted) { T value = unsorted[index1]; unsorted[index1] = unsorted[index2]; From 38d06ee316907262b20ba695f99c129e14eadef4 Mon Sep 17 00:00:00 2001 From: gauravdarbhanga <57192490+gauravdarbhanga@users.noreply.github.com> Date: Wed, 30 Oct 2019 23:59:12 +0530 Subject: [PATCH 36/41] Update CountingSort.java Did comment on the method. --- src/com/jwetherell/algorithms/sorts/CountingSort.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/com/jwetherell/algorithms/sorts/CountingSort.java b/src/com/jwetherell/algorithms/sorts/CountingSort.java index f1593c50..31e6a289 100644 --- a/src/com/jwetherell/algorithms/sorts/CountingSort.java +++ b/src/com/jwetherell/algorithms/sorts/CountingSort.java @@ -27,26 +27,26 @@ private CountingSort() { } public static Integer[] sort(Integer[] unsorted) { int maxValue = findMax(unsorted); - int[] counts = new int[maxValue + 1]; + int[] counts = new int[maxValue + 1];//counts number of elements updateCounts(unsorted, counts); populateCounts(unsorted, counts); return unsorted; } - + //finding maximum value in unsorted array private static int findMax(Integer[] unsorted) { - int max = Integer.MIN_VALUE; + int max = Integer.MIN_VALUE;//assume minimum value(-2147483648) of interger is maximum for (int i : unsorted) { if (i > max) max = i; } return max; } - + //Incrementing the number of counts in unsorted array private static void updateCounts(Integer[] unsorted, int[] counts) { for (int e : unsorted) counts[e]++; } - + private static void populateCounts(Integer[] unsorted, int[] counts) { int index = 0; for (int i = 0; i < counts.length; i++) { From 71d8b30bba0f426607412927279b53637f8b958b Mon Sep 17 00:00:00 2001 From: gauravdarbhanga <57192490+gauravdarbhanga@users.noreply.github.com> Date: Thu, 31 Oct 2019 00:06:22 +0530 Subject: [PATCH 37/41] Update BinarySearch.java --- src/com/jwetherell/algorithms/search/BinarySearch.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/com/jwetherell/algorithms/search/BinarySearch.java b/src/com/jwetherell/algorithms/search/BinarySearch.java index 8d3f8521..42af2c2f 100644 --- a/src/com/jwetherell/algorithms/search/BinarySearch.java +++ b/src/com/jwetherell/algorithms/search/BinarySearch.java @@ -29,7 +29,8 @@ public static final int find(int value, int[] array, boolean optimize) { BinarySearch.sorted = null; } } - + //Recursively find the element + //@return find the element value by recursively private static int recursiveFind(int value, int start, int end, boolean optimize) { if (start == end) { int lastValue = sorted[start]; // start==end @@ -43,8 +44,10 @@ private static int recursiveFind(int value, int start, int end, boolean optimize final int middle = low + ((high - low) / 2); final int middleValue = sorted[middle]; + //checks if the middle index is element if (value == middleValue) return middle; + //if value is greater than move to right if (value > middleValue) { if (optimize && (end - middle) <= SWITCH_TO_BRUTE_FORCE) return linearSearch(value, middle + 1, end); @@ -54,7 +57,10 @@ private static int recursiveFind(int value, int start, int end, boolean optimize return linearSearch(value, start, middle - 1); return recursiveFind(value, start, middle - 1, optimize); } - + //Linear search to find the element. + //@value the element we want to find. + //@start first index of the array in the array + //@end last index of the array in the array. private static final int linearSearch(int value, int start, int end) { for (int i = start; i <= end; i++) { int iValue = sorted[i]; From 4e8de437ac1e921b319a8ff876e2ff81f1d9c7de Mon Sep 17 00:00:00 2001 From: gauravdarbhanga <57192490+gauravdarbhanga@users.noreply.github.com> Date: Thu, 31 Oct 2019 00:14:19 +0530 Subject: [PATCH 38/41] Update LowerBound.java --- src/com/jwetherell/algorithms/search/LowerBound.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/com/jwetherell/algorithms/search/LowerBound.java b/src/com/jwetherell/algorithms/search/LowerBound.java index 569e4532..9e6cd353 100644 --- a/src/com/jwetherell/algorithms/search/LowerBound.java +++ b/src/com/jwetherell/algorithms/search/LowerBound.java @@ -23,6 +23,7 @@ public static int lowerBound(int[] array, int length, int value) { int high = length; while (low < high) { final int mid = (low + high) / 2; + //checks if the value is less than middle element of the array if (value <= array[mid]) { high = mid; } else { From 769cb67c4364301a91ec82b7381ddb9f12b1a34e Mon Sep 17 00:00:00 2001 From: BryanChan777 <43082778+BryanChan777@users.noreply.github.com> Date: Sat, 23 Nov 2019 17:16:39 -0800 Subject: [PATCH 39/41] Removed hedging and improved grammar in README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 459c4d35..0101b605 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ Java : Algorithms and Data Structure ![alt tag](https://api.travis-ci.org/phishman3579/java-algorithms-implementation.svg?branch=master) ============================== -Algorithms and Data Structures implemented in Java +The algorithms and data structures are implemented in Java. -This is a collection of algorithms and data structures which I've implement over the years in my academic and professional life. The code isn't overly-optimized but is written to be correct and readable. The algorithms and data structures are well tested and, unless noted, are believe to be 100% correct. +This is a collection of algorithms and data structures I've implemented in my academic and professional life. The code isn't optimized but is written to be correct and readable. The algorithms and data structures are tested and, unless noted, believed to be correct. ## Created by Justin Wetherell From 81a028f7404765d5b80b882186cac7d1e3af0205 Mon Sep 17 00:00:00 2001 From: KisaragiEffective <48310258+KisaragiEffective@users.noreply.github.com> Date: Fri, 3 Jan 2020 01:50:13 +0900 Subject: [PATCH 40/41] Update README.md close #105 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 459c4d35..d2a96aaa 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ This is a collection of algorithms and data structures which I've implement over * [Hash Map (associative array)](src/com/jwetherell/algorithms/data_structures/HashMap.java) * [Interval Tree](src/com/jwetherell/algorithms/data_structures/IntervalTree.java) * [Implicit Key Treap](src/com/jwetherell/algorithms/data_structures/ImplicitKeyTreap.java) -* [KD Tree (k-dimensional tree or k-d tree)](src/com/jwetherell/algorithms/data_structures/KDTree.java) +* [KD Tree (k-dimensional tree or k-d tree)](src/com/jwetherell/algorithms/data_structures/KdTree.java) * [List [backed by an array or a linked list]](src/com/jwetherell/algorithms/data_structures/List.java) * [LCP Array (Longest Common Prefix) [backed by a Suffix Array]](src/com/jwetherell/algorithms/data_structures/LCPArray.java) * [Matrix](src/com/jwetherell/algorithms/data_structures/Matrix.java) From 6328fcf45f769b79c34089a4cf4ba50f909fa594 Mon Sep 17 00:00:00 2001 From: Justin Wetherell Date: Wed, 29 Jan 2020 08:17:54 -0500 Subject: [PATCH 41/41] Update .travis.yml --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 40a330e3..3a3703f6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,8 @@ before_script: - echo $JAVA_OPTS - echo $ANT_OPTS +dist: trusty + language: java jdk: