Node.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. package com.pact;
  2. import java.io.PrintStream;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. public class Node
  6. {
  7. Node parent;
  8. String venn;
  9. HashSet<Node> children;
  10. HashSet<Node> linkNodes;
  11. Node linkNode;
  12. public Node(Node p, String s) {
  13. this.linkNode = null;
  14. this.linkNodes = new HashSet<Node>();
  15. this.venn = s;
  16. this.parent = p;
  17. this.children = new HashSet<Node>();
  18. if (s.length() > 1)
  19. setChildren(s);
  20. }
  21. public Node(Node p, char s) {
  22. this(p, ""+s);
  23. }
  24. private void setChildren(String s) {
  25. try {
  26. String vennChildren = s.substring(1, s.length() - 1);
  27. int count = 0;
  28. while (count < vennChildren.length())
  29. if (vennChildren.charAt(count) == '(') {
  30. int start = count;
  31. count++;
  32. int end = 0;
  33. int brackets = 1;
  34. while (true)
  35. {
  36. if (vennChildren.charAt(count) == '(') {
  37. brackets++;
  38. }
  39. if (vennChildren.charAt(count) == ')') {
  40. brackets--;
  41. if (brackets == 0) {
  42. count++;
  43. break;
  44. }
  45. }
  46. count++;
  47. }
  48. end = count;
  49. Node child = new Node(this, vennChildren.substring(start, end));
  50. this.children.add(child);
  51. } else {
  52. Node child = new Node(this, vennChildren.charAt(count));
  53. this.children.add(child);
  54. count++;
  55. }
  56. }
  57. catch (StringIndexOutOfBoundsException se) {
  58. throw new BracketException();
  59. }
  60. }
  61. public String getVenn()
  62. {
  63. return this.venn;
  64. }
  65. public String toString()
  66. {
  67. return this.venn;
  68. }
  69. public HashSet<Node> getSiblings() {
  70. if (this.parent != null) {
  71. HashSet sibs = (HashSet)this.parent.children.clone();
  72. sibs.remove(this);
  73. return sibs;
  74. }
  75. return new HashSet();
  76. }
  77. public boolean equals(Node node)
  78. {
  79. String nodeVenn = node.getVenn();
  80. if ((nodeVenn.length() == 1) && (this.venn.length() == nodeVenn.length())) {
  81. return nodeVenn.equals(this.venn);
  82. }
  83. int itCount = 0;
  84. int eqCount = 0;
  85. for (Node theirChild : node.children) {
  86. itCount++;
  87. for (Node myChild : this.children) {
  88. if (myChild.equals(theirChild)) {
  89. eqCount++;
  90. break;
  91. }
  92. }
  93. }
  94. if (itCount != eqCount) {
  95. return false;
  96. }
  97. itCount = 0;
  98. eqCount = 0;
  99. Iterator it = this.children.iterator();
  100. while (it.hasNext()) {
  101. Iterator nIt = node.children.iterator();
  102. Node toCheck = (Node)it.next();
  103. itCount++;
  104. while (nIt.hasNext()) {
  105. if (toCheck.equals((Node)nIt.next())) {
  106. eqCount++;
  107. break;
  108. }
  109. }
  110. }
  111. return itCount == eqCount;
  112. }
  113. public boolean equals(Object o)
  114. {
  115. Node n = (Node)o;
  116. return equals(n);
  117. }
  118. private int getNumAreas() {
  119. int count = 0;
  120. for (int i = 0; i < this.venn.length(); i++) {
  121. if ((this.venn.charAt(i) != ')') && (this.venn.charAt(i) != '(')) {
  122. count++;
  123. }
  124. }
  125. return count;
  126. }
  127. public HashSet<Node> reducedCombine(Node node) {
  128. HashSet result = new HashSet();
  129. if (oneAreaShared(this, node)) {
  130. result.add(combineAtRoot(node));
  131. return result;
  132. }
  133. HashSet compCombine = combine(node);
  134. Iterator it = compCombine.iterator();
  135. if (!it.hasNext()) {
  136. return result;
  137. }
  138. Node curr = (Node)it.next();
  139. int smallest = curr.getNumAreas();
  140. result.add(curr);
  141. while (it.hasNext()) {
  142. Node curr1 = (Node)it.next();
  143. int currAreas = curr1.getNumAreas();
  144. if (currAreas < smallest) {
  145. smallest = currAreas;
  146. result.clear();
  147. result.add(curr1);
  148. } else if (currAreas == smallest) {
  149. result.add(curr1);
  150. }
  151. }
  152. return removeDuplicates(result);
  153. }
  154. private HashSet<Node> removeDuplicates(HashSet<Node> h) {
  155. Iterator it = h.iterator();
  156. HashSet result = new HashSet();
  157. while (it.hasNext()) {
  158. Node curr = (Node)it.next();
  159. if (!contains(curr, result)) {
  160. result.add(curr);
  161. }
  162. }
  163. return result;
  164. }
  165. private boolean oneAreaShared(Node node1, Node node2)
  166. {
  167. HashSet result = new HashSet();
  168. node2.deepSearch(node1, result);
  169. HashSet largest = getLNodes(result);
  170. if (largest.isEmpty()) {
  171. return false;
  172. }
  173. return (Nodes.setNodesSize(largest) == 1) && (largest.size() == 1);
  174. }
  175. public HashSet<Node> combine(Node node) {
  176. Node node1 = node.reduce();
  177. Node node2 = reduce();
  178. node2.resetSearch();
  179. node1.resetSearch();
  180. HashSet result = new HashSet();
  181. if (node1.equals(node2)) {
  182. result.add(node2);
  183. return result;
  184. }
  185. HashSet sResult = new HashSet();
  186. node2.deepSearch(node1, sResult);
  187. if (sResult.isEmpty()) {
  188. result.add(node2.combineAtRoot(node1));
  189. return result;
  190. }
  191. HashSet largest = getLNodes(sResult);
  192. HashSet unsharedNodes = new HashSet();
  193. node2.collectUnshared(unsharedNodes);
  194. node1.collectUnshared(unsharedNodes);
  195. if ((unsharedNodes.isEmpty()) && (node1.getNumAreas() == node2.getNumAreas())) {
  196. if (node1.children.size() < node2.children.size()) {
  197. Node toAdd = new Node(null, node1.getVenn());
  198. result.add(toAdd);
  199. return result;
  200. }if (node2.children.size() < node1.children.size()) {
  201. Node toAdd = new Node(null, node2.getVenn());
  202. result.add(toAdd);
  203. return result;
  204. }
  205. }
  206. if (largest.size() > 0)
  207. {
  208. Iterator itLargest = largest.iterator();
  209. Nodes flargest = (Nodes)itLargest.next();
  210. HashSet lg1sibs = flargest.node1.getSiblings();
  211. HashSet lg2sibs = flargest.node2.getSiblings();
  212. HashSet newSibs = new HashSet();
  213. HashSet otherSibs1 = new HashSet();
  214. HashSet otherSibs2 = new HashSet();
  215. Iterator lg1It = lg1sibs.iterator();
  216. Iterator lg2It = lg2sibs.iterator();
  217. sortLargestSibs(lg1It, flargest.node2, newSibs, otherSibs1);
  218. sortLargestSibs(lg2It, flargest.node1, newSibs, otherSibs2);
  219. Iterator nsIt = newSibs.iterator();
  220. if ((otherSibs1.isEmpty()) && (otherSibs2.isEmpty())) {
  221. while (nsIt.hasNext()) {
  222. unsharedNodes.add((Node)nsIt.next());
  223. }
  224. }
  225. }
  226. HashSet newLinkedNodes = getLinkedNodes(unsharedNodes);
  227. HashSet polys = reviewPolys(newLinkedNodes);
  228. if (!polys.isEmpty())
  229. {
  230. Iterator polyIt = polys.iterator();
  231. if (polyIt.hasNext()) {
  232. HashSet polyresult = addPolys((HashSet)polyIt.next());
  233. Iterator prIt = polyresult.iterator();
  234. if (prIt.hasNext()) {
  235. Nodes curr = (Nodes)prIt.next();
  236. if ((curr.node1.parent != null) && (curr.node1.linkNode.parent != null)) {
  237. String polyString1 = buildUp(curr.node1.parent, curr.node2.getVenn());
  238. String polyString2 = buildUp(curr.node1.linkNode.parent, curr.node2.getVenn());
  239. Node polyOne = new Node(null, polyString1);
  240. Node polyTwo = new Node(null, polyString2);
  241. HashSet polyresults = polyOne.combine(polyTwo);
  242. Iterator prsIt = polyresults.iterator();
  243. while (prsIt.hasNext())
  244. result.add((Node)prsIt.next());
  245. }
  246. else if (curr.node1.parent != null) {
  247. String polyString1 = buildUp(curr.node1.parent, curr.node2.getVenn());
  248. Node polyOne = new Node(null, polyString1);
  249. result.add(polyOne);
  250. } else if (curr.node1.linkNode.parent != null) {
  251. String polyString2 = buildUp(curr.node1.linkNode.parent, curr.node2.getVenn());
  252. Node polyTwo = new Node(null, polyString2);
  253. result.add(polyTwo);
  254. } else {
  255. System.out.println("What's up Houston?");
  256. }
  257. }
  258. }
  259. return result;
  260. }
  261. Iterator nLNit = newLinkedNodes.iterator();
  262. if (nLNit.hasNext()) {
  263. HashSet combined = ((Nodes)nLNit.next()).combineNew();
  264. Iterator cIt = combined.iterator();
  265. while (cIt.hasNext()) {
  266. Nodes curr = (Nodes)cIt.next();
  267. HashSet newResults = curr.node1.combine(curr.node2);
  268. Iterator nRit = newResults.iterator();
  269. while (nRit.hasNext())
  270. {
  271. result.add((Node)nRit.next());
  272. }
  273. }
  274. return result;
  275. }
  276. if (Nodes.setNodesSize(largest) == 1)
  277. {
  278. HashSet unique = getUniqueNodes(sResult);
  279. Iterator uIt = unique.iterator();
  280. while (uIt.hasNext()) {
  281. Node curr = (Node)uIt.next();
  282. HashSet dupres = curr.getDuplicates(sResult);
  283. if (dupres.size() > 1) {
  284. HashSet newDupNodes = getLinkedNodes(dupres);
  285. Iterator ndIt = newDupNodes.iterator();
  286. while (ndIt.hasNext()) {
  287. HashSet combined = ((Nodes)ndIt.next()).combineNew();
  288. Iterator cIt = combined.iterator();
  289. while (cIt.hasNext()) {
  290. Nodes curr2 = (Nodes)cIt.next();
  291. HashSet newResults = curr2.node1.combine(curr2.node2);
  292. Iterator nRit = newResults.iterator();
  293. while (nRit.hasNext())
  294. {
  295. result.add((Node)nRit.next());
  296. }
  297. }
  298. }
  299. }
  300. else {
  301. result.add(node2.combineAtRoot(node1));
  302. }
  303. }
  304. return result;
  305. }
  306. if (Nodes.setNodesSize(largest) > 1)
  307. {
  308. for (Object aLargest : largest) {
  309. Nodes currL = (Nodes) aLargest;
  310. HashSet largeResult = subTrees(currL);
  311. Iterator lrIt = largeResult.iterator();
  312. while (lrIt.hasNext()) {
  313. result.add((Node) lrIt.next());
  314. }
  315. }
  316. return result;
  317. }
  318. return result;
  319. }
  320. private void sortLargestSibs(Iterator<Node> lgsibs, Node lg, HashSet<Node> ns, HashSet<Node> os) {
  321. while (lgsibs.hasNext()) {
  322. Node curr = (Node)lgsibs.next();
  323. Iterator linkIt = curr.linkNodes.iterator();
  324. boolean linkFlargest = false;
  325. if (linkIt.hasNext()) {
  326. linkFlargest = true;
  327. }
  328. while (linkIt.hasNext()) {
  329. Node currLink = (Node)linkIt.next();
  330. if (!lg.isIn(currLink)) {
  331. linkFlargest = false;
  332. }
  333. }
  334. if (linkFlargest) {
  335. ns.add(curr);
  336. }
  337. else if (!curr.linkNodes.isEmpty())
  338. os.add(curr);
  339. }
  340. }
  341. public HashSet<Node> subTrees(Nodes largest)
  342. {
  343. HashSet<Node> result = new HashSet();
  344. HashSet<Node> n = largest.combineSubTrees();
  345. for (Node aN : n) {
  346. String curr = aN.getVenn();
  347. if ((curr.length() > 1) && (
  348. (largest.node1.getSiblings().size() != 1) ||
  349. (largest.node2.getSiblings().size() != 1))) {
  350. curr = curr.substring(1, curr.length() - 1);
  351. }
  352. String curr1 = "(" + largest.node1.getVenn() + curr + ")";
  353. String curr2 = "(" + largest.node2.getVenn() + curr + ")";
  354. if ((largest.node1.parent != null) &&
  355. (largest.node2.parent != null)) {
  356. String one = buildUp(largest.node1.parent, curr1);
  357. String two = buildUp(largest.node2.parent, curr2);
  358. Node newone = new Node(null, one);
  359. Node newtwo = new Node(null, two);
  360. HashSet newresult = newone.combine(newtwo);
  361. Iterator newit = newresult.iterator();
  362. while (newit.hasNext()) {
  363. result.add((Node) newit.next());
  364. }
  365. return result;
  366. }
  367. if (largest.node1.parent != null) {
  368. String one = buildUp(largest.node1.parent, curr1);
  369. Node newone = new Node(null, one);
  370. result.add(newone);
  371. return result;
  372. }
  373. if (largest.node2.parent != null) {
  374. String two = buildUp(largest.node2.parent, curr2);
  375. Node newtwo = new Node(null, two);
  376. result.add(newtwo);
  377. return result;
  378. }
  379. System.out.println("Houston we have a problem.");
  380. }
  381. return result;
  382. }
  383. private void search(Node node, HashSet<Nodes> h)
  384. {
  385. if (equals(node)) {
  386. this.linkNodes.add(node);
  387. node.linkNodes.add(this);
  388. h.add(new Nodes(this, node));
  389. } else {
  390. Iterator it = this.children.iterator();
  391. while (it.hasNext())
  392. ((Node)(Node)it.next()).search(node, h);
  393. }
  394. }
  395. private HashSet<Nodes> searchBothNodes(Node node)
  396. {
  397. HashSet<Nodes> sResult = new HashSet();
  398. search(node, sResult);
  399. HashSet<Nodes> nsResult = new HashSet();
  400. node.search(this, nsResult);
  401. for (Nodes curr : nsResult) {
  402. boolean inSet = inSet(curr, sResult);
  403. if (!inSet) {
  404. sResult.add(curr);
  405. }
  406. }
  407. return sResult;
  408. }
  409. public void deepSearch(Node node, HashSet<Nodes> h)
  410. {
  411. HashSet<Nodes> first_result = searchBothNodes(node);
  412. for(Nodes res : first_result) {
  413. h.add(res);
  414. }
  415. for(Node child : node.children) {
  416. deepSearch(child, h);
  417. }
  418. }
  419. private boolean inSet(Nodes n, Iterable<Nodes> it)
  420. {
  421. for (Nodes nodes : it) {
  422. if (n.equals(nodes)) {
  423. return true;
  424. }
  425. }
  426. return false;
  427. }
  428. private void resetSearch() {
  429. this.linkNodes = new HashSet<Node>();
  430. this.linkNode = null;
  431. for (Node child : this.children) {
  432. child.resetSearch();
  433. }
  434. }
  435. private HashSet<Nodes> getLNodes(HashSet<Nodes> h)
  436. {
  437. HashSet result = new HashSet();
  438. int size = 0;
  439. Iterator it = h.iterator();
  440. if (it.hasNext()) {
  441. Nodes curr = (Nodes)it.next();
  442. size = curr.nodesSize();
  443. result.add(curr);
  444. }
  445. while (it.hasNext()) {
  446. Nodes curr = (Nodes)it.next();
  447. if (size < curr.nodesSize()) {
  448. size = curr.nodesSize();
  449. result.clear();
  450. result.add(curr);
  451. } else if (size == curr.nodesSize()) {
  452. result.add(curr);
  453. }
  454. }
  455. return result;
  456. }
  457. private Node combineAtRoot(Node node)
  458. {
  459. HashSet uniqueNodes = new HashSet();
  460. Iterator thisIt = this.children.iterator();
  461. Iterator nodeIt = node.children.iterator();
  462. boolean reduced = false;
  463. while (thisIt.hasNext()) {
  464. Node curr = (Node)thisIt.next();
  465. if (!contains(curr, uniqueNodes))
  466. uniqueNodes.add(curr);
  467. else {
  468. reduced = true;
  469. }
  470. }
  471. while (nodeIt.hasNext()) {
  472. Node curr = (Node)nodeIt.next();
  473. if (!contains(curr, uniqueNodes))
  474. uniqueNodes.add(curr);
  475. else {
  476. reduced = true;
  477. }
  478. }
  479. Iterator unIt = uniqueNodes.iterator();
  480. String result = "(";
  481. while (unIt.hasNext()) {
  482. result = result + unIt.next();
  483. }
  484. result = result + ")";
  485. if (!reduced) {
  486. return new Node(null, "(" + this.venn + node.getVenn() + ")");
  487. }
  488. return new Node(null, result);
  489. }
  490. private boolean unshared()
  491. {
  492. if (this.linkNodes.isEmpty())
  493. {
  494. Iterator it = this.children.iterator();
  495. if (!it.hasNext()) {
  496. return true;
  497. }
  498. while (it.hasNext()) {
  499. if (!((Node)it.next()).unshared()) {
  500. return false;
  501. }
  502. }
  503. return true;
  504. }
  505. return false;
  506. }
  507. private void collectUnshared(HashSet<Node> h) {
  508. if (unshared()) {
  509. h.add(this);
  510. }
  511. Iterator it = this.children.iterator();
  512. while (it.hasNext())
  513. ((Node)it.next()).collectUnshared(h);
  514. }
  515. private HashSet<Nodes> getLinkedNodes(HashSet<Node> h)
  516. {
  517. HashSet result = new HashSet();
  518. Iterator hit = h.iterator();
  519. while (hit.hasNext()) {
  520. Node curr = (Node)hit.next();
  521. if (curr.hasLink() != null)
  522. result.add(new Nodes(curr, curr.hasLink()));
  523. }
  524. return result;
  525. }
  526. private Node hasLink() {
  527. HashSet sibs = getSiblings();
  528. Iterator it = sibs.iterator();
  529. while (it.hasNext()) {
  530. Node curr = (Node)it.next();
  531. if (!curr.linkNodes.isEmpty()) {
  532. return curr;
  533. }
  534. }
  535. return null;
  536. }
  537. public Node root() {
  538. if (getSiblings().isEmpty()) {
  539. return this;
  540. }
  541. return this.parent.root();
  542. }
  543. private String buildUp(Node n, String s) {
  544. if (n.parent == null) {
  545. return s;
  546. }
  547. String result = "(" + s;
  548. if (n.parent != null) {
  549. HashSet sibs = n.getSiblings();
  550. Iterator it = sibs.iterator();
  551. while (it.hasNext()) {
  552. result = result + ((Node)it.next()).getVenn();
  553. }
  554. }
  555. result = result + ")";
  556. return buildUp(n.parent, result);
  557. }
  558. private HashSet<HashSet<Nodes>> reviewPolys(HashSet<Nodes> h)
  559. {
  560. HashSet result = new HashSet();
  561. Iterator it = h.iterator();
  562. while (it.hasNext()) {
  563. HashSet curresult = new HashSet();
  564. Nodes curr = (Nodes)it.next();
  565. Iterator currIt = h.iterator();
  566. while (currIt.hasNext()) {
  567. Nodes inCurr = (Nodes)currIt.next();
  568. if (inCurr == curr) {
  569. continue;
  570. }
  571. HashSet linkedNodes = inCurr.node2.linkNodes;
  572. Iterator lnIt = linkedNodes.iterator();
  573. boolean found = false;
  574. while (lnIt.hasNext()) {
  575. Node currln = (Node)lnIt.next();
  576. if (currln == curr.node2) {
  577. found = true;
  578. inCurr.node2.linkNode = currln;
  579. break;
  580. }
  581. }
  582. if (found) {
  583. curresult.add(inCurr);
  584. }
  585. }
  586. if (!curresult.isEmpty()) {
  587. curresult.add(curr);
  588. result.add(curresult);
  589. }
  590. }
  591. return result;
  592. }
  593. private HashSet<Nodes> addPolys(HashSet<Nodes> h) {
  594. HashSet setresult = new HashSet();
  595. Iterator it = h.iterator();
  596. Nodes first = (Nodes)it.next();
  597. String result = first.node2.getVenn();
  598. result = result + first.node1.getVenn();
  599. while (it.hasNext()) {
  600. result = result + ((Nodes)it.next()).node1.getVenn();
  601. }
  602. Iterator itr = h.iterator();
  603. while (itr.hasNext()) {
  604. Nodes toAdd = new Nodes(((Nodes)itr.next()).node2, new Node(null, "(" + result + ")"));
  605. setresult.add(toAdd);
  606. }
  607. return setresult;
  608. }
  609. private boolean isIn(Node node) {
  610. if (this == node) {
  611. return true;
  612. }
  613. Iterator it = this.children.iterator();
  614. while (it.hasNext()) {
  615. if (((Node)it.next()).isIn(node)) {
  616. return true;
  617. }
  618. }
  619. return false;
  620. }
  621. private HashSet<Node> getUniqueNodes(HashSet<Nodes> h)
  622. {
  623. HashSet result = new HashSet();
  624. Iterator it = h.iterator();
  625. while (it.hasNext()) {
  626. Nodes curr = (Nodes)it.next();
  627. if (!result.contains(curr.node1)) {
  628. result.add(curr.node1);
  629. }
  630. if (!result.contains(curr.node2)) {
  631. result.add(curr.node2);
  632. }
  633. }
  634. return result;
  635. }
  636. private HashSet<Node> getDuplicates(HashSet<Nodes> h) {
  637. HashSet result = new HashSet();
  638. Iterator it = h.iterator();
  639. while (it.hasNext()) {
  640. Nodes curr = (Nodes)it.next();
  641. if (curr.node1 == this)
  642. result.add(curr.node2);
  643. else if (curr.node2 == this) {
  644. result.add(curr.node1);
  645. }
  646. }
  647. return result;
  648. }
  649. protected Node reduce()
  650. {
  651. HashSet c = this.children;
  652. HashSet uniqueC = new HashSet();
  653. HashSet dupC = new HashSet();
  654. Iterator it = c.iterator();
  655. while (it.hasNext()) {
  656. Node curr = (Node)it.next();
  657. if (contains(curr, uniqueC))
  658. dupC.add(curr);
  659. else {
  660. uniqueC.add(curr);
  661. }
  662. }
  663. if ((dupC.isEmpty()) || (this.venn.length() == 1)) {
  664. return this;
  665. }
  666. Iterator uIt = uniqueC.iterator();
  667. String result = "";
  668. if (uIt.hasNext()) {
  669. result = result + ((Node)uIt.next()).getVenn();
  670. }
  671. if (uIt.hasNext()) {
  672. result = result + "(" + result;
  673. while (uIt.hasNext()) {
  674. result = result + ((Node)uIt.next()).getVenn();
  675. }
  676. result = result + ")";
  677. }
  678. return new Node(null, result);
  679. }
  680. private boolean contains(Node n, HashSet<Node> h)
  681. {
  682. Iterator it = h.iterator();
  683. while (it.hasNext()) {
  684. Node curr = (Node)it.next();
  685. if (curr.equals(n)) {
  686. return true;
  687. }
  688. }
  689. return false;
  690. }
  691. }