BinarySearchTree
function Tree(value) {
this.value = value
this.left = null
this.right = null
}
Tree.prototype.add = function(value) {
var value = value
var newTreeNode
if (typeof value === "object") {
newTreeNode = value
value = newTreeNode.value
}
if(this.value > value) {
if(!this.left) {
this.left = newTreeNode ? newTreeNode : new Tree(value)
} else if (this.left) {
this.left.add(value)
}
} else if (this.value < value) {
if (!this.right) {
this.right = newTreeNode ? newTreeNode : new Tree(value)
} else if (this.right) {
this.right.add(value)
}
}
}
Tree.prototype.deleteNode = function(value) {
var node = this
if (node.left && value === node.left.value) {
var deleteNode = node.left
node.left = null
if (deleteNode.left) {
node.add(deleteNode.left)
}
if (deleteNode.right) {
node.add(deleteNode.right)
}
}
if (node.right && value === node.right.value) {
var deleteNode = node.right
node.right = null
if (deleteNode.left) {
node.add(deleteNode.left)
}
if (deleteNode.right) {
node.add(deleteNode.right)
}
}
if (node.left && node.value > value) {
node.left.deleteNode()
} else if (node.right && node.value < value) {
node.right.deleteNode()
}
}
Tree.prototype.search = function(value, cb) {
if (this.value === value) {
cb(this)
return
}
if (this.right && this.value < value) {
this.right.search(value, cb)
}
if (this.left && this.value > value) {
this.left.search(value, cb)
}
return false
}
Tree.prototype.bfs = function(cb) {
var queue = [this]
while (queue.length > 0) {
var node = queue.shift()
cb(node)
if (node.left) {
queue.push(node.left)
}
if (node.right) {
queue.push(node.right)
}
}
}
Tree.prototype._inOrder = function(cb) {
if (this.left) {
this.left._inOrder(cb)
}
cb(this)
if (this.right) {
this.right._inOrder(cb)
}
}
Tree.prototype._preOrder = function(cb) {
cb(this)
if (this.left) {
this.left._inOrder(cb)
}
if (this.right) {
this.right._inOrder(cb)
}
}
Tree.prototype._postOrder = function(cb) {
if (this.left) {
this.left._inOrder(cb)
}
if (this.right) {
this.right._inOrder(cb)
}
cb(this)
}
Tree.prototype.dfs = function(cb) {
this._inOrder(cb)
}
Tree.prototype.getMax = function() {
var node = this
while (node.right) {
node = node.right
}
return node.value
}
Tree.prototype.getMin = function() {
var node = this
while (node.left) {
node = node.left
}
return node.value
}
Tree.prototype.getHeight = function(node) {
if (!node) {
return 0
}
var left = this.getHeight(node.left)
var right = this.getHeight(node.right)
return 1 + Math.max(left, right)
}
Tree.prototype.isBalanced = function() {
var left = this.getHeight(this.left)
var right = this.getHeight(this.right)
return Math.abs(left - right) < 1
}
var tree = new Tree(41)
tree.add(98)
tree.add(2)
tree.add(31)
tree.add(23)
console.log("---------- bfs ------------")
tree.bfs(function(node) { console.log(node.value) })
console.log("---------- dfs ------------")
tree.dfs(function(node) { console.log(node.value) })
console.log("-------------------------")
console.log("Min Number", tree.getMin())
console.log("Max Number", tree.getMax())
console.log(tree.getHeight(tree))
console.log("----- isBalanced -------")
console.log(tree.isBalanced())
console.log("----- search -------")
tree.search(2, function(node) { console.log(node.value) })
console.log(" ")
console.log("####### ! Delete ########")
tree.deleteNode(2)
tree.deleteNode(1)
tree.deleteNode(98)
tree.deleteNode(31)
tree.deleteNode(23)
console.log("---------- bfs ------------")
tree.bfs(function(node) { console.log(node.value) })
console.log("---------- dfs ------------")
tree.dfs(function(node) { console.log(node.value) })
console.log("Min Number", tree.getMin())
console.log("Max Number", tree.getMax())