Guest User

Untitled

a guest
Oct 20th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. public V get(String key, HashNode<String, V>[] arr) {
  2. return getHelper(key, arr, 0, arr.length - 1);
  3. }
  4.  
  5. /**
  6. * Gets the helper.
  7. *
  8. * @param key the key
  9. * @param arr the arr
  10. * @param left the left
  11. * @param right the right
  12. * @return the helper
  13. */
  14. private V getHelper(String key, HashNode<String, V>[] arr, int left,
  15. int right) {
  16. if (left > right)
  17. return null;
  18.  
  19. int middle = (left + right) / 2;
  20.  
  21. if (arr[middle].getKey().equals(key))
  22. return arr[middle].getValue();
  23.  
  24. else if (arr[middle].getKey().compareTo(key) > 0)
  25. return getHelper(key, arr, left, middle - 1);
  26.  
  27. else
  28. return getHelper(key, arr, middle + 1, right);
  29. }
  30.  
  31. /**
  32. * Contains.
  33. *
  34. * @param key the key
  35. * @param arr the arr
  36. * @return true, if successful
  37. */
  38. public boolean contains(String key, HashNode<String, V>[] arr) {
  39. return containsHelper(key, arr, 0, arr.length - 1);
  40. }
  41.  
  42. /**
  43. * Contains helper.
  44. *
  45. * @param key the key
  46. * @param arr the arr
  47. * @param left the left
  48. * @param right the right
  49. * @return true, if successful
  50. */
  51. private boolean containsHelper(String key, HashNode<String, V>[] arr, int left,
  52. int right) {
  53. if (left > right)
  54. return false;
  55.  
  56. int middle = (left + right) / 2;
  57.  
  58. if (arr[middle].getKey().equals(key))
  59. return true;
  60.  
  61. else if (arr[middle].getKey().compareTo(key) > 0)
  62. return containsHelper(key, arr, left, middle - 1);
  63.  
  64. else
  65. return containsHelper(key, arr, middle + 1, right);
  66. }
Add Comment
Please, Sign In to add comment