@Test public void testContains() { String messageContainExpected = "The tree node was expected to contain the specified node, but actually was not"; assertTrue(messageContainExpected, root.contains(node2)); assertTrue(messageContainExpected, root.contains(node6)); assertTrue(messageContainExpected, node3.contains(node4)); assertTrue(messageContainExpected, node2.contains(node5)); String messageNotContainExpected = "The tree node was not expected to contain the specified tree node, but actually was"; assertFalse(messageNotContainExpected, root.contains(root)); assertFalse(messageNotContainExpected, node4.contains(node3)); assertFalse(messageNotContainExpected, node2.contains(null)); assertFalse(messageNotContainExpected, node3.contains(root)); assertFalse(messageNotContainExpected, root.contains(anotherNode)); }
public boolean contains(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } for (TreeNode<T> subtree : subtrees()) { if (subtree.equals(node) || subtree.contains(node)) { return true; } } return false; }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public boolean contains(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } for (TreeNode<T> subtree : subtrees()) { if (subtree.equals(node) || subtree.contains(node)) { return true; } } return false; } }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public boolean contains(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } for (TreeNode<T> subtree : subtrees()) { if (subtree.equals(node) || subtree.contains(node)) { return true; } } return false; } TreeNode(T data);  TreeNode(); }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public boolean contains(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } for (TreeNode<T> subtree : subtrees()) { if (subtree.equals(node) || subtree.contains(node)) { return true; } } 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(); }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public boolean contains(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } for (TreeNode<T> subtree : subtrees()) { if (subtree.equals(node) || subtree.contains(node)) { return true; } } 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();  }