Skip to content

Instantly share code, notes, and snippets.

@kaisinli
kaisinli / arrayception-recursion.js
Last active August 22, 2018 18:19
arrayception-recursion.js
function arrayception(arr) {
let maxDepth = 0;
let count = 0;
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
count = arrayception(arr[i]);
if (count > 0) {
count++
}
@kaisinli
kaisinli / arrayception.js
Last active August 22, 2018 15:47
arrayception
function arrayception(arr) {
let str = JSON.stringify(arr);
let openCounter = 0;
let maxDepth = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === '[') {
openCounter++;
} else if (str[i] === ']') {
openCounter--
@kaisinli
kaisinli / btsDepthFirst.js
Created July 30, 2018 04:51
btsDepthFirst
BST.prototype.depthFirstTraversal = function (iteratorFunc, order) {
if (order === 'preOrder') iteratorFunc(this.value);
if (this.left) this.left.depthFirstTraversal(iteratorFunc,order);
if (order === 'inOrder') iteratorFunc(this.value);
if (this.right) this.right.depthFirstTraversal(iteratorFunc,order);
if (order === 'postOrder') iteratorFunc(this.value);
}
@kaisinli
kaisinli / btsBreadthFirstTraversal.js
Created July 30, 2018 04:30
btsBreadthFirstTraversal
BST.prototype.breadthFirstTraversal = function (iteratorFunc) {
let queue = [this];
while (queue.length) {
let parentNode = queue.shift();
iteratorFunc(parentNode);
if(parentNode.left) queue.push(parentNode.left);
if(parentNode.right) queue.push(parentNode.right);
}
}
@kaisinli
kaisinli / btsMaxOrMin.js
Created July 30, 2018 04:12
btsMaxOrMin
BST.prototype.getMinOrMax = function (option) {
if (option === 'min') {
if(!this.left) return this.value;
else return this.left.getMinOrMax(option);
}
else if (option === 'max') {
if(!this.right) return this.value;
else return this.right.getMinOrMax(option);
}
}
@kaisinli
kaisinli / btsContains.js
Created July 30, 2018 04:00
btsContains
BST.prototype.contains = function (val) {
if (this.value === val) return true;
if (val < this.value) {
if (!this.left) return false;
else return this.left.contains(val)
}
else if (val > this.value) {
if (!this.right) return false;
else return this.right.contains(val)
}
@kaisinli
kaisinli / btsInsert.js
Last active July 30, 2018 03:56
btsInsert
BST.prototype.insert = function (val) {
if (val <= this.value) {
if (!this.left) this.left = new BST(val);
else this.left.insert(val);
}
else if (val > this.value) {
if (!this.right) this.right = new BST(val);
else this.right.insert(val);
}
}
@kaisinli
kaisinli / btsConstructor.js
Last active July 30, 2018 03:55
JavaScript BTS
function BST(val) {
this.value = val;
this.left = null;
this.right = null;
}