This file contains hidden or 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
function mergeSort(array, start, end){ | |
if (start === end) return [array[start]]; | |
var mid = Math.floor( (start + end) / 2 ); | |
var arr1 = mergeSort(array, start, mid); | |
var arr2 = mergeSort(array, mid+1, end); | |
var result = merge(arr1, arr2); | |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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(); |
This file contains hidden or 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 insertionSort = function(array){ | |
var target; | |
var sortedIndex = 0; | |
var targetIndex; | |
for(var i = 0; i < array.length; i++){ | |
target = array[i]; | |
targetIndex = i; | |
for(var j = i; j >= sortedIndex; j--){ |
This file contains hidden or 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 hidden or 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; |