Nodes.java 3.5 KB

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