This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var selectionRank = function(array, rank){ | |
// randomly select a pivot | |
var pivotIndex = Math.floor( (Math.random() * array.length) ); | |
var pivot = array[pivotIndex]; | |
// left array stores the smallest <rank> elements in the array | |
var leftArr = []; | |
var rightArr = []; | |
// partition into left and right arrays | |
for (var i = 0; i < array.length; i++){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var quickSort = function(array, left, right){ | |
var leftIndex = partition(array, left, right); | |
if (left < leftIndex - 1){ | |
quickSort(array, left, leftIndex-1); | |
} | |
if (right > leftIndex){ | |
quickSort(array, leftIndex, right); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var findLocInInterspersedSortedArr = function(strArr, target, first, last){ | |
var mid = Math.floor( last - first / 2); | |
if (strArr[mid] === ''){ | |
var left = mid - 1; | |
var right = mid + 1; | |
while (true){ | |
if (left < first && right > last){ | |
return -1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var inOrderSucc = function(node){ | |
var p = null; | |
if (node !== null){ | |
if (node.parent === null || node.right !== null){ | |
p = leftMostChild(node.right); | |
console.log("result=", p); | |
} else { | |
while ((p = node.parent) !== null){ | |
if (p.left === node){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var findLevelLinkedList = function(root){ | |
var queue = []; | |
var linkedListArray = []; | |
var level = 0; | |
queue.push([root, level]); | |
root.visit = true; | |
while(queue.length !== 0){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var createMinimalBST = function(array){ | |
var makeNode = function(value){ | |
var obj = { | |
value:value, | |
left:null, | |
right:null | |
}; | |
return obj; | |
}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var search = function(startNode, endNode){ | |
//visit startNode | |
console.log("Visting:", startNode.key); | |
if (startNode.key === endNode.key){ | |
return true; | |
} | |
startNode.visit = true; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isBalanced = function(node){ | |
var maxDepth = function(node){ | |
if (node === undefined){ | |
return 0; | |
} | |
return 1 + Math.max(maxDepth(node.next[0]), maxDepth(node.next[1])); | |
} | |
var minDepth = function(node){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var breathFirstSearch = function(node){ | |
var queue = []; | |
// visit root | |
console.log("Node key, value", node.key, node.value); | |
root.visit=true; | |
queue.push(root); | |
while(queue.length !== 0){ | |
currentNode = queue.pop(); |
NewerOlder