123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- import java.util.HashSet;
- import java.util.Iterator;
- public class Nodes
- {
- Node node1;
- Node node2;
- public Nodes(Node n1, Node n2)
- {
- this.node1 = n1;
- this.node2 = n2;
- }
- public static int setNodesSize(HashSet<Nodes> h)
- {
- Iterator it = h.iterator();
- return ((Nodes)it.next()).nodesSize();
- }
- public int nodesSize()
- {
- return this.node1.getVenn().length();
- }
- public String toString() {
- return "<" + this.node1 + ", " + this.node2 + ">";
- }
- public boolean equals(Object o) {
- Nodes input = (Nodes)o;
- return ((input.node1 == this.node1) && (input.node2 == this.node2)) || (
- (input.node1 == this.node2) && (input.node2 == this.node1));
- }
- public HashSet<Node> combineSubTrees()
- {
- HashSet comsibs = new HashSet();
- HashSet n1sibs = this.node1.getSiblings();
- Iterator it2 = n1sibs.iterator();
- HashSet n2sibs = this.node2.getSiblings();
- Iterator it1 = n2sibs.iterator();
- String sibs1 = "";
- if (it2.hasNext()) {
- sibs1 = sibs1 + ((Node)it2.next()).getVenn();
- }
- else
- {
- if (it1.hasNext()) {
- sibs1 = sibs1 + ((Node)it1.next()).getVenn();
- if (it1.hasNext()) {
- String temp = sibs1;
- sibs1 = "(" + temp;
- while (it1.hasNext()) {
- sibs1 = sibs1 + ((Node)it1.next()).getVenn();
- }
- sibs1 = sibs1 + ")";
- }
- comsibs.add(new Node(null, sibs1));
- return comsibs;
- }
- return comsibs;
- }
- if (it2.hasNext())
- {
- String temp = sibs1;
- sibs1 = "(" + temp;
- while (it2.hasNext()) {
- sibs1 = sibs1 + ((Node)it2.next()).getVenn();
- }
- sibs1 = sibs1 + ")";
- }
- Node nodesibs1 = new Node(null, sibs1);
- if (!it1.hasNext()) {
- comsibs.add(nodesibs1);
- return comsibs;
- }
- String sibs2 = "";
- if (it1.hasNext()) {
- sibs2 = sibs2 + ((Node)it1.next()).getVenn();
- if (it1.hasNext()) {
- String temp = sibs2;
- sibs2 = "(" + temp;
- while (it1.hasNext()) {
- sibs2 = sibs2 + ((Node)it1.next()).getVenn();
- }
- sibs2 = sibs2 + ")";
- }
- }
- Node nodesibs2 = new Node(null, sibs2);
- return nodesibs1.combine(nodesibs2);
- }
- private boolean inSet(Node n, Iterator<Node> it)
- {
- boolean inSet = false;
- while (it.hasNext()) {
- if (n.equals((Node)it.next())) {
- inSet = true;
- break;
- }
- }
- return inSet;
- }
- protected HashSet<Nodes> combineNew()
- {
- HashSet setResult = new HashSet();
- Node rnode1 = new Node(null, this.node2.root().getVenn());
- String added = "(" + this.node2.getVenn() + this.node1.getVenn() + ")";
- HashSet ln = this.node2.linkNodes;
- Iterator lnIt = ln.iterator();
- while (lnIt.hasNext()) {
- String result = buildUp((Node)lnIt.next(), added);
- Node rnode2 = new Node(null, result);
- setResult.add(new Nodes(rnode1, rnode2));
- }
- return setResult;
- }
- private String buildUp(Node n, String s)
- {
- if (n.parent == null) {
- return s;
- }
- String result = s;
- if (n.parent != null) {
- HashSet sibs = n.getSiblings();
- Iterator it = sibs.iterator();
- if (it.hasNext()) {
- String temp = "(" + result;
- result = temp;
- while (it.hasNext()) {
- result = result + ((Node)it.next()).getVenn();
- }
- result = result + ")";
- }
- }
- return buildUp(n.parent, result);
- }
- }
|