js二叉树的遍历算法

访客4年前黑客资讯1113

二叉树的观点

二叉树是非常重要的数据结构,其中一棵树最上面的点称为根节点,若是一个节点下面毗邻多个节点,那么该节点称为父节点,下面的节点称为子节点,二叉树的每一个节点最多有2个子节点,一个节点子节点的个数称为度,二叉树每个节点的度只能是0,1,2中的一个,度为0的节点称为叶节点。


js二叉树的实现

用 *** 实现二叉树数据结构, 完成遍历、查找更大/小值、查找特定值以及删除节点的操作。  

//界说节点
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);


思源资源网:分类流动

1.阿里云: 本站现在使用的是阿里云主机,平安/可靠/稳固。点击领取2000米代金券、领会最新阿里云产物的种种优惠流动点击进入

相关文章

广州两黑客,火线精英黑客软件,黑客入侵网站违反吗

1.77 47.10 b) 高档:(-v 2)你或许会想知道为什么咱们不让程序员直接拜访内存,而是增加这一层抽象化的内存。 直接拜访内存将会翻开一些安全漏洞。 。 全世界的巨...

荣耀9耳机多少钱,荣耀耳机多少钱

  你好。先用耳机插头沾上一两滴酒精(最好是无水酒精),然后插入耳机孔里,快速插拔十来次。若无效,说明耳机孔内部的触点没有正常复位,耳机排线很自制,十来块。   耳机线很自制,本钱价较低。一般10元阁...

诚信黑客先做事后付款(知道手机号怎么搞一个人24小时接单的黑客QQ)

  我被骗,在网上找了一个网络黑客帮助,又被网络黑客骗了,我很惨。有诚实守信网络黑客先办事后支付的那类吗?24小时在线等!   先而言说我上当受骗的历经:   前前任吧,哪个死花心男,说着我也来气。他...

黑客王继刚,找黑客帮忙破解qq,网站黑客手机摄像头

triplec.com.lb 一、网络安全态势总述……略Google Play上韩国公交车使用系列软件,被使用进犯特定方针(host="DESKTOP-2C3IQHO"full...

齐齐哈尔公司取名大全(13个高大上名字)

洋气两个字齐齐哈尔公司取名名字大全打分 纵步齐齐哈尔公司   7.64分   三废齐齐哈尔公司   50.89分   新闻齐齐哈尔公司   65.82分   辩题齐齐哈尔公司   13.39分...

微信聊天记录对话框删除了怎么恢复(微信聊天会话怎么恢复)

微信聊天记录对话框删除了怎么恢复(微信聊天会话怎么恢复)微信聊天记录删除了怎么恢复?:recover为啥恢复不了?如何找回误删的微信聊天记录,相信现在很多人都在使用微信进行工作上或是生活上的消息传递,...