예제 #1
0
 /*
  * @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());
   }
 }
예제 #2
0
 /*
  * @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;
 }
예제 #3
0
 /*
  * @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);
   }
 }
예제 #4
0
 /*
  * @see org.primordion.xholon.base.IXholon#remove()
  */
 public void remove() {
   if (hasChildNodes()) {
     firstChild.remove();
   }
   if (hasNextSibling()) {
     nextSibling.remove();
   }
 }
예제 #5
0
 /*
  * @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);
   }
 }
예제 #6
0
 /*
  * @see org.primordion.xholon.base.IXholon#postAct()
  */
 public void postAct() {
   // execute recursively
   if (firstChild != null) {
     firstChild.postAct();
   }
   if (nextSibling != null) {
     nextSibling.postAct();
   }
 }
예제 #7
0
 /*
  * @see org.primordion.xholon.base.Xholon#cleanup()
  */
 public void cleanup() {
   // execute recursively
   if (firstChild != null) {
     firstChild.cleanup();
   }
   if (nextSibling != null) {
     nextSibling.cleanup();
   }
 }
예제 #8
0
 /*
  * @see org.primordion.xholon.base.IXholon#getLastChild()
  */
 public XholonTreeNode getLastChild() {
   if (hasChildNodes()) {
     if (firstChild.hasNextSibling()) {
       return firstChild.getLastSibling();
     } else {
       return firstChild;
     }
   }
   return null;
 }
예제 #9
0
 /*
  * @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;
   }
 }
예제 #10
0
 /**
  * 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;
 }
예제 #11
0
 /*
  * @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);
 }
예제 #12
0
 /*
  * @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;
   }
 }
예제 #13
0
 /**
  * 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;
 }
예제 #14
0
 /*
  * @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;
 }
예제 #15
0
 /*
  * @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();
   }
 }
예제 #16
0
 /*
  * @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);
   }
 }
예제 #17
0
 /*
  * @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;
   }
 }
예제 #18
0
 /*
  * @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);
     }
   }
 }
예제 #19
0
 /*
  * @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);
 }
예제 #20
0
 /*
  * @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);
 }
예제 #21
0
 /*
  * @see org.primordion.xholon.base.IXholon#setParentChildLinks()
  */
 public void setParentChildLinks(XholonTreeNode parent) {
   setParentNode(parent);
   parent.setFirstChild(this);
 }