算法 --- > 二叉树查找树的先序(中序、后序)遍历的js实现

结点:

function Node(data, left, right) {
    this.data = data;
    this.left = left;
    this.right = right;
    this.show = show;
}

显示树的数据:

function show(){
    return this.data;
}

二叉查找树:

// Binary Search Tree
function BST(){
    this.root = null;
    this.insert = insert;
}

添加结点到二叉树:

function insert(data){
    let n = new Node(data, null, null)
    if(this.root == null){
        this.root = n;
    }else{
        let current = this.root;
        let parent;
        while(true){
            parent = current;
            if(data < current.data){
                current = current.left;
                if(current == null){
                    parent.left = n;
                    breakk
                }
            }
            else{
                current = current.right
                if(current == null){
                    parent.right = n;
                    break;
                }
            }
        }
    }
}

生成二叉查找树:

function genBST(list){
    if(list.length>0){
        let t = new BST();
        list.forEach((data)=>{
            t.insert(data);
        })
        return t
    }
}
let list = [2,3,4,1];
console.log(genBST(list));

在这里插入图片描述

先序遍历:

function DLR(t){
    if(t.root !== undefined){
        console.log(t.root.data);
        if(t.root.left !== null){
            DLR(t.root.left)
        }
        if(t.root.right!==null){
            DLR(t.root.right)
        }
    }else{
        if(t !== null){
            console.log(t.data);
            if(t.left !== null){
               DLR(t.left)
            }
            if(t.right!==null){
               DLR(t.right)
            }
        }
    }
}


let list = [1,2,3,6,5,4];
let t = genBST(list);
DLR(t);

在这里插入图片描述
中序遍历:

function LDR(t){
    if(t.root !== undefined){
        if(t.root.left !==null){
            LDR(t.root.left)
        }
        console.log(t.root.data);
        if(t.root.right !== null){
            LDR(t.root.right)
        }
    }else{
        if(t !==null){
            if(t.left !== null){
                LDR(t.left);
            }
            console.log(t.data);
            if(t.right !== null){
                LDR(t.right);
            }
        }
    }
}

let list = [1,2,3,6,5,4];
let t = genBST(list);
LDR(t);

在这里插入图片描述
后续遍历:

function LRD(t){
    if(t.root !== undefined){
        if(t.root.left !==null){
            LRD(t.root.left)
        }
        if(t.root.right !== null){
            LRD(t.root.right)
        }
        console.log(t.root.data);
    }else{
        if(t !==null){
            if(t.left !== null){
                LRD(t.left);
            }
            if(t.right !== null){
                LRD(t.right);
            }
            console.log(t.data);
        }
    }
}

let list = [1,2,3,6,5,4];
let t = genBST(list);
LRD(t);

在这里插入图片描述

参考https://github.com/zoro-web/blog/issues/4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值