Advertisement
xatzisktv

Untitled

Feb 15th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. public int range(int min, int max) {
  2. //throw exception if min < max
  3. if(min > max){
  4. throw new IllegalArgumentException("Range was not valid. Please insert min < max");
  5. }
  6. if(root != null){
  7. rangeSearch(root, min, max);
  8. }
  9. else {
  10. throw new EmptyException("Tree is empty");
  11. }
  12. return rangeSearch(root, min, max);
  13.  
  14. }
  15.  
  16. private int rangeSearch(BTNode<Integer> root, int min, int max){
  17. //check if tree is empty
  18. if(root == null){
  19. return 0;
  20. }
  21. if (root.data == max && root.data == min){
  22. return 1;
  23. }
  24. if (root.data <= max && root.data >= min){
  25. return 1 + rangeSearch(root.left, min, max) + rangeSearch(root.right, min, max);
  26. }
  27. else if(root.data < min){
  28. return rangeSearch(root.right, min, max);
  29. }
  30. else return rangeSearch(root.left, min, max);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement