binary search on a binary tree javascript code example

Example 1: javascript binary search

const arr = [1, 3, 5, 7, 8, 9];
const binarySearch = (arr, x , strt=0, end=arr.length) => {
  if(end < strt) return false;
  let mid = Math.floor((strt + end) / 2);
  if(arr[mid] === x) {
    return true;
  }
  if(arr[mid] < x) {
    return binarySearch(arr, x, mid+1, end);
  }
  if(arr[mid] > x) {
    return binarySearch(arr, x , strt, mid-1);
  }
}

binarySearch(arr, 7); // Returns true

Example 2: binary tree javascript

Toast.makeText(getActivity(), "This is my Toast message!",
   Toast.LENGTH_LONG).show();

Tags:

Java Example