直接干代码
function BinarySearchTree(){
this.root = null;
function Node(key){
this.key = key;
this.left = null;
this.right = null;
}
BinarySearchTree.prototype.insert = function (key){
let newNode = new Node(key);
if (this.root == null){
this.root = newNode;
return true;
}
let current = this.root;
let i = 0;
while (true){
i++;
if (current.key === key) return false;
if (current.key < key){
if (current.right == null){
current.right = newNode;
return true;
}
current = current.right;
}else {
if (current.left == null){
current.left = newNode;
return true;
}
current = current.left;
}
}
}
BinarySearchTree.prototype.search = function (key){
if (this.root == null) return false;
let current = this.root;
while (current.key != key){
if (current.key > key) current = current.left;
else if (current.key < key) current = current.right;
if (current == null) return false;
}
return true;
}
BinarySearchTree.prototype.delete = function (key){
if (this.root == null) return false;
let parent = this.root;
let current = this.root;
let isLeftNode = true;
while (current.key != key){
parent = current;
if (current.key > key) {
isLeftNode = true;
current = current.left;
}
else if (current.key < key) {
isLeftNode = false;
current = current.right;
}
if (current == null) return false;
}
if (current.left == null && current.right == null){
if (current == this.root) this.root = null;
else if (isLeftNode) parent.left = null;
else parent.right = null;
}else if(current.left == null){
if (current == this.root) this.root = current.right;
else if (isLeftNode) parent.left = current.right;
else parent.right = current.right;
}else if (current.right == null){
if (current == this.root) this.root = current.left;
else if (isLeftNode) parent.left = current.left;
else parent.right = current.left;
}else{
let successor = this.getSuccessor(current)
if (current == this.root) {
this.root = successor
}else if (isLeftNode){
parent.left = successor
}else{
parent.right = successor
}
successor.left = current.left
}
return true;
}
BinarySearchTree.prototype.getSuccessor = function(delNode){
let successor = delNode
let current = delNode.right
let successorParent = delNode
while(current != null){
successorParent = successor
successor = current
current = current.left
}
if(successor != delNode.right){
successorParent.left = successor.right
successor.right = delNode.right
}
return successor
}
BinarySearchTree.prototype.Max = function (){
if (this.root == null) return null;
let current = this.root;
while (current.right){
current = current.right;
}
console.log(current);
return current.key;
}
BinarySearchTree.prototype.Min = function (){
if (this.root == null) return null;
let current = this.root;
while (current.left){
current = current.left;
}
console.log(current);
return current.key;
}
BinarySearchTree.prototype.preOrderTraversal = function (handler){
this.preOrderNode(this.root,handler);
}
BinarySearchTree.prototype.preOrderNode = function (node,handler){
if (node != null){
handler(node.key);
this.preOrderNode(node.left,handler);
this.preOrderNode(node.right,handler);
}
}
BinarySearchTree.prototype.midOrderTraversal = function (handler){
this.midOrderNode(this.root,handler);
}
BinarySearchTree.prototype.midOrderNode = function (node,handler){
if (node != null){
this.midOrderNode(node.left,handler);
handler(node.key);
this.midOrderNode(node.right,handler);
}
}
BinarySearchTree.prototype.postOrderTraversal = function (handler){
this.postOrderNode(this.root,handler);
}
BinarySearchTree.prototype.postOrderNode = function (node,handler){
if (node != null){
this.postOrderNode(node.left,handler);
this.postOrderNode(node.right,handler);
handler(node.key);
}
}
}
let searchTree = new BinarySearchTree();
let strTree = '';
function handler(str){
strTree += str+" ";
}
console.log("---------------插入------------------")
console.log(searchTree.insert(5));
console.log(searchTree.insert(9));
console.log(searchTree.insert(3));
console.log(searchTree.insert(2));
console.log(searchTree.insert(1));
console.log(searchTree.insert(10));
console.log(searchTree.insert(11));
console.log(searchTree.insert(13));
console.log(searchTree.insert(8));
console.log(searchTree.insert(4));
console.log(searchTree.insert(4));
console.log(searchTree.root);
console.log("--------------------查询--------------------")
console.log(searchTree.search(10));
console.log(searchTree.search(8));
console.log(searchTree.search(1));
console.log(searchTree.search(16));
console.log("--------------------最值--------------------")
console.log("最大值:"+searchTree.Max());
console.log("最小值:"+searchTree.Min());
console.log("--------------------先序遍历--------------------")
searchTree.preOrderTraversal(handler);
console.log("先序遍历",strTree);
strTree = "";
console.log("--------------------中序遍历--------------------")
searchTree.midOrderTraversal(handler);
console.log("中序遍历",strTree);
strTree = "";
console.log("--------------------后序遍历--------------------")
searchTree.postOrderTraversal(handler);
console.log("后序遍历",strTree);
strTree = "";
console.log("--------------------移除父节点节点--------------------")
console.log(searchTree.delete(5));
console.log(searchTree.delete(9));
console.log(searchTree.root)
测试结果
![在这里插入图片描述]()