NodePair.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.pact;
  2. import java.util.ArrayList;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.List;
  6. public class NodePair {
  7. Node node1;
  8. Node node2;
  9. public NodePair(Node n1, Node n2) {
  10. this.node1 = n1;
  11. this.node2 = n2;
  12. }
  13. /**
  14. * Gets size of shared node (just length of venn)
  15. * @return size of node
  16. */
  17. int nodesSize()
  18. {
  19. return this.node1.getVenn().length();
  20. }
  21. public String toString() {
  22. return "<" + this.node1 + ", " + this.node2 + ">";
  23. }
  24. HashSet<Node> combineSubTrees() {
  25. List<Node> res = _combineSubTrees();
  26. return new HashSet<>(res);
  27. }
  28. private List<Node> _combineSubTrees() {
  29. System.out.println("combineSubTrees==========================");
  30. System.out.println("node1: " + node1.getVenn());
  31. System.out.println("node2: " + node2.getVenn());
  32. System.out.println("node1Full: " + node1.root().getVenn());
  33. System.out.println("node2Full: " + node2.root().getVenn());
  34. List<Node> commonSiblings = new ArrayList<>();
  35. List<Node> node1Siblings = new ArrayList<>(this.node1.getSiblings());
  36. Iterator it1 = node1Siblings.iterator();
  37. List<Node> node2Siblings = new ArrayList<>(this.node2.getSiblings());
  38. Iterator it2 = node2Siblings.iterator();
  39. StringBuilder siblingsVenn = new StringBuilder();
  40. if (!node1Siblings.isEmpty()) {
  41. siblingsVenn.append(((Node) it2.next()).getVenn());
  42. } else {
  43. if (it1.hasNext()) {
  44. siblingsVenn.append(((Node) it1.next()).getVenn());
  45. if (it1.hasNext()) {
  46. String temp = siblingsVenn.toString();
  47. siblingsVenn = new StringBuilder("(" + temp);
  48. while (it1.hasNext()) {
  49. siblingsVenn.append(((Node) it1.next()).getVenn());
  50. }
  51. siblingsVenn.append(")");
  52. }
  53. commonSiblings.add(new Node(null, siblingsVenn.toString()));
  54. return commonSiblings;
  55. }
  56. return commonSiblings;
  57. }
  58. if (it2.hasNext()) {
  59. String temp = siblingsVenn.toString();
  60. siblingsVenn = new StringBuilder("(" + temp);
  61. while (it2.hasNext()) {
  62. siblingsVenn.append(((Node) it2.next()).getVenn());
  63. }
  64. siblingsVenn.append(")");
  65. }
  66. Node nodesibs1 = new Node(null, siblingsVenn.toString());
  67. if (!it1.hasNext()) {
  68. commonSiblings.add(nodesibs1);
  69. return commonSiblings;
  70. }
  71. StringBuilder siblings2 = new StringBuilder();
  72. if (it1.hasNext()) {
  73. siblings2.append(((Node) it1.next()).getVenn());
  74. if (it1.hasNext()) {
  75. String temp = siblings2.toString();
  76. siblings2 = new StringBuilder("(" + temp);
  77. while (it1.hasNext()) {
  78. siblings2.append(((Node) it1.next()).getVenn());
  79. }
  80. siblings2.append(")");
  81. }
  82. }
  83. Node nodeSibs2 = new Node(null, siblings2.toString());
  84. return new ArrayList<>(Node.combine(nodesibs1, nodeSibs2));
  85. }
  86. HashSet<NodePair> combineNew() {
  87. List<NodePair> setResult = new ArrayList<>();
  88. Node resultNode1 = new Node(null, this.node2.root().getVenn());
  89. String added = "(" + this.node2.getVenn() + this.node1.getVenn() + ")";
  90. for (Node linkNode : node2.linkNodes) {
  91. String result = buildUp(linkNode, added);
  92. Node resultNode2 = new Node(null, result);
  93. setResult.add(new NodePair(resultNode1, resultNode2));
  94. }
  95. return new HashSet<>(setResult);
  96. }
  97. private String buildUp(Node n, String s) {
  98. if (n.parent == null) {
  99. return s;
  100. } else {
  101. StringBuilder result = new StringBuilder();
  102. result.append(s);
  103. for(Node sibling : n.getSiblings()){
  104. result.append(sibling.getVenn());
  105. }
  106. return buildUp(n.parent, "(" + result + ")");
  107. }
  108. }
  109. }