/** 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; }
// 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; }
public List<Node> getLeafChildren() { List<Node> list = new ArrayList<Node>(); for (Node n : children()) { if (n.isLeaf()) { list.add(n); } } return list; }
/** Remove child from parent */ public void removeFromParent() { Node parent = this.getParent(); if (parent == null) { return; } else { parent.children().remove(this); this.setParent(null); } }
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; }
/** 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; }
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; }
/** * 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; }
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; }
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; }
/** * 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; }
/** 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; }
/** * 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); }
/** * Remove child from children list * * @param child */ public void remove(Node child) { childrenList.remove(child); child.setParent(null); }
/** * Remove child at childIndex * * @param childIndex child indexs */ public void remove(int childIndex) { Node child = getChildAt(childIndex); childrenList.remove(childIndex); child.setParent(null); }