DATA VISUALIZATION

Binary Search Tree

Binary search is a divide-and-conquer algorithm that searches for a target value within a sorted array.

Steps:

  1. Start with the middle element.
  2. If the target equals the middle, return the index.
  3. If target < middle, repeat on the left half.
  4. If target > middle, repeat on the right half.

Code:

function binarySearch(arr, target) {
    let left = 0;
    let right = arr.length - 1;
    
    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        if (arr[mid] === target) return mid;
        if (arr[mid] < target) left = mid + 1;
        else right = mid - 1;
    }
    
    return -1;
    }

✅ Pros:

  • Fast search in sorted arrays (O(log n))
  • Simple and efficient
  • Low memory usage

❌ Cons:

  • Only works on sorted arrays
  • Difficult to debug for off-by-one errors
  • Not suitable for frequently updated arrays

Binary Search Tree Visualization