/** * 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; }