|
@@ -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;
|
|
|
}
|
|
|
}
|