Exemple #1
0
 public PQNode findPertinentRoot(PQNode node) {
   if (node.isLeaf()) {
     if (node.getGraphNode() == myNodeToMerge) {
       node.setState(PQNode.State.FULL);
       node.setNumFullLeaves(1);
     } else {
       node.setState(PQNode.State.EMPTY);
       node.setNumFullLeaves(0);
     }
     return node;
   } else {
     PQNode pertinentRootCandidate = null;
     int numFullChildren = 0;
     int numEmptyChildren = 0;
     int numPartialChildren = 0;
     int numFullLeaves = 0;
     for (PQNode child : ListSequence.fromList(node.getChildren())) {
       PQNode childPertinentRoot = findPertinentRoot(child);
       numFullLeaves += child.getNumFullLeaves();
       if (child.getState() == PQNode.State.EMPTY) {
         numEmptyChildren++;
       } else {
         pertinentRootCandidate = childPertinentRoot;
         if (child.getState() == PQNode.State.FULL) {
           numFullChildren++;
         }
         if (child.getState() == PQNode.State.PARTIAL) {
           numPartialChildren++;
         }
       }
     }
     node.setNumFullLeaves(numFullLeaves);
     if (numPartialChildren > 0) {
       node.setState(PQNode.State.PARTIAL);
     } else {
       int numChildren = ListSequence.fromList(node.getChildren()).count();
       if (numFullChildren == numChildren) {
         node.setState(PQNode.State.FULL);
       } else if (numEmptyChildren == numChildren) {
         node.setState(PQNode.State.EMPTY);
       } else {
         node.setState(PQNode.State.PARTIAL);
       }
     }
     if (numPartialChildren + numFullChildren == 1) {
       return pertinentRootCandidate;
     } else {
       return node;
     }
   }
 }
Exemple #2
0
 public PQNode modifyTree(Node nodeToMerge) {
   myNodeToMerge = nodeToMerge;
   String oldTreeRepresentation = this.toString();
   int oldNumFullLeaves = findPertinentRoot(myRoot).getNumFullLeaves();
   makeReducible();
   PQNode pertinentRoot = findPertinentRoot(myRoot);
   int numFullLeaves = pertinentRoot.getNumFullLeaves();
   if (SHOW_LOG > 0) {
     if (oldNumFullLeaves > numFullLeaves) {
       System.out.println("Graph has been made reductible!");
       System.out.println("before:");
       System.out.println(oldTreeRepresentation);
       System.out.println("after:");
       System.out.println(this);
     }
   }
   if (numFullLeaves == 1) {
     pertinentRoot = pertinentRoot.getParent();
     List<PQNode> children = pertinentRoot.getChildren();
     ListIterator<PQNode> childItr = children.listIterator();
     PNode newNode = null;
     while (childItr.hasNext()) {
       PQNode child = childItr.next();
       if (child.getState() == PQNode.State.FULL) {
         newNode = new PNode(nodeToMerge, ((PNode) child).getEdge());
         newNode.setParent(pertinentRoot);
         childItr.set(newNode);
       }
     }
     return newNode;
   } else {
     List<PQNode> modifiedChildren = ListSequence.fromList(new ArrayList<PQNode>());
     if (pertinentRoot instanceof QNode) {
       PQNode prePertinentRoot = pertinentRoot.getParent();
       for (PQNode child : ListSequence.fromList(prePertinentRoot.getChildren())) {
         if (child == pertinentRoot) {
           ListSequence.fromList(modifiedChildren).addElement(arrange(child, true));
         } else {
           ListSequence.fromList(modifiedChildren).addElement(arrange(child, false));
         }
       }
       for (PQNode child : ListSequence.fromList(modifiedChildren)) {
         child.setParent(prePertinentRoot);
       }
       return prePertinentRoot.processAsPertinentRoot(modifiedChildren, nodeToMerge);
     } else {
       for (PQNode child : ListSequence.fromList(pertinentRoot.getChildren())) {
         ListSequence.fromList(modifiedChildren).addElement(arrange(child, false));
       }
       for (PQNode child : ListSequence.fromList(modifiedChildren)) {
         child.setParent(pertinentRoot);
       }
       return pertinentRoot.processAsPertinentRoot(modifiedChildren, nodeToMerge);
     }
   }
 }