123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package com.pact;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.List;
- public class NodePair {
- Node node1;
- Node node2;
- public NodePair(Node n1, Node n2) {
- this.node1 = n1;
- this.node2 = n2;
- }
- /**
- * Gets size of shared node (just length of venn)
- * @return size of node
- */
- int nodesSize()
- {
- return this.node1.getVenn().length();
- }
- public String toString() {
- return "<" + this.node1 + ", " + this.node2 + ">";
- }
- HashSet<Node> combineSubTrees() {
- List<Node> res = _combineSubTrees();
- return new HashSet<>(res);
- }
- private List<Node> _combineSubTrees() {
- System.out.println("combineSubTrees==========================");
- System.out.println("node1: " + node1.getVenn());
- System.out.println("node2: " + node2.getVenn());
- System.out.println("node1Full: " + node1.root().getVenn());
- System.out.println("node2Full: " + node2.root().getVenn());
- List<Node> commonSiblings = new ArrayList<>();
- List<Node> node1Siblings = new ArrayList<>(this.node1.getSiblings());
- Iterator it1 = node1Siblings.iterator();
- List<Node> node2Siblings = new ArrayList<>(this.node2.getSiblings());
- Iterator it2 = node2Siblings.iterator();
- StringBuilder siblingsVenn = new StringBuilder();
- if (!node1Siblings.isEmpty()) {
- siblingsVenn.append(((Node) it2.next()).getVenn());
- } else {
- if (it1.hasNext()) {
- siblingsVenn.append(((Node) it1.next()).getVenn());
- if (it1.hasNext()) {
- String temp = siblingsVenn.toString();
- siblingsVenn = new StringBuilder("(" + temp);
- while (it1.hasNext()) {
- siblingsVenn.append(((Node) it1.next()).getVenn());
- }
- siblingsVenn.append(")");
- }
- commonSiblings.add(new Node(null, siblingsVenn.toString()));
- return commonSiblings;
- }
- return commonSiblings;
- }
- if (it2.hasNext()) {
- String temp = siblingsVenn.toString();
- siblingsVenn = new StringBuilder("(" + temp);
- while (it2.hasNext()) {
- siblingsVenn.append(((Node) it2.next()).getVenn());
- }
- siblingsVenn.append(")");
- }
- Node nodesibs1 = new Node(null, siblingsVenn.toString());
- if (!it1.hasNext()) {
- commonSiblings.add(nodesibs1);
- return commonSiblings;
- }
- StringBuilder siblings2 = new StringBuilder();
- if (it1.hasNext()) {
- siblings2.append(((Node) it1.next()).getVenn());
- if (it1.hasNext()) {
- String temp = siblings2.toString();
- siblings2 = new StringBuilder("(" + temp);
- while (it1.hasNext()) {
- siblings2.append(((Node) it1.next()).getVenn());
- }
- siblings2.append(")");
- }
- }
- Node nodeSibs2 = new Node(null, siblings2.toString());
- return new ArrayList<>(Node.combine(nodesibs1, nodeSibs2));
- }
- 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() + ")";
- for (Node linkNode : node2.linkNodes) {
- String result = buildUp(linkNode, added);
- Node resultNode2 = new Node(null, result);
- setResult.add(new NodePair(resultNode1, resultNode2));
- }
- return new HashSet<>(setResult);
- }
- private String buildUp(Node n, String s) {
- if (n.parent == null) {
- return s;
- } else {
- StringBuilder result = new StringBuilder();
- result.append(s);
- for(Node sibling : n.getSiblings()){
- result.append(sibling.getVenn());
- }
- return buildUp(n.parent, "(" + result + ")");
- }
- }
- }
|