TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public boolean isAncestorOf(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot() || this.equals(node)) { return false; } TreeNode<T> mNode = node; do { mNode = mNode.parent(); if (this.equals(mNode)) { return true; } } while (!mNode.isRoot()); return false; } 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();  }
@Test public void testIsAncestorOf() { String messageTrue = "The specified tree node was expected to be the ancestor of the current tree node, " + "but actually was not"; assertTrue(messageTrue, root.isAncestorOf(node1)); assertTrue(messageTrue, root.isAncestorOf(node8)); assertTrue(messageTrue, node2.isAncestorOf(node6)); assertTrue(messageTrue, node9.isAncestorOf(node10)); String messageFalse = "The specified tree node was not expected to be the ancestor of the current tree node, " + "but actually was"; assertFalse(messageFalse, node10.isAncestorOf(node9)); assertFalse(messageFalse, node9.isAncestorOf(null)); assertFalse(messageFalse, node2.isAncestorOf(root)); assertFalse(messageFalse, node1.isAncestorOf(node1)); assertFalse(messageFalse, node3.isAncestorOf(anotherNode)); }