Skip to content

Instantly share code, notes, and snippets.

@nicksspirit
Created January 29, 2022 16:21
Show Gist options
  • Select an option

  • Save nicksspirit/23640df2e4ebf5b64c7b31168c4c18f0 to your computer and use it in GitHub Desktop.

Select an option

Save nicksspirit/23640df2e4ebf5b64c7b31168c4c18f0 to your computer and use it in GitHub Desktop.
let cmp = function(a, b){
if(a < b) return -1;
if(a > b) return 1;
return 0;
};
function bsearch(arr, value, comparator) {
let low = 0,
high = arr.length - 1,
mid;
while (low <= high) {
mid = Math.floor((low + high) / 2);
let result = comparator(arr[mid], value);
if (result > 0) high = mid - 1;
else if (result < 0) low = mid + 1;
else return mid;
}
return -1;
}
console.log(bsearch([1,2,3,4,5,6], 6, cmp));
let html = `
<div id="start" class="a">
<div class="aa">
<span class="aaa">
hello
</span>
<span class="aab">
</span>
</div>
<div class="ab">
<span class="aba">
</span>
<span class="abb">
<span class="abb2">
<span class="abb3">
</span>
</span>
</span>
</div>
</div>
`
function bfs(startNode, callback) {
const historyQueue = []
const visited = []
historyQueue.push(startNode)
while(historyQueue.length) {
let currNode = historyQueue.shift()
if (currNode == null) continue
if (visited.includes(currNode)) continue
visited.push(currNode)
callback(currNode)
for (let node of Array.from(currNode.children)) {
historyQueue.push(node)
}
}
}
function dfs(startNode, callback) {
const historyStack = []
const visited = []
historyStack.push(startNode)
while(historyStack.length) {
let currNode = historyStack.pop()
if (currNode == null) continue
if (visited.includes(currNode)) continue
visited.push(currNode)
callback(currNode)
for (let node of Array.from(currNode.children)) {
historyStack.push(node)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment