二叉搜索树是一颗二叉树, 可以为空;如果不为空,满足以下性质:
class Node { constructor(key) { // 创建结点构造函数 this.key = key this.left = null this.right = null } } class BinarySearchTree { // 保存根的属性 this.root = null }复制成功
class Node { constructor(key) { this.key = key this.left = null this.right = null } } class BinarySearchTree { root = null //插入数据,对外暴露的方法 insert(key) { let newNode = new Node(key) //this.root存在? if(!this.root) { this.root = newNode }else { // 递归开始 this.insertNode(this.root, newNode) } } //在递归的过程中,插入的数据要么往左走,要么往右走。总能插入中作叶子元素的 insertNode(node, newNode) { let { key, left,right } = node if(newNode.key < key) { //向左查找 if(node.left === null) { // 递归结束 node.left = newNode }else { // 递归继续 this.insertNode(node.left, newNode) } }else if(newNode.key > key){ //向右查找 if(node.right === null) { node.right = newNode }else { this.insertNode(node.right, newNode) } }else { console.error(`键值相等`) } } }复制成功
// 测试代码 class Node { constructor(key) { this.key = key this.left = null this.right = null } } class BinarySearchTree { root = null //插入数据,对外暴露的方法 insert(key) { let newNode = new Node(key) //this.root存在? if(!this.root) { this.root = newNode }else { // 递归开始 this.insertNode(this.root, newNode) } } //在递归的过程中,插入的数据要么往左走,要么往右走。总能插入中作叶子元素的 insertNode(node, newNode) { let { key, left,right } = node if(newNode.key < key) { //向左查找 if(node.left === null) { // 递归结束 node.left = newNode }else { // 递归继续 this.insertNode(node.left, newNode) } }else if(newNode.key > key){ //向右查找 if(node.right === null) { node.right = newNode }else { this.insertNode(node.right, newNode) } }else { console.error(`键值相等`) } } }复制成功
三种遍历方式:

// 先序遍历 BinarySerachTree.prototype.preOrderTraversal = function (handler) { this.preOrderTranversalNode(this.root, handler) } BinarySerachTree.prototype.preOrderTranversalNode = function (node, handler) { if (node !== null) { // 1.打印当前经过的节点 handler(node.key) // 2.遍历所有的左子树 this.preOrderTranversalNode(node.left, handler) // 3.遍历所有的右子树 this.preOrderTranversalNode(node.right, handler) } } // 中序遍历 BinarySerachTree.prototype.inOrderTraversal = function (handler) { this.inOrderTraversalNode(this.root, handler) } BinarySerachTree.prototype.inOrderTraversalNode = function (node, handler) { if (node !== null) { this.inOrderTraversalNode(node.left, handler) // 2.打印当前经过的节点 handler(node.key) this.inOrderTraversalNode(node.right, handler) } } // 后序遍历 BinarySerachTree.prototype.postOrderTraversal = function (handler) { this.postOrderTraversalNode(this.root, handler) } BinarySerachTree.prototype.postOrderTraversalNode = function (node, handler) { if (node !== null) { this.postOrderTraversalNode(node.left, handler) this.postOrderTraversalNode(node.right, handler) handler(node.key) } }复制成功
二叉搜索树的最左边的值和最右边的值。用循环,一直向左找,一直向右找。

// 获取最大值和最小值 BinarySerachTree.prototype.min = function () { var node = this.root while (node.left !== null) { node = node.left } return node.key } BinarySerachTree.prototype.max = function () { var node = this.root while (node.right !== null) { node = node.right } return node.key }复制成功
// 搜索特定的值-递归 BinarySerachTree.prototype.search = function (key) { return this.searchNode(this.root, key) } BinarySerachTree.prototype.searchNode = function (node, key) { // 1.如果传入的node为null那么, 那么就退出递归 if (node === null) { return false } // 2.判断node节点的值和传入的key大小 if (node.key > key) { // 2.1.传入的key较小, 向左边继续查找 return this.searchNode(node.left, key) } else if (node.key < key) { // 2.2.传入的key较大, 向右边继续查找 return this.searchNode(node.right, key) } else { // 2.3.相同, 说明找到了key return true } }复制成功
// 搜索特定的值-循环 BinarySerachTree.prototype.search = function (key) { var node = this.root while (node !== null) { if (node.key > key) { node = node.left } else if (node.key < key) { node = node.right } else { // 找到了匹配的叶节点 return true } } // 找到某一个叶节点,仍未找到特定的值 return false }复制成功
// BinarySerachTree.prototype.remove // 希望删除,需要用三个变量 var current = this.root var parent = this.root var isLeftChild = true // 循环 while (current.key !== key) { parent = current if (key < current.key) { isLeftChild = true current = current.left } else { isLeftChild = false current = current.right } // 如果发现current已经指向null, 那么说明没有找到要删除的数据 if (current === null) return false // 得到三个变量,已查出目标current节点。... }复制成功
// 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 } else if (isLeftChild) { parent.left = current.left } else { parent.right = current.left } } else if (current.left === null) { if (current == this.root) { this.root = current.right } else if (isLeftChild) { parent.left = current.right } else { parent.right = current.right } }复制成功
规律总结:如果要删除的节点有两个子节点,甚至子节点还有子节点,这种情况下需要从要删除节点下面的子节点中找到一个合适的节点,来替换当前的节点。
若用 current 表示需要删除的节点,则合适的节点指的是:
删除这个节点的思路:

// 2.3.删除有两个节点的节点 else { // 1.获取后继节点 var successor = this.getSuccessor(current) // 2.判断是否是根节点 if (current == this.root) { this.root = successor } else if (isLeftChild) { parent.left = successor } else { parent.right = successor } // 3.将删除节点的左子树赋值给successor successor.left = current.left }复制成功
// 找后继的方法 // (循环,从current.right开始一直迭代current/successor等三个变量,一直往左找最小值) BinarySerachTree.prototype.getSuccessor = function (delNode) { // 1.使用变量保存临时的节点 var successorParent = var successor = delNode var current = delNode.right // 要从右子树开始找 // 2.寻找节点 while (current != null) { successorParent = successor successor = current current = current.left } // 3.如果是删除图中15的情况, 还需要如下代码 // ps:此时后继节点是18 // 18!=20 // 后继节点父亲的left指针不再指向后继节点,指向 后继节点.right(图中的19) // 后继节点的right指针数据已存好,改为指向 delNode.right(图中的20) if (successor != delNode.right) { successorParent.left = successor.right successor.right = delNode.right } return successor }复制成功
getSuccessor()后继节点图注:

// 删除结点 BinarySerachTree.prototype.remove = function (key) { // 1.定义临时保存的变量 var current = this.root var parent = this.root var isLeftChild = true // 2.开始查找节点 while (current.key !== key) { parent = current if (key < current.key) { isLeftChild = true current = current.left } else { isLeftChild = false current = current.right } // 如果发现current已经指向null, 那么说明没有找到要删除的数据 if (current === null) return false } // 3.删除的结点是叶结点 if (current.left === null && current.right === null) { if (current == this.root) { this.root == null } else if (isLeftChild) { parent.left = null } else { parent.right = null } } // 4.删除有一个子节点的节点 else if (current.right === null) { if (current == this.root) { this.root = current.left } else if (isLeftChild) { parent.left = current.left } else { parent.right = current.left } } else if (current.left === null) { if (current == this.root) { this.root = current.right } else if (isLeftChild) { parent.left = current.right } else { parent.right = current.right } } // 5.删除有两个节点的节点 else { // 1.获取后继节点 var successor = this.getSuccessor(current) // 2.判断是否是根节点 if (current == this.root) { this.root = successor } else if (isLeftChild) { parent.left = successor } else { parent.right = successor } // 3.将删除节点的左子树赋值给successor successor.left = current.left } return true } // 找后继的方法 BinarySerachTree.prototype.getSuccessor = function (delNode) { // 1.使用变量保存临时的节点 var successorParent = delNode var successor = delNode var current = delNode.right // 要从右子树开始找 // 2.寻找节点 while (current != null) { successorParent = successor successor = current current = current.left } // 3.如果是删除图中15的情况, 还需要如下代码 if (successor != delNode.right) { successorParent.left = successor.right successor.right = delNode.right } return successor }复制成功