Example #1
0
  public List<GenericTreeNode<T>> traceParents() {
    // logger.debug("traceParents");
    List<GenericTreeNode<T>> parents = new ArrayList<>();
    boolean reachedRoot = false;

    int maxIterations = 100;
    int i = 0;
    GenericTreeNode<T> currentNode = this;

    while (!reachedRoot) {
      i++;
      // logger.debug("Iteration: " + i);
      if (i > maxIterations) {
        logger.warn("Too deep recursion, breaking");
        break;
      }
      GenericTreeNode<T> p = currentNode.getParent();
      if (p != null) {
        // logger.debug("Adding parent: " + p.getData().toString());
        parents.add(p);
        currentNode = p;
      } else {
        // logger.debug("this node is a root node: " + currentNode.getData().toString());
        reachedRoot = true;
      }
    }

    return parents;
  }
Example #2
0
  private int auxiliaryGetNumberOfNodes(GenericTreeNode<T> node) {
    int numberOfNodes = node.getNumberOfChildren();

    for (GenericTreeNode<T> child : node.getChildren()) {
      numberOfNodes += auxiliaryGetNumberOfNodes(child);
    }

    return numberOfNodes;
  }
Example #3
0
  private GenericTreeNode<T> auxiliaryFind(GenericTreeNode<T> currentNode, T dataToFind) {
    GenericTreeNode<T> returnNode = null;
    int i = 0;

    if (currentNode.getData().equals(dataToFind)) {
      returnNode = currentNode;
    } else if (currentNode.hasChildren()) {
      i = 0;
      while (returnNode == null && i < currentNode.getNumberOfChildren()) {
        returnNode = auxiliaryFind(currentNode.getChildAt(i), dataToFind);
        i++;
      }
    }

    return returnNode;
  }
Example #4
0
  private void buildPostOrder(GenericTreeNode<T> node, List<GenericTreeNode<T>> traversalResult) {
    for (GenericTreeNode<T> child : node.getChildren()) {
      buildPostOrder(child, traversalResult);
    }

    traversalResult.add(node);
  }
Example #5
0
  private void buildPostOrderWithDepth(
      GenericTreeNode<T> node, Map<GenericTreeNode<T>, Integer> traversalResult, int depth) {
    for (GenericTreeNode<T> child : node.getChildren()) {
      buildPostOrderWithDepth(child, traversalResult, depth + 1);
    }

    traversalResult.put(node, depth);
  }
Example #6
0
 public void addChildAt(int index, GenericTreeNode<T> child) throws IndexOutOfBoundsException {
   child.parent = this;
   children.add(index, child);
 }
Example #7
0
 public void addChild(GenericTreeNode<T> child) {
   child.parent = this;
   children.add(child);
 }
Example #8
0
 public void setChildren(List<GenericTreeNode<T>> children) {
   for (GenericTreeNode<T> child : children) {
     child.parent = this;
   }
   this.children = children;
 }