javascript – How to Change the Binary search Tree Node Js code to Graph using ES6
I have implemented BST using Old way
function binarySearchIterative(arr, value ){
var mid, left, right;
left = 0;
right = arr.length-1;
while(left<=right){
mid = parseInt((left+right)/2); if(value === arr[mid]){
return "Found";
}
else if(value < arr[mid]){
right = mid-1;
}
else {
left = mid+1;
}
}
return "Not found";
}
var arr = [2,3,5,7,9,12,17,20];
binarySearchIterative(arr, 17);
I wish to use Graph to implement Binary search tree,can someone provide code for BST using Java script Graph Map Objects
, basically In Binary search, would like to start with the array of sorted elements. then check if the value of the middle element in the array is equal to, greater to, or less than the value of the element to be searched. If the value of the mid-point is equal to the element to be searched, we have found the element.
If the value of the element to be searched is less than the mid-point, we narrow the interval of the array to the lower half in the next iteration. Else we consider the upper half. We repeat the process until the element is found. Thus, we ignore half of the elements in each iteration.
Regards,
Carolyn
Read more here: Source link