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
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++ | |
} |
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
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-- |
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
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); | |
} |
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
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); | |
} | |
} |
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
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); | |
} | |
} |
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
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) | |
} |
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
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); | |
} | |
} |
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
function BST(val) { | |
this.value = val; | |
this.left = null; | |
this.right = null; | |
} |