BST 搜索二叉树 JS 中序遍历 先序遍历 后序遍历 删除节点

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function BinarySearchtree() {
            function Node(key) {

                this.key = key;
                this.left = null;
                this.right = null;
            }
            // console.log("1")
            this.root = null
                //Bst方法
            BinarySearchtree.prototype.insert = function(key) {
                //根据key创建节点
                var newNode = new Node(key)
                    // console.log("s")
                    //判断是否有root
                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.search = function(key) {
                var node = this.root
                while (node != null) {
                    // console.log(1)
                    if (key < node.key) {
                        node = node.left
                    } else if (key > node.key) {
                        node = node.right
                    } else {
                        return true;
                        // console.log(1)
                    }

                }
                return false
            }
            BinarySearchtree.prototype.remove = function(key) {

                    //1。寻找删除的节点
                    // 1.1 定义变量,保存删除节点
                    var current = this.root
                    var parent = null
                    var 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
                        }
                    }

                    //2。删除
                    // 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
                        }
                    } else {
                        var successor = this.getSucessor(current)
                        if (current == this.root) {
                            this.root = successor
                        } else if (isLeftChild) {
                            parent.left = successor
                        } else {
                            parent.right = successor
                        }
                        //3.删除节点左子树
                        successor.left = current.left

                    }
                }
                //获取后继
            BinarySearchtree.prototype.getSucessor = function(delNode) {
                    var successor = delNode;
                    var current = delNode.right;
                    var successorParent = delNode
                    while (current != null) {
                        successor = current
                        current = current.left
                    }
                    //判断寻找后继是否为直接delNode的right
                    if (successor != delNode.right) {
                        successorParent.left = delNode.right
                        successor.right = delNode.right

                    }
                    return successor
                }
                //获取前ji
                // BinarySearchtree.prototype.getSucessor = function() {

            // }
            BinarySearchtree.prototype.preOrderTraveral = function(handler) {
                this.preOrderTraveralNode(this.root, handler)
            }
            BinarySearchtree.prototype.preOrderTraveralNode = function(node, handler) {
                if (node != null) {
                    handler(node.key)
                    this.preOrderTraveralNode(node.left, handler)
                    this.preOrderTraveralNode(node.right, handler)
                }
            }
            BinarySearchtree.prototype.midOrdertraveral = function(handler) {
                this.midOrdertraveralNode(this.root, handler)
            }
            BinarySearchtree.prototype.midOrdertraveralNode = function(node, handler) {
                if (node != null) {
                    this.midOrdertraveralNode(node.left, handler)
                    handler(node.key)
                    this.midOrdertraveralNode(node.right, handler)

                }
            }
            BinarySearchtree.prototype.LasOrdertraveral = function(handler) {
                this.LasOrdertraveralNode(this.root, handler)
            }
            BinarySearchtree.prototype.LasOrdertraveralNode = function(node, handler) {
                if (node != null) {
                    this.LasOrdertraveralNode(node.left, handler)

                    this.LasOrdertraveralNode(node.right, handler)
                    handler(node.key)
                }
            }

            BinarySearchtree.prototype.Max = function() {
                //1.获取根节点
                var node = this.root;
                //2.依次向又不断查找
                var key = null
                while (node != null) {
                    key = node.key
                    node = node.right
                }
                return key
            }
            BinarySearchtree.prototype.Min = function() {
                //1.获取根节点
                var node = this.root;
                //2.依次向又不断查找
                var key = null
                while (node != null) {
                    key = node.key
                    node = node.left
                }
                return key
            }
        }



        var bst = new BinarySearchtree()
        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(15);
        bst.insert(6)
            //遍历测试
        console.log(bst)

        //先序遍历
        var resultString = "";
        bst.preOrderTraveral(function(key) {
            resultString += key + " "
        })
        console.log("先序遍历=" + resultString)
            //中序遍历
        var resultString = "";
        bst.midOrdertraveral(function(key) {
            resultString += key + " "
        })
        console.log("中序遍历=" + resultString)
            //后续
        var resultString = "";
        bst.LasOrdertraveral(function(key) {
            resultString += key + " "
        })
        console.log("后序遍历=" + resultString)
            //最大
        console.log("最大=" + bst.Max())
            //最小
        console.log("最小=" + bst.Min())
            //测试搜索
        console.log(bst.search(23), bst.search(21), bst.search(2), bst.search(3), bst.search(13))
            //测试删除操作
        bst.remove(3)
        bst.remove(13)
        console.log(bst.search(23), bst.search(21), bst.search(2), bst.search(3), bst.search(13))
    </script>
</body>

</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

优价实习

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

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

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

打赏作者

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

抵扣说明:

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

余额充值