Created
April 8, 2014 23:45
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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