Exemplo n.º 1
0
  /**
   * Adds a child to this node. If possible will merge, and will perform a deep copy of the child
   * tree.
   *
   * @param newChild the new child to add (will be cloned)
   * @param addingTo the tree that this node belongs to
   */
  public final void addChild(Splitter newChild, ADTree addingTo) {

    // search for an equivalent child
    Splitter oldEqual = null;
    for (Enumeration<Splitter> e = children(); e.hasMoreElements(); ) {
      Splitter split = e.nextElement();
      if (newChild.equalTo(split)) {
        oldEqual = split;
        break;
      }
    }
    if (oldEqual == null) { // didn't find one so just add
      Splitter addChild = (Splitter) newChild.clone();
      setOrderAddedSubtree(addChild, addingTo);
      children.add(addChild);
    } else { // found one, so do a merge
      for (int i = 0; i < newChild.getNumOfBranches(); i++) {
        PredictionNode oldPred = oldEqual.getChildForBranch(i);
        PredictionNode newPred = newChild.getChildForBranch(i);
        if (oldPred != null && newPred != null) {
          oldPred.merge(newPred, addingTo);
        }
      }
    }
  }