You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{isSorted}from'@aureooms/js-sort';consthasBinarySearchProperty=(compare,tree)=>{constarray=[...tree];returnisSorted(compare,array,0,array.length);};consthasBinarySearchPropertyAndDoesNotContainDuplicates=(compare,tree)=>{conststrictCompare=(a,b)=>compare(a,b)<=0 ? -1 : 1;// const strictCompare = (a,b) => compare(a,b) < 0 ? -1 : 1; // ugly because the choice of `<=` vs `<` depends on the implementation of `hasBinarySearchProperty` and actually does not work in the general casereturnhasBinarySearchProperty(strictCompare,tree);
consthasBinarySearchProperty=(compare,node,left=undefined,right=undefined)=>{if(node===null)returntrue;if(left!==undefined&&compare(node.key,left)<0)returnfalse;if(right!==undefined&&compare(node.key,right)>0)returnfalse;// if (right !== undefined && compare(right, node.key) < 0) return false; // if using the `strictCompare` trickreturnhasBinarySearchProperty(compare,node.left,left,node.key)&&hasBinarySearchProperty(compare,node.right,node.key,right);};consthasBinarySearchPropertyAndDoesNotContainDuplicates=(compare,node,left=undefined,right=undefined)=>{if(node===null)returntrue;if(left!==undefined&&compare(node.key,left)<=0)returnfalse;if(right!==undefined&&compare(node.key,right)>=0)returnfalse;returnhasBinarySearchProperty(compare,node.left,left,node.key)&&hasBinarySearchProperty(compare,node.right,node.key,right);};
See https://en.wikipedia.org/wiki/Binary_search_tree#Verification
The text was updated successfully, but these errors were encountered: