Ejemplo n.º 1
0
 /** Get the first leaf among all its children */
 public Node<T> getFirstLeafDescendant() {
   List<Node<T>> list = getAllChildren();
   for (Node<T> node : list) {
     if (node.isLeaf()) {
       return node;
     }
   }
   return null;
 }
Ejemplo n.º 2
0
 // return the level distance to the current node
 public int getDistance(Node node) {
   Node myParent = node.getParent();
   int level = 1;
   while (myParent != null && myParent != this) {
     level++;
     myParent = myParent.getParent();
   }
   return level;
 }
Ejemplo n.º 3
0
 public List<Node> getLeafChildren() {
   List<Node> list = new ArrayList<Node>();
   for (Node n : children()) {
     if (n.isLeaf()) {
       list.add(n);
     }
   }
   return list;
 }
Ejemplo n.º 4
0
 /** Remove child from parent */
 public void removeFromParent() {
   Node parent = this.getParent();
   if (parent == null) {
     return;
   } else {
     parent.children().remove(this);
     this.setParent(null);
   }
 }
Ejemplo n.º 5
0
 public Node<T> clone() {
   Node<T> newNode = new Node<T>();
   newNode.setUserObject(this.getUserObject());
   if (this.isLeaf() == false) {
     for (Node<T> n : this.childrenList) {
       newNode.addNode(n.clone());
     }
   }
   return newNode;
 }
Ejemplo n.º 6
0
 /** Return all children and grand children */
 public List<Node<T>> getAllChildren() {
   List<Node<T>> nodeList = new ArrayList<Node<T>>();
   for (Node<T> child : childrenList) {
     nodeList.add(child);
     if (!child.isLeaf()) {
       nodeList.addAll(child.getAllChildren());
     }
   }
   return nodeList;
 }
Ejemplo n.º 7
0
  public List<Node> getLeafSiblings() {
    List<Node> list = new ArrayList<Node>();
    List<Node> siblings = getSiblings();
    for (Node n : siblings) {
      if (n.isLeaf()) {
        list.add(n);
      }
    }

    return list;
  }
Ejemplo n.º 8
0
  /**
   * Returns the total number of leaves that are descendants of this node.
   *
   * @return the number of leaves beneath this node
   */
  public int getAllLeafCount() {
    int count = 0;
    List<Node<T>> nodeList = getAllChildren();
    for (Node<T> node : nodeList) {
      if (node.isLeaf()) {
        count++;
      }
    }

    return count;
  }
Ejemplo n.º 9
0
 public List<Node> deepTrans(Node root) {
   List<Node> list = new ArrayList<Node>();
   for (int i = 0; i < root.getChildCount(); i++) {
     Node n = root.getChildAt(i);
     if (n.isLeaf()) {
       list.add(n);
     } else {
       list.addAll(deepTrans(n));
     }
   }
   list.add(root);
   return list;
 }
Ejemplo n.º 10
0
  public List<Node> getSiblings() {
    Node parent = this.getParent();
    List<Node> list = new ArrayList<Node>();
    if (parent != null) {

      for (int i = 0; i < parent.getChildCount(); i++) {
        if (parent.getChildAt(i) != this) {
          list.add(parent.getChildAt(i));
        }
      }
      return list;
    }
    return list;
  }
Ejemplo n.º 11
0
 /**
  * Is passed node a child of current node
  *
  * @param aNode
  */
 public boolean isNodeChild(Node aNode) {
   boolean retval;
   if (aNode == null) {
     retval = false;
   } else {
     if (getChildCount() == 0) {
       retval = false;
     } else {
       retval = (aNode.getParent() == this);
     }
   }
   return retval;
 }
Ejemplo n.º 12
0
  /** Is parsed in node a sibling */
  public boolean isNodeSibling(Node anotherNode) {
    boolean retval;

    if (anotherNode == null) {
      retval = false;
    } else if (anotherNode == this) {
      retval = true;
    } else {
      Node myParent = getParent();
      retval = (myParent != null && myParent == anotherNode.getParent());

      if (retval && !((Node) getParent()).isNodeChild(anotherNode)) {
        throw new Error("sibling has different parent");
      }
    }
    return retval;
  }
Ejemplo n.º 13
0
 /**
  * Add one child and set child's parent to current node
  *
  * @param node new child
  */
 public void addNode(Node node) {
   childrenList.add(node);
   node.setParent(this);
 }
Ejemplo n.º 14
0
 /**
  * Remove child from children list
  *
  * @param child
  */
 public void remove(Node child) {
   childrenList.remove(child);
   child.setParent(null);
 }
Ejemplo n.º 15
0
 /**
  * Remove child at childIndex
  *
  * @param childIndex child indexs
  */
 public void remove(int childIndex) {
   Node child = getChildAt(childIndex);
   childrenList.remove(childIndex);
   child.setParent(null);
 }