@Test(expected = TreeNodeException.class) public void testPathToRootException() { node3.path(root); }
public Collection<? extends TreeNode<T>> path(TreeNode<T> descendant) { if (descendant == null || isLeaf() || this.equals(descendant)) { return Collections.singletonList(this); } String errorMessage = "Unable to build the path between tree nodes. "; if (descendant.isRoot()) { String message = String.format(errorMessage + "Current node %1$s is root", descendant); throw new TreeNodeException(message); } List<TreeNode<T>> path = new LinkedList<>(); TreeNode<T> node = descendant; path.add(node); do { node = node.parent(); path.add(0, node); if (this.equals(node)) { return path; } } while (!node.isRoot()); String message = String.format(errorMessage + "The specified tree node %1$s is not the descendant of tree node %2$s", descendant, this); throw new TreeNodeException(message); }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public Collection<? extends TreeNode<T>> path(TreeNode<T> descendant) { if (descendant == null || isLeaf() || this.equals(descendant)) { return Collections.singletonList(this); } String errorMessage = "Unable to build the path between tree nodes. "; if (descendant.isRoot()) { String message = String.format(errorMessage + "Current node %1$s is root", descendant); throw new TreeNodeException(message); } List<TreeNode<T>> path = new LinkedList<>(); TreeNode<T> node = descendant; path.add(node); do { node = node.parent(); path.add(0, node); if (this.equals(node)) { return path; } } while (!node.isRoot()); String message = String.format(errorMessage + "The specified tree node %1$s is not the descendant of tree node %2$s", descendant, this); throw new TreeNodeException(message); } }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public Collection<? extends TreeNode<T>> path(TreeNode<T> descendant) { if (descendant == null || isLeaf() || this.equals(descendant)) { return Collections.singletonList(this); } String errorMessage = "Unable to build the path between tree nodes. "; if (descendant.isRoot()) { String message = String.format(errorMessage + "Current node %1$s is root", descendant); throw new TreeNodeException(message); } List<TreeNode<T>> path = new LinkedList<>(); TreeNode<T> node = descendant; path.add(node); do { node = node.parent(); path.add(0, node); if (this.equals(node)) { return path; } } while (!node.isRoot()); String message = String.format(errorMessage + "The specified tree node %1$s is not the descendant of tree node %2$s", descendant, this); throw new TreeNodeException(message); } TreeNode(T data);  TreeNode(); }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public Collection<? extends TreeNode<T>> path(TreeNode<T> descendant) { if (descendant == null || isLeaf() || this.equals(descendant)) { return Collections.singletonList(this); } String errorMessage = "Unable to build the path between tree nodes. "; if (descendant.isRoot()) { String message = String.format(errorMessage + "Current node %1$s is root", descendant); throw new TreeNodeException(message); } List<TreeNode<T>> path = new LinkedList<>(); TreeNode<T> node = descendant; path.add(node); do { node = node.parent(); path.add(0, node); if (this.equals(node)) { return path; } } while (!node.isRoot()); String message = String.format(errorMessage + "The specified tree node %1$s is not the descendant of tree node %2$s", descendant, this); throw new TreeNodeException(message); } TreeNode(T data);  TreeNode(); abstract Collection<? extends TreeNode<T>> subtrees(); abstract boolean add(TreeNode<T> subtree); abstract boolean dropSubtree(TreeNode<T> subtree); abstract void clear(); abstract TreeNodeIterator iterator(); T data(); void setData(T data); boolean isRoot(); TreeNode<T> root(); TreeNode<T> parent(); boolean isLeaf(); @SuppressWarnings("unchecked") TreeNode<T> find(final T data); Collection<? extends TreeNode<T>> findAll(final T data); boolean hasSubtree(TreeNode<T> subtree); boolean contains(TreeNode<T> node); boolean containsAll(Collection<TreeNode<T>> nodes); boolean remove(TreeNode<T> node); boolean removeAll(Collection<TreeNode<T>> nodes); void traversePreOrder(TraversalAction<TreeNode<T>> action); void traversePostOrder(TraversalAction<TreeNode<T>> action); Collection<TreeNode<T>> preOrdered(); Collection<TreeNode<T>> postOrdered(); Collection<? extends TreeNode<T>> path(TreeNode<T> descendant); TreeNode<T> commonAncestor(TreeNode<T> node); boolean isSiblingOf(TreeNode<T> node); boolean isAncestorOf(TreeNode<T> node); boolean isDescendantOf(TreeNode<T> node); long size(); int height(); int level(); @SuppressWarnings("unchecked") @Override TreeNode<T> clone(); @SuppressWarnings("unchecked") @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public Collection<? extends TreeNode<T>> path(TreeNode<T> descendant) { if (descendant == null || isLeaf() || this.equals(descendant)) { return Collections.singletonList(this); } String errorMessage = "Unable to build the path between tree nodes. "; if (descendant.isRoot()) { String message = String.format(errorMessage + "Current node %1$s is root", descendant); throw new TreeNodeException(message); } List<TreeNode<T>> path = new LinkedList<>(); TreeNode<T> node = descendant; path.add(node); do { node = node.parent(); path.add(0, node); if (this.equals(node)) { return path; } } while (!node.isRoot()); String message = String.format(errorMessage + "The specified tree node %1$s is not the descendant of tree node %2$s", descendant, this); throw new TreeNodeException(message); } TreeNode(T data);  TreeNode(); abstract Collection<? extends TreeNode<T>> subtrees(); abstract boolean add(TreeNode<T> subtree); abstract boolean dropSubtree(TreeNode<T> subtree); abstract void clear(); abstract TreeNodeIterator iterator(); T data(); void setData(T data); boolean isRoot(); TreeNode<T> root(); TreeNode<T> parent(); boolean isLeaf(); @SuppressWarnings("unchecked") TreeNode<T> find(final T data); Collection<? extends TreeNode<T>> findAll(final T data); boolean hasSubtree(TreeNode<T> subtree); boolean contains(TreeNode<T> node); boolean containsAll(Collection<TreeNode<T>> nodes); boolean remove(TreeNode<T> node); boolean removeAll(Collection<TreeNode<T>> nodes); void traversePreOrder(TraversalAction<TreeNode<T>> action); void traversePostOrder(TraversalAction<TreeNode<T>> action); Collection<TreeNode<T>> preOrdered(); Collection<TreeNode<T>> postOrdered(); Collection<? extends TreeNode<T>> path(TreeNode<T> descendant); TreeNode<T> commonAncestor(TreeNode<T> node); boolean isSiblingOf(TreeNode<T> node); boolean isAncestorOf(TreeNode<T> node); boolean isDescendantOf(TreeNode<T> node); long size(); int height(); int level(); @SuppressWarnings("unchecked") @Override TreeNode<T> clone(); @SuppressWarnings("unchecked") @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString();  }