二叉树的数据结构的重点内容,而二叉搜索树又是二叉树的重点,地位仅次于红黑树
下面是js实现的具体方法,插入 先中后遍历,求最值,删除(最难,没有完善,求大佬)。
function BinarySearchTree() {
function Node(key) {
this.key = key;
this.left = null;
this.right = null;
}
//属性
this.root = null
//方法
//插入数据
BinarySearchTree.prototype.insert = function(key) {
//1.根据key创建节点
let newNode = new Node(key);
//2.判断根节点是否有值
if (this.root == null) {
this.root = newNode
} else {
this.insertNode(this.root, newNode)
}
}
//查找方法的私有方法,不对外开放
BinarySearchTree.prototype.insertNode = function(node, newNode) {
if (newNode.key < node.key) {
//向左查找
if (node.left == null) {
node.left = newNode;
} else {
this.insertNode(node.left, newNode);
}
} else {
//向右查找
if (node.right == null) {
node.right = newNode
} else {
this.insertNode(node.right, newNode);
}
}
}
//树的遍历
BinarySearchTree.prototype.preOrderTraversal = function(handler) {
this.preOrderTraversalNode(this.root, handler);
}
BinarySearchTree.prototype.preOrderTraversalNode = function(node, handler) {
if (node != null) {
//处理经过的节点
handler(node.key);
//处理经过节点的左子节点
this.preOrderTraversalNode(node.left, handler);
//3.处理经过节点的柚子节点
this.preOrderTraversalNode(node.right, handler)
}
}
//2.中序遍历
BinarySearchTree.prototype.midOrderTraser = function(handler) {
this.midOrderTraserNode(this.root, handler)
}
BinarySearchTree.prototype.midOrderTraserNode = function(node, handler) {
if (node != null) {
//处理左子树中的节点
this.midOrderTraserNode(node.left, handler)
//处理经过的节点
handler(node.key);
this.midOrderTraserNode(node.right, handler)
}
}
//后序遍历
BinarySearchTree.prototype.postOrderTraser = function(handler) {
this.postOrderTraserNode(this.root, handler)
}
BinarySearchTree.prototype.postOrderTraserNode = function(node, hanler) {
if (node != null) {
//1.处理左子树中的节点
this.postOrderTraserNode(node.left, hanler)
//1.处理You子树中的节点
this.postOrderTraserNode(node.right, hanler)
hanler(node.key)
}
}
//寻找最值(最大值)
BinarySearchTree.prototype.max = function() {
let node = this.root;
let key = null;
while (node != null) {
key = node.key
node = node.right
}
return key
}
//最小值
BinarySearchTree.prototype.min = function() {
let node = this.root;
let key = null;
while (node != null) {
key = node.key
node = node.left
}
return key
}
//搜索某一个key
BinarySearchTree.prototype.search = function(key) {
//1.获取更节点
let node = this.root;
//2.循环
while (node != null) {
if (node.key > key) {
//往左找
node = node.left
} else if (node.key < key) {
node = node.right
} else {
return true
}
}
return false
}
//删除节点
/*
1.没有子节点
*/
BinarySearchTree.prototype.remove = function(key) {
//1.寻找要删除的节点
//2.定义变量,保存一些信息
let current = this.root
let parent = null;
let isLeftChild = true
//1.2寻找删除的节点
while (current.key != key) {
parent = current
if (key < current.key) {
isLeftChild = true
current = current.left
} else {
isLeftChild = false;
current = current.right
}
}
//如果没有找到
if (current == null) {
return false
}
//根据对应的情况删除的节点
//找到了key
// 2.1.刷除的节点是叶子节点(没有子节点)
if (current.left == null && current.right == null) {
if (current == this.root) {
this.root = null
} else if (isLeftChild) {
parent.left = null
} else {
parent.right = null
}
}
// 2.2.删除的节点有一个子节点
else if (current.right == null) {
if (current == this.root) {
this.root = current.left
}
if (isLeftChild) {
parent.left = current.left
} else {
parent.right = parent.left
}
} else if (current.left == null) {
if (current == this.root) {
this.root = current.right
}
if (isLeftChild) {
parent.left = current.right
} else {
parent.right = current.right
}
}
// 2.3.删除的节点有两个子节点
}
};
var bst = new BinarySearchTree()
// 2.插入数据
bst.insert(11)
bst.insert(7)
bst.insert(15)
bst.insert(5)
bst.insert(3)
bst.insert(9)
bst.insert(8)
bst.insert(10)
bst.insert(13)
bst.insert(12)
bst.insert(14)
bst.insert(20)
bst.insert(18)
bst.insert(25)
bst.insert(6)
//3.测试遍历(先)
var resultString = "";
bst.preOrderTraversal(function(key) {
resultString += key + " "
})
// alert(resultString)
resultString
//中序遍历
var resultStringmid = "";
bst.midOrderTraser(function(key) {
resultStringmid += key + " "
})
// alert(resultString)
resultStringmid
//后序遍历
var resultStringpost = "";
bst.postOrderTraser(function(key) {
resultStringpost += key + " "
})
// alert(resultString)
resultStringpost
//求二叉搜索树的最值
console.log(bst.max())
console.log(bst.min())
//搜索方法
console.log(bst.search(6))
console.log(bst.search(24))