树是什么

*一种分层数据的抽象模型

*前端工作中常见的树包括:DOM树、级联选择、树形控件....

*js中没有树,但是可以用Object和Array构建树

*树的常用操作:深度/广度优先遍历、先中后序遍历

什么是深度/广度优先遍历

*深度优先遍历:进可能深的搜索树的分支

*广度优先遍历:先访问离根节点最近的节点

深度优先遍历算法口诀

1访问根节点

2对根节点的children挨个进行深度优先遍历

复制代码
const tree ={
    val :'a',
    children:[{
        val :'b',
        children:[{
            val :'d',
            children:[]
        },{
            val :'e',
            children:[]
        }]
    },{
        val :'c',
        children:[{
            val :'f',
            children:[]
        },{
            val :'g',
            children:[]
        }]
    }]
}
const dfs=(root)=>{
    console.log(root.val)
    root.children.forEach(dfs)
}
dfs(tree)
复制代码

 

广度优先遍历算法

1新建一个队列,把根节点入队

2吧队头出队并访问

3把队头的children挨个入队

4重复第二,三步,直到队列为空

复制代码
const tree ={
    val :'a',
    children:[{
        val :'b',
        children:[{
            val :'d',
            children:[]
        },{
            val :'e',
            children:[]
        }]
    },{
        val :'c',
        children:[{
            val :'f',
            children:[]
        },{
            val :'g',
            children:[]
        }]
    }]
}
const bfs=(root)=>{
    const q =[root]
    while (q.length>0){
        const  n = q.shift()
        console.log(n.val)
        n.children.forEach(child=>{
            q.push(child)
        })
    }
}
bfs(tree)
复制代码

 二叉树是什么

*树中每个节点最多只能有2个子节点

*在js中通常用Object来模拟二叉树

 

 

先序遍历算法口诀

1访问根节点

2对根节点的左子树进行先序遍历

3对根节点的右子树进行先序遍历

复制代码
const  bt =require('./bt')
const inorder=(root)=>{
    if(!root)return;
console.log(root.val)
preorder(root.left)
preorder(root.right)
} inorder(bt)
//递归版如口诀一样执行
复制代码
复制代码
const  preorder=(root)=>{
    if(!root) return;
    const stack=[root];

    while (stack.length){
        const n=stack.pop();
        console.log(n.val)
        if(n.right) stack.push(n.right)
        if(n.left) stack.push(n.left)
    }

}
//非递归版,用栈模拟js函数运行
复制代码

 

 

中序遍历算法口诀

1对根节点的左子树进行中序遍历

2访问根节点

3对根节点的右子树进行中序遍历

复制代码
const bt=require('./bt')
const postorder=(root)=>{
     if(!root)return ;
inorder(root.left)
console.log(root.val)
inorder(root.right)
} postorder(bt)
复制代码
复制代码
const inorder=(root)=>{
    if(!root)return;
    const stack=[]
    let p =root
    while (stack.length||p){
        while (p){
            stack.push(p)
            p=p.left
        }
        const  n = stack.pop();
        console.log(n.val)
        p = n .right;
    }
}
inorder(bt)
//使用栈和指针的方法的来模拟递归版中序遍历,所以指针和栈同时未空,才算是遍历结束。
复制代码

 

 

后序遍历算法口诀

1对根节点的左子树进行后序遍历

2对根节点的右子树进行后序遍历

3访问根节点

复制代码
const bt =require('./bt')

const  preorder=(root)=>{
    if(!root) return;
postorder(root.left)
postorder(root.right)
console.log(root.val)
} preorder(bt)
复制代码
复制代码
const postorder=(root)=>{
    if(!root)return ;
    const outputStack=[]
    const stack=[root];
    while (stack.length){
        const n=stack.pop();
        outputStack.push(n)
        if(n.left) stack.push(n.left)
        if(n.right) stack.push(n.right)
    }//先模拟先序遍历然后,改变左右的顺序。
    while (outputStack.length){
        const  n = outputStack.pop()
        console.log(n.val)
    }//然后再用栈放出去
}
postorder(bt)
//后序遍历麻烦,采用模拟先序遍历然后倒一下,再遍历。
复制代码

 

bt.js

复制代码
const bt={
    val :1,
    left:{
        val :2,
        left:{
            val:4,
            left :null,
            right:null
        },
        right:{
            val:5,
            left :null,
            right:null
        }
    },
    right:{
        val:3,
        left:{
            val:6,
            left :null,
            right:null
        },
        right:{
            val:7,
            left :null,
            right:null
        }
    }
}
module.exports=bt
复制代码

 技术要点

+树是一种分层数据的抽象模型,在前端广泛应用(dom,级联选择)

+树的常用操作:深度、广度优先遍历,先中后序遍历(面试中使用)

posted @   前端路远且长  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示