Node.java 21 KB

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