BST.prototype.insert = function (value) { if (value <= this.value) { if (!this.left) this.left = new BST(value); else this.left.insert(value); } else { if (!this.right) this.right = new BST(value); else this.right.insert(value); } };
BST.prototype.contains = function (searchVal) { if (this.value === searchVal) return true; if (searchVal < this.value) { if (!this.left) return false; else this.left.contains(searchVal); } else { if (!this.right) return false; else this.right.contains(searchVal); } };
BST.prototype.depthFirstTraversal = function (iteratorFnc, order) { if (order === 'pre-order') iteratorFnc(this); if (this.left) this.left.depthFirstTraversal(iteratorFnc, order); if (order === 'in-order') iteratorFnc(this); if (this.right) this.right.depthFirstTraversal(iteratorFnc, order); if (order === 'post-order') iteratorFnc(this); };
BST.prototype.breathFirstTraversal = function (iteratorFnc) { const queue = [this]; while (queue.length) { const currentNode = queue.shift(); iteratorFnc(currentNode); if (currentNode.left) queue.push(currentNode.left); if (currentNode.right) queue.push(currentNode.right); } };
BST.prototype.getMinVal = function () { if (!this.left) return this.value; else return this.left.getMinVal(); };
BST.prototype.getMaxVal = function () { if (!this.right) return this.value; else return this.right.getMaxVal(); };