Nodes.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.pact;
  2. import java.util.HashSet;
  3. import java.util.Iterator;
  4. public class Nodes
  5. {
  6. Node node1;
  7. Node node2;
  8. public Nodes(Node n1, Node n2)
  9. {
  10. this.node1 = n1;
  11. this.node2 = n2;
  12. }
  13. public static int setNodesSize(HashSet<Nodes> h)
  14. {
  15. Iterator it = h.iterator();
  16. return ((Nodes)it.next()).nodesSize();
  17. }
  18. public int nodesSize()
  19. {
  20. return this.node1.getVenn().length();
  21. }
  22. public String toString() {
  23. return "<" + this.node1 + ", " + this.node2 + ">";
  24. }
  25. public boolean equals(Object o) {
  26. Nodes input = (Nodes)o;
  27. return ((input.node1 == this.node1) && (input.node2 == this.node2)) || (
  28. (input.node1 == this.node2) && (input.node2 == this.node1));
  29. }
  30. public HashSet<Node> combineSubTrees()
  31. {
  32. HashSet comsibs = new HashSet();
  33. HashSet n1sibs = this.node1.getSiblings();
  34. Iterator it2 = n1sibs.iterator();
  35. HashSet n2sibs = this.node2.getSiblings();
  36. Iterator it1 = n2sibs.iterator();
  37. String sibs1 = "";
  38. if (it2.hasNext()) {
  39. sibs1 = sibs1 + ((Node)it2.next()).getVenn();
  40. }
  41. else
  42. {
  43. if (it1.hasNext()) {
  44. sibs1 = sibs1 + ((Node)it1.next()).getVenn();
  45. if (it1.hasNext()) {
  46. String temp = sibs1;
  47. sibs1 = "(" + temp;
  48. while (it1.hasNext()) {
  49. sibs1 = sibs1 + ((Node)it1.next()).getVenn();
  50. }
  51. sibs1 = sibs1 + ")";
  52. }
  53. comsibs.add(new Node(null, sibs1));
  54. return comsibs;
  55. }
  56. return comsibs;
  57. }
  58. if (it2.hasNext())
  59. {
  60. String temp = sibs1;
  61. sibs1 = "(" + temp;
  62. while (it2.hasNext()) {
  63. sibs1 = sibs1 + ((Node)it2.next()).getVenn();
  64. }
  65. sibs1 = sibs1 + ")";
  66. }
  67. Node nodesibs1 = new Node(null, sibs1);
  68. if (!it1.hasNext()) {
  69. comsibs.add(nodesibs1);
  70. return comsibs;
  71. }
  72. String sibs2 = "";
  73. if (it1.hasNext()) {
  74. sibs2 = sibs2 + ((Node)it1.next()).getVenn();
  75. if (it1.hasNext()) {
  76. String temp = sibs2;
  77. sibs2 = "(" + temp;
  78. while (it1.hasNext()) {
  79. sibs2 = sibs2 + ((Node)it1.next()).getVenn();
  80. }
  81. sibs2 = sibs2 + ")";
  82. }
  83. }
  84. Node nodesibs2 = new Node(null, sibs2);
  85. return nodesibs1.combine(nodesibs2);
  86. }
  87. private boolean inSet(Node n, Iterator<Node> it)
  88. {
  89. boolean inSet = false;
  90. while (it.hasNext()) {
  91. if (n.equals((Node)it.next())) {
  92. inSet = true;
  93. break;
  94. }
  95. }
  96. return inSet;
  97. }
  98. protected HashSet<Nodes> combineNew()
  99. {
  100. HashSet setResult = new HashSet();
  101. Node rnode1 = new Node(null, this.node2.root().getVenn());
  102. String added = "(" + this.node2.getVenn() + this.node1.getVenn() + ")";
  103. HashSet ln = this.node2.linkNodes;
  104. Iterator lnIt = ln.iterator();
  105. while (lnIt.hasNext()) {
  106. String result = buildUp((Node)lnIt.next(), added);
  107. Node rnode2 = new Node(null, result);
  108. setResult.add(new Nodes(rnode1, rnode2));
  109. }
  110. return setResult;
  111. }
  112. private String buildUp(Node n, String s)
  113. {
  114. if (n.parent == null) {
  115. return s;
  116. }
  117. String result = s;
  118. if (n.parent != null) {
  119. HashSet sibs = n.getSiblings();
  120. Iterator it = sibs.iterator();
  121. if (it.hasNext()) {
  122. String temp = "(" + result;
  123. result = temp;
  124. while (it.hasNext()) {
  125. result = result + ((Node)it.next()).getVenn();
  126. }
  127. result = result + ")";
  128. }
  129. }
  130. return buildUp(n.parent, result);
  131. }
  132. }