当前位置:网站首页>js中树形结构的深度遍历与广度遍历
js中树形结构的深度遍历与广度遍历
2022-07-13 18:59:00 【前端三脚猫】
树形结构的深度遍历与广度遍历
定义
深度遍历:一个树形结构中,由一个数据分支全部遍历完才去遍历另外一个分支,直至全部数据遍历完成。
广度遍历:先遍历最外层的分支数据,然后一层一层的进行深入遍历,直至全部数据遍历完成。
树形数据
const nodeList = [
{
id: 1,
children: [
{
id: 3,
children: [
{
id: 6,
children: [
{ id: 9 }
]
},
{
id: 7
}
]
},
{
id: 4,
children: [
{ id: 8 }
]
},
{
id: 5
}
]
},
{
id: 2
}
]
详细描述
深度遍历: 如上树形数据结构nodeList,深度遍历的时候首先遍历到id为1的节点,如果有children子节点就继续往下遍历(如id为3数据),直至把id为1分支上的数据全部遍历完成后才去遍历另外分支上的数据(如id为2节点),然后再id为2的分支上也执行之前操作。
广度遍历:如上树形数据结构nodeList,首先遍历最外层数据(首先是id为1,然后是id为2的数据),最外层遍历完成后再去遍历往里一层的数据(如id为3,然后id为4的数据),直至当前层数据遍历完毕,接着去遍历在网下一层数据,直至所有数据遍历完毕。
使用场景
在日常开发中经常会遇到多级菜单或者树形下拉框等,可能需要给数据增加自己的标识和字段,还有要查找某项数据的id传给后端,再或者执行平铺等操作,都会涉及到树形数据的处理。
深度遍历
深度遍历的实现可以使用递归的方式,也可以借用栈的思想特点(先入后出,只能在栈顶进行操作)来实现。因为我们日常开发的树形层级不会嵌套特别多,所以采用哪种方式都可以,但是如果遇到层级特别深的递归会有很大的性能问题,最好采用栈思想来实现。代码实例:
// 深度遍历
const each = data => {
let stack = [...data], node;
while (node = stack.shift()) {
console.log(node.id);
// 如果有子元素的话进行压栈操作
if (node.children) stack.unshift(...node.children)
}
}
each(nodeList)
// 打印数据顺序
// 1
// 3
// 6
// 9
// 7
// 4
// 8
// 5
// 2
广度遍历
广度遍历的实现可以借用队列的思想(先进先出)来实现。实例代码如下:
// 广度遍历
const each = data => {
let node, list = [...data];
while (node = list.shift()) {
console.log(node.id);
// 如果有子元素就放入队列
node.children && list.push(...node.children);
}
}
each(nodeList);
// 打印数据顺序
// 1
// 2
// 3
// 4
// 5
// 6
// 7
// 8
// 9
边栏推荐
- win10系统如何实现开机启动程序?用shell:startup命令
- Response. Write specific introduction
- The mental journey of a sealer maintainer
- Test basis 1
- How to manage win11 local users and groups? Win11 method of creating user administrator
- 红柳林,煤矿里挖出了数据金矿!
- 不刷新页面内容,改变浏览器访问地址url
- [play with FPGA learning 7 in simple terms ----- cross clock domain signal processing based on FPGA]
- 1. Create SAP OData project in SAP ABAP transaction code segw
- What happens when you unplug the power? Gaussdb (for redis) dual life keeps you prepared
猜你喜欢

Win11怎么共享文件夹?Win11创建共享文件夹的方法

Where is the win11 uninstaller? Two methods of uninstalling software in win11

Test basis 4
![[go] Ⅱ. Introduction à l'API reposante et au processus et à la structure du Code de l'API](/img/fd/8ae3d6a4c0d0c973ce81672c1c529c.png)
[go] Ⅱ. Introduction à l'API reposante et au processus et à la structure du Code de l'API

电脑定时清理微信数据
![[day 2] machine reading comprehension -- common machine reading comprehension models (Part 1)](/img/bf/0fd972f8c1749a0cd1964b633fc5e3.png)
[day 2] machine reading comprehension -- common machine reading comprehension models (Part 1)

Could not connect to redis at 192.168.164.118:6379: connection rejected under Linux

Win11本地用户和组怎么管理?Win11创建用户管理员的方法

VRRP基础配置

No one really thinks that chatting robots are difficult -- fine tune the Bert model to get the similarity between sentences
随机推荐
Redis的持久化机制、过期策略、淘汰策略
News: JD technology released the "ten billion income plan"; The president of Broadcom software business resigned
Test basis 1
[day 2] machine reading comprehension -- common machine reading comprehension models (Part 1)
Web安全-ReDos正则表达式的拒绝服务攻击
Record a detailed penetration test of a site
VRRP基础配置
华为交换机SEP双半环设计方案及配置详细步骤
HCIP第二个实验
程序员转型项目管理?考个PMP?
win11触控板用不了怎么办?win11触控板用不了的解决方法
Virtualization path of GPU resource pool
密码密钥硬编码检查
模块
Burpsite v2.1 Chinese version
Lesson 3: take coins
Taishan Office Technology Lecture: the height of strange times New Roman Fonts
有没有完全自主的国产化数据库技术
Test basis 2
不会真有人觉得聊天机器人难吧——微调BERT模型得到句子间的相似度