Skip to content

Instantly share code, notes, and snippets.

@dmdevoss
Created April 8, 2014 23:45
Show Gist options
  • Select an option

  • Save dmdevoss/10209837 to your computer and use it in GitHub Desktop.

Select an option

Save dmdevoss/10209837 to your computer and use it in GitHub Desktop.
Check binary tree's values are where they should be in the tree.
/**
* Return true if keys in this subtree are never null and are correctly sorted
* and are all in the range between the lower and upper (both exclusive).
* If either bound is null, then that means that there is no limit at this side.
* @param node root of subtree to examine
* @param lower value that all nodes must be greater than. If null, then
* there is no lower bound.
* @param upper value that all nodes must be less than. If null,
* then there is no upper bound.
* @return true if the subtree is fine. If false is returned, a problem
* should be reported.
*/
private boolean checkInRange(Node<K,V> node, K lower, K upper) {
if (node != null){
if (node.value == null) return report("null data in tree");
if (lower != null)
if (comparator.compare(node.key, lower) < 0) return report("under the bounds");
if (upper != null)
if (comparator.compare(node.key, upper) > 0) return report("above the bounds");
checkInRange(node.right, node.key, upper);
checkInRange(node.left, lower, node.key);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment