/** * {@inheritDoc} */ @Override public TreeNode[] getPath() { return getPathToRoot(this, 0); }
/** * Builds the parents of node up to and including the root node, where the original node is the last element in the * returned array. The length of the returned array gives the node's depth in the tree. * * @param aNode the DefaultTreeNode to get the path for * @param depth an int giving the number of steps already taken towards the root (on recursive calls), used to size * the returned array * @return an array of TreeNodes giving the path from the root to the specified node */ private TreeNode[] getPathToRoot(final TreeNode aNode, final int depth) { TreeNode[] retNodes; // This method recurses, traversing towards the root in order // size the array. On the way back, it fills in the nodes, // starting from the root and working back to the original node. // Check for null, in case someone passed in a null node, or they passed // in an element that isn't rooted at root. if (aNode == null) { if (depth == 0) { return null; } else { retNodes = new TreeNode[depth]; } } else { int newDepth = depth + 1; retNodes = getPathToRoot(aNode.getParent(), newDepth); retNodes[retNodes.length - newDepth] = aNode; } return retNodes; }