JavaScript数据结构:二叉搜索树

创建二叉搜索树

// 创建BinarySearchTree
function BinarySerachTree() {
    // 创建结点构造函数
    function Node(key) {
        this.key = key
        this.left = null
        this.right = null
    }
    
    // 保存根的属性
    this.root = null
    
}

二叉搜索树的操作

  • insert(key):向树中插入一个新的键。
  • search(key):在树中查找一个键,如果结点存在,则返回true;如果不存在,则返回false
  • inOrderTraverse:通过中序遍历方式遍历所有结点。
  • preOrderTraverse:通过先序遍历方式遍历所有结点。
  • postOrderTraverse:通过后序遍历方式遍历所有结点。
  • min:返回树中最小的值/键。
  • max:返回树中最大的值/键。
  • remove(key):从树中移除某个键。
insert(key):

向树中插入一个新的键。

// 向树中插入数据
BinarySerachTree.prototype.insert = function (key) {
    // 1.根据key创建对应的node
    var newNode = new Node(key)
    
    // 2.判断根结点是否有值
    if (this.root === null) {
        this.root = newNode
    } else {
        this.insertNode(this.root, newNode)
    }
}

BinarySerachTree.prototype.insertNode = function (node, newNode) {
    if (newNode.key < node.key) { // 1.准备向左子树插入数据
        if (node.left === null) { // 1.1.node的左子树上没有内容
            node.left = newNode
        } else { // 1.2.node的左子树上已经有了内容
            this.insertNode(node.left, newNode)
        }
    } else { // 2.准备向右子树插入数据
        if (node.right === null) { // 2.1.node的右子树上没有内容
            node.right = newNode
        } else { // 2.2.node的右子树上有内容
            this.insertNode(node.right, newNode)
        }
    }
}
先序遍历
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)
    }
}

这里的handler是一个函数,是在测试代码时传入的函数,目的是保存每次遍历要打印的key

测试代码:

// 测试前序遍历结果
var resultString = ""
bst.preOrderTraversal(function (key) {
    resultString += key + " "
})
alert(resultString) // 11 7 5 3 6 9 8 10 15 13 12 14 20 18 25

在这里插入图片描述

中序遍历
// 中序遍历
BinarySerachTree.prototype.inOrderTraversal = function (handler) {
    this.inOrderTraversalNode(this.root, handler)
}

BinarySerachTree.prototype.inOrderTraversalNode = function (node, handler) {
    if (node !== null) {
        this.inOrderTraversalNode(node.left, handler)
        handler(node.key)
        this.inOrderTraversalNode(node.right, handler)
    }
}

测试代码:

// 测试中序遍历结果
resultString = ""
bst.inOrderTraversal(function (key) {
    resultString += key + " "
})
alert(resultString) // 3 5 6 7 8 9 10 11 12 13 14 15 18 20 25 

在这里插入图片描述

后序遍历
// 后续遍历
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)
    }
}

测试代码:

// 测试后续遍历结果
resultString = ""
bst.postOrderTraversal(function (key) {
    resultString += key + " "
})
alert(resultString) // 3 6 5 8 10 9 7 12 14 13 18 25 20 15 11 
最大值&最小值
  • 代码依次向左找到最左边的结点就是最小值,
  • 代码依次向右找到最右边的结点就是最大值.
// 获取最大值和最小值
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
}
删除节点
删除节点的思路
  • 删除节点要从查找要删的节点开始, 找到节点后, 需要考虑三种情况:

    • 该节点是叶子结点(没有子节点)
    • 该节点有一个子节点
    • 该节点有两个子节点

    查找要删除的节点

    current就是要删除的节点,parent指向的是要删除节点的父节点,isLeftChild表示要删除的节点是parent节点的左孩子还是右孩子

// 删除结点
BinarySerachTree.prototype.remove = function (key) {
    // 1.定义临时保存的变量
    var current = this.root
    var parent = null
    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
    }
    //结束循环后,此时current就是要删除的节点,parent指向的是要删除节点的父节点
    

    //后面的删除操作
    //...
}

情况一: 没有子节点

  • 需要检测current的left以及right是否都为null.
  • 都为null之后还要检测:是否current就是根, 左右子节点都为null, 那么相当于要清空二叉树
  • 如果current有左子节点或右子节点,就把父节点的left或者right字段设置为null即可.
// 3.删除的结点是叶结点
if (current.left === null && current.right === null) {
    if (current == this.root) { //current是根节点
        this.root == null
    } else if (isLeftChild) {  //current是左孩子
        parent.left = null
    } else {  //current是右孩子
        parent.right = null
    }
}

情况二: 一个子节点

要删除的current结点, 只有2个连接, 一个连接父节点, 一个连接唯一的子节点.

需要从这三者之间: 爷爷 - 自己 - 儿子, 将自己(current)剪短, 让爷爷直接连接儿子即可.

这个过程要求改变父节点的left或者right, 指向要删除节点的子节点.

当然, 在这个过程中还要考虑是否current就是根.如果current是根,就让根变成current的左孩子或者右孩子

// 4.删除有一个子节点的节点
else if (current.right === null) {  
    //此时current没有右孩子,下面一定是将current的左孩子(current.left)赋值给parent节点
    
    if (current == this.root) {
        this.root = current.left
    } else if (isLeftChild) { //此时current在parent的左边,那么current的孩子也在parent的左边
        parent.left = current.left
    } else {  //此时current在parent的右边,那么current的孩子也在parent的右边
        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节点下面所有节点中最接近current节点的.
    • 要么比current节点小一点点, 要么比current节点大一点点.
    • 总结就是最接近current, 可以用来替换current的位置.
  • 这个节点的寻找:
    • 比current小一点点的节点, 一定是current左子树的最大值.
    • 比current大一点点的节点, 一定是current右子树的最小值.
  • 前驱&后继
    • 而在二叉搜索树中, 这两个特别的节点:
    • 比current小一点点的节点, 称为current节点的前驱.
    • 比current大一点点的节点, 称为current节点的后继.
  • 也就是为了能够删除有两个子节点的current, 要么找到它的前驱, 要么找到它的后继.

这里以寻找后继为例

寻找后继的代码:

// 找后继的方法
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
}

找到后序节点后的删除节点的代码(该节点有两个子节点):

// 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
}

删除节点的完整代码

// 删除结点
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.如果是删除(后继节点有父节点和子节点)的情况, 还需要如下代码
    if (successor != delNode.right) {
        successorParent.left = successor.right
        successor.right = delNode.right
    }
    
    return successor
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛小y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值