From dd0fd1959c32f557539a8a2b1fb4e2e064cedbd1 Mon Sep 17 00:00:00 2001 From: Bill Jacobs Date: Sat, 28 May 2016 10:56:08 -0700 Subject: [PATCH] Clear links after removing node This changes the remove methods to set the parent and child links to be null, so that we're more likely to encounter an exception if we attempt to access the node. --- .../btrekkie/red_black_node/RedBlackNode.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/com/github/btrekkie/red_black_node/RedBlackNode.java b/src/com/github/btrekkie/red_black_node/RedBlackNode.java index cc84a6ce7..26fbaffe7 100644 --- a/src/com/github/btrekkie/red_black_node/RedBlackNode.java +++ b/src/com/github/btrekkie/red_black_node/RedBlackNode.java @@ -342,7 +342,7 @@ public abstract class RedBlackNode> implements Compara return root(); } - /** Returns a Comparator that compares instances of N using their natural order, as in N.compare. */ + /** Returns a Comparator that compares instances of N using their natural order, as in N.compareTo. */ private Comparator naturalOrder() { @SuppressWarnings("unchecked") Comparator comparator = (Comparator)NATURAL_ORDER; @@ -537,8 +537,8 @@ public abstract class RedBlackNode> implements Compara * unspecified. This method assumes that this is not a leaf node. This method is more efficient than remove() if * augment() might return false. * - * If the node has two children, we begin by moving the node's successor to its former position, by changing its - * "left", "right", "parent", and "isBlack" fields. + * If the node has two children, we begin by moving the node's successor to its former position, by changing the + * successor's "left", "right", "parent", and isRed fields. */ public void removeWithoutGettingRoot() { if (isLeaf()) { @@ -612,14 +612,21 @@ public abstract class RedBlackNode> implements Compara } } } + + // Clear any previously existing links, so that we're more likely to encounter an exception if we attempt to + // access the removed node + parent = null; + left = null; + right = null; + isRed = true; } /** * Removes this node from the tree that contains it. The effect of this method on the fields of this node is * unspecified. This method assumes that this is not a leaf node. * - * If the node has two children, we begin by moving the node's successor to its former position, by changing its - * "left", "right", "parent", and "isBlack" fields. + * If the node has two children, we begin by moving the node's successor to its former position, by changing the + * successor's "left", "right", "parent", and isRed fields. * * @return The root of the resulting tree. */