Skip to content

Instantly share code, notes, and snippets.

@woonketwong
woonketwong / depthFirstSearch
Created February 18, 2014 14:32
Depth First Search
function depthFirstSearch(node){
if (node === null) return;
// visit node
console.log(node.value);
node.visited = true;
for (var i = 0; i < node.children.length; i++){
if (node.children[i].visited === false){
depthFirstSearch(node.children[i]);
@woonketwong
woonketwong / breadthFirstSearch
Created February 18, 2014 14:42
Breadth first search.
function breadthFirstSearch(node){
// build a queue
var q = [];
// initialize q
q.push(node);
var currentNode = null;
@woonketwong
woonketwong / findLocalMin
Created February 19, 2014 00:35
Suppose we are given an array A[1 .. n] with the special property that A[1] ≥ A[2] and A[n − 1] ≤ A[n]. We say that an element A[x] is a local minimum if it is less than or equal to both its neighbors, or more formally, if A[x − 1] ≥ A[x] and A[x] ≤ A[x + 1]. For example, there are five local minima in the following array: 9 7 7 2 1 3 7 5 4 7 3 3…
function findLocalMin(array, start, end){
var mid = Math.floor( (start + end)/2 );
if (((mid - 1) < 0) || ((mid + 1) >= array.length)) return null;
if (array[mid - 2] > array[mid - 1] && array[mid - 1] < array[mid]){
return array[mid-1];
} else if ( (array[mid-1] >= array[mid-2])){
return findLocalMin(array, start, mid);
} else {
@woonketwong
woonketwong / airteaching-local-gateway-setting.txt
Last active July 31, 2021 15:26
local airteaching gateway setting
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;