/* * @see org.primordion.xholon.base.IXholon#appendChild(org.primordion.xholon.base.IXholon) */ public void appendChild(XholonTreeNode newParentNode) { if (newParentNode.getFirstChild() == null) { setParentChildLinks(newParentNode); } else { setParentSiblingLinks(newParentNode.getLastChild()); } }
/* * @see org.primordion.xholon.base.IXholon#getRootNode() */ public XholonTreeNode getRootNode() { // get root node of this tree XholonTreeNode node = this; while (!node.isRootNode()) { node = node.getParentNode(); } return node; }
/* * @see org.primordion.xholon.base.IXholon#insertFirstChild(org.primordion.xholon.base.IXholon) */ public void insertFirstChild(XholonTreeNode newParentNode) { if (newParentNode.getFirstChild() == null) { setParentChildLinks(newParentNode); } else { XholonTreeNode newSibling = newParentNode.getFirstChild(); insertBefore(newSibling); } }
/* * @see org.primordion.xholon.base.IXholon#remove() */ public void remove() { if (hasChildNodes()) { firstChild.remove(); } if (hasNextSibling()) { nextSibling.remove(); } }
/* * @see org.primordion.xholon.base.IXholon#insertAfter(org.primordion.xholon.base.IXholon) */ public void insertAfter(XholonTreeNode newLeftSibling) { if (newLeftSibling.hasNextSibling()) { parent = newLeftSibling.getParentNode(); nextSibling = newLeftSibling.getNextSibling(); newLeftSibling.setNextSibling(this); } else { // last sibling setParentSiblingLinks(newLeftSibling); } }
/* * @see org.primordion.xholon.base.IXholon#postAct() */ public void postAct() { // execute recursively if (firstChild != null) { firstChild.postAct(); } if (nextSibling != null) { nextSibling.postAct(); } }
/* * @see org.primordion.xholon.base.Xholon#cleanup() */ public void cleanup() { // execute recursively if (firstChild != null) { firstChild.cleanup(); } if (nextSibling != null) { nextSibling.cleanup(); } }
/* * @see org.primordion.xholon.base.IXholon#getLastChild() */ public XholonTreeNode getLastChild() { if (hasChildNodes()) { if (firstChild.hasNextSibling()) { return firstChild.getLastSibling(); } else { return firstChild; } } return null; }
/* * @see org.primordion.xholon.base.IXholon#getLastSibling() */ public XholonTreeNode getLastSibling() { XholonTreeNode node = this; while (node.hasNextSibling()) { node = node.getNextSibling(); } if (node == this) { return null; } else { return node; } }
/** * Helper function to recursively get all children. * * @param v List that gets filled with children. * @param deep If true then return entire nested subtree, if false return only immediate children. * @return List containing children. */ protected List getChildNodes(List v, boolean deep) { XholonTreeNode nextChild = getFirstChild(); while (nextChild != null) { v.add(nextChild); if (deep) { ((XholonTreeNode) nextChild).getChildNodes(v, deep); } nextChild = nextChild.getNextSibling(); } return v; }
/* * @see org.primordion.xholon.base.IXholon#treeSize() */ public int treeSize() { int lSize = 0; int rSize = 0; XholonTreeNode node = getFirstChild(); if (node != null) { lSize = node.treeSize(); } node = getNextSibling(); if (node != null) { rSize = node.treeSize(); } return (1 + lSize + rSize); }
/* * @see org.primordion.xholon.base.IXholon#height() */ public int height() { int hL = 0; int hR = 0; if (isExternal()) { return 0; } else { if (hasChildNodes()) { hL = firstChild.height(); } if (hasNextSibling()) { hR = nextSibling.height(); } return hL > hR ? hL + 1 : hR + 1; } }
/** * Get the least common ancestor of this node and the specified other node. * * @param otherNode * @return */ public XholonTreeNode getLeastCommonAncestor(XholonTreeNode otherNode) { if (otherNode == null) { return this; } if (otherNode == this) { return this; } // if (isRootNode()) {return this;} // if (otherNode.isRootNode()) {return otherNode;} XholonTreeNode thisNode = this; // 2 nodes cannot have a common parent unless they are at the same level (depth), // so start by adjusting the path with the greatest depth int thisDepth = depth(); int otherDepth = otherNode.depth(); while (thisDepth > otherDepth) { thisNode = thisNode.getParentNode(); thisDepth--; } while (otherDepth > thisDepth) { otherNode = otherNode.getParentNode(); otherDepth--; } // now compare ancestors in lock-step while (!thisNode.equals(otherNode)) { thisNode = thisNode.getParentNode(); otherNode = otherNode.getParentNode(); } return thisNode; }
/* * @see org.primordion.xholon.base.IXholon#getPreviousSibling() */ public XholonTreeNode getPreviousSibling() { if (isRootNode()) { return null; // this is the root node } XholonTreeNode node = getParentNode().getFirstChild(); if (node == this) { return null; // this node is already the first sibling } XholonTreeNode leftNode = node; while (leftNode.getNextSibling() != null) { node = leftNode.getNextSibling(); if (node == this) { return leftNode; } leftNode = node; // should never get here } return null; }
/* * @see org.primordion.xholon.base.IXholon#getSiblings() */ public List getSiblings() { if (!isRootNode()) { List v = parent.getChildNodes(false); v.remove(this); return v; } else { return new ArrayList(); } }
/* * @see org.primordion.xholon.base.IXholon#removeChild() */ public void removeChild() { if (!isRootNode()) { XholonTreeNode lNode = getPreviousSibling(); XholonTreeNode rNode = getNextSibling(); if (lNode == null) { // this is the first (leftmost) sibling if (rNode == null) { getParentNode().setFirstChild(null); } else { getParentNode().setFirstChild(rNode); // nextSibling is new firstChild of parent } } else { if (rNode == null) { lNode.setNextSibling(null); } else { lNode.setNextSibling(rNode); } } setParentNode(null); setNextSibling(null); } }
/* * @see org.primordion.xholon.base.IXholon#getFirstSibling() */ public XholonTreeNode getFirstSibling() { XholonTreeNode node = null; if (!isRootNode()) { node = parent.getFirstChild(); if (node == this) { return null; } else { return node; } } else { return null; } }
/* * @see org.primordion.xholon.base.IXholon#swapNode(org.primordion.xholon.base.IXholon) */ public void swapNode(XholonTreeNode otherNode) { XholonTreeNode thisParent = parent; XholonTreeNode otherParent = otherNode.getParentNode(); XholonTreeNode thisNextSibling = nextSibling; XholonTreeNode otherNextSibling = otherNode.getNextSibling(); if (thisParent != otherParent) { // no change if both have same parent removeChild(); otherNode.removeChild(); // insert this node if (otherNextSibling == null) { appendChild(otherParent); } else { insertBefore(otherNextSibling); } // insert other node if (thisNextSibling == null) { otherNode.appendChild(thisParent); } else { otherNode.insertBefore(thisNextSibling); } } }
/* * @see org.primordion.xholon.base.IXholon#insertBefore(org.primordion.xholon.base.IXholon) */ public void insertBefore(XholonTreeNode newNextSibling) { parent = newNextSibling.getParentNode(); if (parent.getFirstChild() == newNextSibling) { parent.setFirstChild(this); } else { XholonTreeNode lSibling = newNextSibling.getPreviousSibling(); lSibling.setNextSibling(this); } setNextSibling(newNextSibling); }
/* * @see org.primordion.xholon.base.IXholon#setParentSiblingLinks(org.primordion.xholon.base.TreeNode) */ public void setParentSiblingLinks(XholonTreeNode previousSibling) { setParentNode(previousSibling.getParentNode()); // previousSibling already has parent previousSibling.setNextSibling(this); }
/* * @see org.primordion.xholon.base.IXholon#setParentChildLinks() */ public void setParentChildLinks(XholonTreeNode parent) { setParentNode(parent); parent.setFirstChild(this); }