function BST() {
function Node(key) {
this.key = key;
this.left = null;
this.right = null;
}
this.root = null;
BST.prototype.insert = function (key) {
let newNode = new Node(key);
if (!this.root) {
this.root = newNode;
} else {
this._insert(this.root, newNode);
}
};
BST.prototype._insert = function (root, node) {
if (node.key < root.key) {
if (!root.left) {
root.left = node;
} else {
this._insert(root.left, node);
}
} else {
if (!root.right) {
root.right = node;
} else {
this._insert(root.right, node);
}
}
};
BST.prototype.preSeaerch = function (handler) {
if (!this.root) {
return;
}
this._preSearch(this.root, handler);
};
BST.prototype._preSearch = function (root, handler) {
if (!root) {
return;
}
handler(root.key);
this._preSearch(root.left, handler);
this._preSearch(root.right, handler);
};
BST.prototype.midSeaerch = function (handler) {
if (!this.root) {
return;
}
this._midSearch(this.root, handler);
};
BST.prototype._midSearch = function (root, handler) {
if (!root) {
return;
}
this._midSearch(root.left, handler);
handler(root.key);
this._midSearch(root.right, handler);
};
BST.prototype.postSeaerch = function (handler) {
if (!this.root) {
return;
}
this._postSearch(this.root, handler);
};
BST.prototype._postSearch = function (root, handler) {
if (!root) {
return;
}
this._postSearch(root.left, handler);
this._postSearch(root.right, handler);
handler(root.key);
};
BST.prototype.max = function () {
let root = this.root;
let key = null;
while (root) {
key = root.key;
root = root.right;
}
return key;
};
BST.prototype.min = function () {
let root = this.root
let key = null
while (root) {
key = root.key
root = root.left
}
return key
}
BST.prototype.search = function (key) {
let root = this.root
while (root) {
if (key < root.key) {
root = root.left
} else if (key > root.key) {
root = root.right
} else {
return true
}
return false
}
}
BST.prototype.remove = function (key) {
let root = this.root
let parent = null
let isLeft = true
while(root.key !== key) {
parent = root
if (key < root.key) {
isLeft = true
root = root.left
} else {
isLeft = false
root = root.right
}
if (!root) {
return false
}
}
if (!root.left && !root.right) {
if (root === this.root) {
this.root = null
} else if(isLeft) {
parent.left = null
} else {
parent.right = null
}
}
else if(!root.right) {
if (root === this.root) {
this.root = root.left
} else if(isLeft) {
parent.left = root.left
} else {
parent.right = root.left
}
} else if(!root.left) {
if (root === this.root) {
this.root = root.right
} else if (isLeft) {
parent.left = root.right
} else {
parent.right = root.right
}
}
else {
let childNode = this.getChildNode(root)
if (root === this.root) {
this.root = childNode
} else if (isLeft) {
parent.left = childNode
} else {
parent.right = childNode
}
childNode.left = root.left
}
}
BST.prototype.getChildNode = function (delNode) {
let childNode = delNode
let current = delNode.right
let childNodeParent = delNode
while (current) {
childNodeParent = childNode
childNode = current
current = current.left
}
if (childNode !== delNode.right) {
childNodeParent.left = childNode.right
childNode.right = delNode.right
}
return childNode
}
}
let tree = new BST();
tree.insert(11);
tree.insert(7);
tree.insert(15);
tree.insert(5);
tree.insert(3);
tree.insert(9);
tree.insert(8);
tree.insert(10);
tree.insert(13);
tree.insert(12);
tree.insert(14);
tree.insert(20);
tree.insert(18);
tree.insert(25);
tree.insert(6);
let preStr = "";
tree.preSeaerch((key) => {
preStr += key + " ";
});
let midStr = "";
tree.midSeaerch((key) => {
midStr += key + " ";
});
let postStr = "";
tree.postSeaerch((key) => {
postStr += key + " ";
});
console.log(preStr);
console.log(midStr);
console.log(postStr);
console.log(tree.max());
console.log(tree.min());
console.log(tree.search(11));
console.log(tree);
tree.remove(5)
let preStr2 = "";
tree.preSeaerch((key) => {
preStr2 += key + " ";
});
console.log(preStr2);