@Test public void testRemove() { String messageRemoveExpected = "The tree node was expected to be removed, but actually was not"; String messageRemoveResultTrueExpected = "The tree node remove result was expected to be true, but actually was false"; assertTrue(messageRemoveResultTrueExpected, node3.remove(node4)); assertFalse(messageRemoveExpected, node3.contains(node4)); assertTrue(messageRemoveResultTrueExpected, root.remove(node9)); assertFalse(messageRemoveExpected, root.contains(node9)); assertFalse(messageRemoveExpected, root.contains(node10)); String messageRemoveNotExpected = "The tree node was not expected to be removed, but actually was"; String messageRemoveResultFalseExpected = "The tree node remove result was expected to be false, but actually was true"; assertFalse(root.contains(anotherNode)); assertFalse(messageRemoveResultFalseExpected, root.remove(anotherNode)); assertFalse(messageRemoveNotExpected, root.contains(anotherNode)); assertFalse(messageRemoveResultFalseExpected, node7.remove(node6)); assertFalse(messageRemoveResultFalseExpected, node1.remove(null)); assertFalse(messageRemoveResultFalseExpected, node1.remove(root)); TreeNode<String> mNodeToRemove1 = createTreeNode(ANOTHER_NODE_DATA); TreeNode<String> mNodeToRemove2 = createTreeNode(ANOTHER_NODE_DATA); node1.add(mNodeToRemove1); node1.add(mNodeToRemove2); assertTrue(messageRemoveResultTrueExpected, root.remove(mNodeToRemove1)); assertFalse(messageRemoveExpected, root.contains(mNodeToRemove1)); assertTrue(messageRemoveNotExpected, root.contains(mNodeToRemove2)); assertTrue(messageRemoveNotExpected, node1.contains(mNodeToRemove2)); }
public boolean remove(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } if (dropSubtree(node)) { return true; } for (TreeNode<T> subtree : subtrees()) { if (subtree.remove(node)) { return true; } } return false; }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public boolean remove(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } if (dropSubtree(node)) { return true; } for (TreeNode<T> subtree : subtrees()) { if (subtree.remove(node)) { return true; } } return false; } }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public boolean remove(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } if (dropSubtree(node)) { return true; } for (TreeNode<T> subtree : subtrees()) { if (subtree.remove(node)) { return true; } } return false; } TreeNode(T data);  TreeNode(); }
TreeNode implements Iterable<TreeNode<T>>, Serializable, Cloneable { public boolean remove(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } if (dropSubtree(node)) { return true; } for (TreeNode<T> subtree : subtrees()) { if (subtree.remove(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 remove(TreeNode<T> node) { if (node == null || isLeaf() || node.isRoot()) { return false; } if (dropSubtree(node)) { return true; } for (TreeNode<T> subtree : subtrees()) { if (subtree.remove(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();  }