瀏覽代碼

Fixes bug in reduce

Caleb Fangmeier 6 年之前
父節點
當前提交
503209b984
共有 3 個文件被更改,包括 96 次插入76 次删除
  1. 72 48
      src/com/pact/Node.java
  2. 18 27
      src/com/pact/NodePair.java
  3. 6 1
      src/com/pact/test/NodeTest.java

+ 72 - 48
src/com/pact/Node.java

@@ -94,6 +94,15 @@ public class Node {
         return new HashSet<>();
     }
 
+    List<Node> getSiblingsL() {
+        if (this.parent != null) {
+            List<Node> sibs = new ArrayList<>(parent.children);
+            sibs.remove(this);
+            return sibs;
+        }
+        return new ArrayList<>();
+    }
+
     public boolean equals(Object obj) {
         if (this == obj) return true;  // Literally the same object
         if (!(obj instanceof Node)) return false; // Wrong type
@@ -128,19 +137,24 @@ public class Node {
         return true;
     }
 
-    public List<NodePair> deepSearch(Node node) {
+//    @Override
+//    public int hashCode(){
+//        return hash;
+//    }
+
+    public static List<NodePair> deepSearch(Node nodeA, Node nodeB) {
         List<NodePair> result = new ArrayList<>();
 
         Stack<Node> searchNodes = new Stack<>();
-        searchNodes.push(node);
+        searchNodes.push(nodeB);
         while (!searchNodes.isEmpty()) {
             Node curr = searchNodes.pop();
 
             // res1: Occurrences of node in this tree
-            List<NodePair> res1 = this.search(curr);
+            List<NodePair> res1 = nodeA.search(curr);
 
             // res2: Occurrences of this tree in node
-            List<NodePair> res2 = curr.search(this);
+            List<NodePair> res2 = curr.search(nodeA);
 
             assert(res1.size() == 0 || res2.size() == 0 || res1.equals(res2));
 
@@ -155,7 +169,7 @@ public class Node {
         return result;
     }
 
-    public Node root() {
+    Node root() {
         if (getSiblings().isEmpty()) {
             return this;
         }
@@ -194,6 +208,11 @@ public class Node {
         return result;
     }
 
+    /**
+     * Searches in this tree for the presence of the Node node.
+     * @param node
+     * @return
+     */
     private boolean isIn(Node node) {
         if (this == node) {
             return true;
@@ -254,32 +273,32 @@ public class Node {
         return result;
     }
 
-    private Node reduce() {
-        List<Node> uniqueC = new ArrayList<>();
-        List<Node> dupC = new ArrayList<>();
-        for (Node child : this.children) {
-            if (child.inSet(uniqueC))
-                dupC.add(child);
-            else {
-                uniqueC.add(child);
+    /**
+     * Produces a reduced version of unreduced where any duplicate children have been removed. Note that this doesn't
+     * look for duplicate sub-trees at lower levels. (ie duplicate grandchildren)
+     * @param unreduced
+     * @return
+     */
+    private static Node reduce(Node unreduced) {
+        List<Node> uniqueChildren = new ArrayList<>();
+        for (Node child : unreduced.children) {
+            if (!child.inSet(uniqueChildren)) {
+                uniqueChildren.add(child);
             }
         }
-        if ((dupC.isEmpty()) || (this.venn.length() == 1)) {
-            return this;
-        }
-        Iterator uIt = uniqueC.iterator();
-        String result = "";
-        if (uIt.hasNext()) {
-            result = result + ((Node)uIt.next()).getVenn();
+        if (uniqueChildren.size() == unreduced.children.size() || (unreduced.venn.length() == 1)) {
+            return unreduced;
         }
+        Iterator<Node> uIt = uniqueChildren.iterator();
+        String result = uIt.next().getVenn();
         if (uIt.hasNext()) {
-            result = result + "(" + result;
             while (uIt.hasNext()) {
-                result = result + ((Node)uIt.next()).getVenn();
+                result += uIt.next().getVenn();
             }
-            result = result + ")";
+            result = "(" + result + ")";
         }
-        return new Node(null, result);
+        Node reduced = new Node(null, result);
+        return reduced;
     }
 
     private boolean inSet(List<Node> h) {
@@ -309,9 +328,10 @@ public class Node {
         }
     }
 
-    public static HashSet<Node> combine(Node node1, Node node2) {
-        node1 = node1.reduce();
-        node2 = node2.reduce();
+    static HashSet<Node> combine(Node node1, Node node2) {
+        System.out.println("Combining: " + node1 + ", " + node2);
+        node1 = reduce(node1);
+        node2 = reduce(node2);
 
         node2.resetSearch();
         node1.resetSearch();
@@ -322,7 +342,7 @@ public class Node {
             return result;
         }
 
-        List<NodePair> sResult = node2.deepSearch(node1);
+        List<NodePair> sResult = deepSearch(node2, node1);
 
         if (sResult.isEmpty()) {
             result.add(combineAtRoot(node2, node1));
@@ -422,17 +442,15 @@ public class Node {
                     result.add(nRit.next());
                 }
             }
-
             return result;
         }
 
         if (largest.get(0).nodesSize() == 1) { // Shares only leaves
-            HashSet unique = getUniqueNodePair(sResult);
-            Iterator uIt = unique.iterator();
+            List<Node> unique = getUniqueNodes(sResult);
+//            HashSet<Node> unique = getUniqueNodes(sResult);
 
-            while (uIt.hasNext()) {
-                Node curr = (Node)uIt.next();
-                HashSet dupres = curr.getDuplicates(sResult);
+            for (Node curr : unique) {
+                HashSet<Node> dupres = curr.getDuplicates(sResult);
                 if (dupres.size() > 1) {
                     HashSet newDupNodePair = getLinkedNodePair(dupres);
                     Iterator ndIt = newDupNodePair.iterator();
@@ -498,11 +516,11 @@ public class Node {
                 result.add(curr);
             }
         }
-        return new ArrayList(result);
+        return new ArrayList<>(result);
     }
 
     private static boolean oneAreaShared(Node node1, Node node2) {
-        List<NodePair> result = node2.deepSearch(node1);
+        List<NodePair> result = deepSearch(node2, node1);
         if (result.isEmpty()) {
             return false;
         }
@@ -597,8 +615,8 @@ public class Node {
         return result;
     }
 
-    private static Node combineAtRoot(Node node1, Node node2) {
-        HashSet<Node> uniqueNodes = new HashSet<>();
+    private static Node combineAtRoot(Node node1, Node node2) { // CONVERTED
+        List<Node> uniqueNodes = new ArrayList<>();
         for(Node curr : node1.children) {
             if (!curr.inSet(uniqueNodes))
                 uniqueNodes.add(curr);
@@ -628,18 +646,17 @@ public class Node {
         return result;
     }
 
-    private static String buildUp(Node n, String s) {
+    private static String buildUp(Node n, String s) { // CONVERTED
+        System.out.println("buildUp: " + s);
         if (n.parent == null) {
             return s;
         }
         String result = "(" + s;
-        if (n.parent != null) {
-            HashSet<Node> sibs = n.getSiblings();
-            for (Node sib : sibs) {
-                result = result + sib.getVenn();
-            }
+        List<Node> sibs = n.getSiblingsL();
+        for (Node sib : sibs) {
+            result += sib.getVenn();
         }
-        result = result + ")";
+        result += ")";
         return buildUp(n.parent, result);
     }
 
@@ -690,9 +707,9 @@ public class Node {
         return setResult;
     }
 
-    private static HashSet<Node> getUniqueNodePair(List<NodePair> h) {
+    private static List<Node> getUniqueNodes(List<NodePair> nodePairs) { // CONVERTED
         HashSet<Node> result = new HashSet<>();
-        for (NodePair curr : h) {
+        for (NodePair curr : nodePairs) {
             if (!result.contains(curr.node1)) {
                 result.add(curr.node1);
             }
@@ -700,6 +717,13 @@ public class Node {
                 result.add(curr.node2);
             }
         }
-        return result;
+        System.out.println("result: " + result);
+        List<Node> result_list = new ArrayList<>();
+        for (NodePair curr : nodePairs) {
+            result_list.add(curr.node1);
+            result_list.add(curr.node2);
+        }
+        System.out.println("result_list: " + result_list);
+        return result_list;
     }
 }

+ 18 - 27
src/com/pact/NodePair.java

@@ -14,15 +14,11 @@ public class NodePair {
         this.node2 = n2;
     }
 
-    public static int getNodesSize(List<NodePair> node_pairs) {
-        return node_pairs.get(0).nodesSize();
-    }
-
     /**
-     * Gets size of
+     * Gets size of shared node (just length of venn)
      * @return size of node
      */
-    public int nodesSize()
+    int nodesSize()
     {
         return this.node1.getVenn().length();
     }
@@ -31,19 +27,13 @@ public class NodePair {
         return "<" + this.node1 + ", " + this.node2 + ">";
     }
 
-    public boolean equals(Object o) {
-        NodePair input = (NodePair)o;
-        return ((input.node1 == this.node1) && (input.node2 == this.node2)) || (
-                (input.node1 == this.node2) && (input.node2 == this.node1));
-    }
-
-    public HashSet<Node> combineSubTrees() {
+    HashSet<Node> combineSubTrees() {
         List<Node> res = _combineSubTrees();
-        return new HashSet(res);
+        return new HashSet<>(res);
 
     }
 
-    public List<Node> _combineSubTrees() {
+    private List<Node> _combineSubTrees() {
         System.out.println("combineSubTrees==========================");
         System.out.println("node1: " + node1.getVenn());
         System.out.println("node2: " + node2.getVenn());
@@ -91,24 +81,24 @@ public class NodePair {
             commonSiblings.add(nodesibs1);
             return commonSiblings;
         }
-        String siblings2 = "";
+        StringBuilder siblings2 = new StringBuilder();
         if (it1.hasNext()) {
-            siblings2 = siblings2 + ((Node)it1.next()).getVenn();
+            siblings2.append(((Node) it1.next()).getVenn());
             if (it1.hasNext()) {
-                String temp = siblings2;
-                siblings2 = "(" + temp;
+                String temp = siblings2.toString();
+                siblings2 = new StringBuilder("(" + temp);
                 while (it1.hasNext()) {
-                    siblings2 = siblings2 + ((Node)it1.next()).getVenn();
+                    siblings2.append(((Node) it1.next()).getVenn());
                 }
-                siblings2 = siblings2 + ")";
+                siblings2.append(")");
             }
         }
 
-        Node nodeSibs2 = new Node(null, siblings2);
-        return new ArrayList(Node.combine(nodesibs1, nodeSibs2));
+        Node nodeSibs2 = new Node(null, siblings2.toString());
+        return new ArrayList<>(Node.combine(nodesibs1, nodeSibs2));
     }
 
-    protected HashSet<NodePair> combineNew() {
+    HashSet<NodePair> combineNew() {
         List<NodePair> setResult = new ArrayList<>();
         Node resultNode1 = new Node(null, this.node2.root().getVenn());
         String added = "(" + this.node2.getVenn() + this.node1.getVenn() + ")";
@@ -118,16 +108,17 @@ public class NodePair {
             Node resultNode2 = new Node(null, result);
             setResult.add(new NodePair(resultNode1, resultNode2));
         }
-        return new HashSet(setResult);
+        return new HashSet<>(setResult);
     }
 
     private String buildUp(Node n, String s) {
         if (n.parent == null) {
             return s;
         } else {
-            String result = s;
+            StringBuilder result = new StringBuilder();
+            result.append(s);
             for(Node sibling : n.getSiblings()){
-                result +=  sibling.getVenn();
+                result.append(sibling.getVenn());
             }
             return buildUp(n.parent, "(" + result + ")");
         }

+ 6 - 1
src/com/pact/test/NodeTest.java

@@ -43,7 +43,7 @@ class NodeTest {
     void searchTest1() {
         Node node1 = new Node(null, "(A(B(C(DA))))");
         Node node2 = new Node(null, "(A(B(CD)))");
-        List<NodePair> result = node2.deepSearch(node1);
+        List<NodePair> result = Node.deepSearch(node2, node1);
         System.out.println(result);
     }
 
@@ -177,5 +177,10 @@ class NodeTest {
                            "(((TH)(IS))(((E(AL))R)T))");
     }
 
+    @Test
+    void reducedCombineTest19() {
+        reducedCombineTest("((AB)(AB)(AC))", "((AB)(AB)(AB))",
+                           "((AB)(AC))");
+    }
 
 }