js二叉树的建立与中序遍历

二叉树概念

1.除了最下面一层,每个节点都是父节点,每个节点都有且最多有两个子节点;

2.除了最上面一层,每个节点是子节点,每个节点都会有一个父节点;

3.最上面一层的节点为根节点;

图例说明:

中序遍历概念

先打印左子树(左子节点),接着打印父节点,最后打印右子树(右子节点)

 

 代码实现:

 //创建二叉树
        function Node(data,left,right){
            this.data = data;
            this.left = left;
            this.right = right;
        }
        Node.prototype.show = function(){
            return this.data;
        }
        function BST(){
            this.root = null;
        }
        BST.prototype.insert = function(data){
            var node = new Node(data,null,null);
            if(this.root == null){
                this.root = node;
            }else{
                var current = this.root;
                var parent;
                while(true){
                    parent = current;
                    if(data < current.data){
                        current = current.left;
                        if(current == null){
                            parent.left = node;
                            break;
                        }
                    }else{
                        current = current.right;
                        if(current == null){
                            parent.right = node;
                            break;
                        }
                    }
                }
            }
        }
        //二叉树中序遍历
        BST.prototype.inOrder = function(node){
            if(node){
                this.inOrder(node.left);
                console.log(node.show() + " ");
                this.inOrder(node.right);
            }
        }
        //测试数据
        var bst  = new BST();
        var nums = [10,3,18,2,4,13,21,9,8,9];
        for(var i = 0;i < nums.length;i ++){
            bst.insert(nums[i]);
        }
        console.log(bst.root)
        bst.inOrder(bst.root);
//定义节点
    class Node {
        constructor(data) {
            this.root = this;
            this.data = data;
            this.left = null;
            this.right = null;
        }
    }
    //创建二叉搜索树(BST))
    class BinarySearchTree {
        constructor() {
            this.root = null;
        }
        //插入节点
        insert(data) {
            const newNode = new Node(data);
            const insertNode = (node, newNode) => {
                if (newNode.data < node.data) {
                    if (node.left === null) {
                        node.left = newNode;
                    } else {
                        insertNode(node.left, newNode);
                    }
                } else {
                    if (node.right === null) {
                        node.right = newNode;
                    } else {
                        insertNode(node.right, newNode);
                    }

                }
            };
            if (!this.root) {
                this.root = newNode;
            } else {
                insertNode(this.root, newNode);

            }
        }
        //中序遍历
        inOrder() {
            let backs = [];
            const inOrderNode = (node, callback) => {
                if (node !== null) {
                    inOrderNode(node.left, callback);
                    backs.push(callback(node.data));
                    inOrderNode(node.right, callback);
                }
            };
            inOrderNode(this.root, callback);

            function callback(v) {
                return v;
            }
            return backs;
        }
        //前序遍历
        preOrder() {
            let backs = [];
            const preOrderNode = (node, callback) => {
                if (node !== null) {
                    backs.push(callback(node.data));
                    preOrderNode(node.left, callback);
                    preOrderNode(node.right, callback);
                }
            };
            preOrderNode(this.root, callback);

            function callback(v) {
                return v;
            }
            return backs;
        }
        //后序遍历
        postOrder() {
            let backs = [];
            const postOrderNode = (node, callback) => {
                if (node !== null) {
                    postOrderNode(node.left, callback);
                    postOrderNode(node.right, callback);
                    backs.push(callback(node.data));
                }
            };
            postOrderNode(this.root, callback);

            function callback(v) {
                return v;
            }
            return backs;
        }
        //查找最小值
        getMin(node) {
            const minNode = node => {
                return node ? (node.left ? minNode(node.left) : node) : null;
            };
            return minNode(node || this.root);
        }
        //查找最大值
        getMax(node) {
            const minNode = node => {
                return node ? (node.right ? minNode(node.right) : node) : null
            };
            return minNode(node || this.root);
        }
        //查找特定值
        find(data) {
            const findNode = (node, data) => {
                if (node === null) return false;
                if (node.data === data) return node;
                return findNode((data < node.data) ? node.left : node.right, data);
            };
            return findNode(this.root, data);

        }
        //删除节点
        remove(data) {
            const removeNode = (node, data) => {
                if (node === null) return null;
                if (node.data === data) {
                    if (node.left === null && node.right === null) return null;
                    if (node.left === null) return node.right;
                    if (node.right === null) return node.left;
                    if (node.left !== null && node.right !== null) {
                        let _node = this.getMin(node.right);
                        node.data = _node.data;
                        node.right = removeNode(node.right, data);
                        return node;
                    }
                } else if (data < node.data) {
                    node.left = removeNode(node.left, data);
                    return node;
                } else {
                    node.right = removeNode(node.right, data);
                    return node;
                }
            };
            return removeNode(this.root, data);
        }
    }
    //创建BST
    const tree = new BinarySearchTree();
    tree.insert(11);
    tree.insert(7);
    tree.insert(5);
    tree.insert(3);
    tree.insert(9);
    tree.insert(8);
    tree.insert(10);
    tree.insert(13);
    tree.insert(12);
    tree.insert(14);
    tree.insert(20);
    tree.insert(18);
    tree.insert(25);
    console.log(tree);
    console.log(tree.root);
    //中序遍历BST
    console.log(tree.inOrder());
    //前序遍历BST
    console.log(tree.preOrder());
    //后序遍历BST
    console.log(tree.postOrder());
    //搜索最小值
    console.log(tree.getMin());
    //搜索最大值
    console.log(tree.getMax());
    //查找特定值
    console.log(tree.find(2));
    console.log(tree.find(3));
    console.log(tree.find(20));
    //删除节点,返回新的二叉树,不改变原来的二叉树
    console.log(tree.remove(11));
    a = tree.remove(11);
    console.log(a.root);
    console.log(tree);

 

原文

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

风儿轻丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值