Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int range(int min, int max) {
- //throw exception if min < max
- if(min > max){
- throw new IllegalArgumentException("Range was not valid. Please insert min < max");
- }
- if(root != null){
- rangeSearch(root, min, max);
- }
- else {
- throw new EmptyException("Tree is empty");
- }
- return rangeSearch(root, min, max);
- }
- private int rangeSearch(BTNode<Integer> root, int min, int max){
- //check if tree is empty
- if(root == null){
- return 0;
- }
- if (root.data == max && root.data == min){
- return 1;
- }
- if (root.data <= max && root.data >= min){
- return 1 + rangeSearch(root.left, min, max) + rangeSearch(root.right, min, max);
- }
- else if(root.data < min){
- return rangeSearch(root.right, min, max);
- }
- else return rangeSearch(root.left, min, max);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement