JavaScript深度优先查找(DFS)和广度优先查找(BFS)

1.深度优先查找(DFS)

在这里插入图片描述
这个是要进行遍历的树形节点

// 
const root = [
    {
        "id":1,
        "pid":null,
        "key":1,
        "title":"1-1111",
        "children":[
            {
                "id":2,
                "pid":1,
                "key":2,
                "title":"2-1111",
                "children":[
                    {
                        "id":4,
                        "pid":2,
                        "key":4,
                        "title":"8-1111",
                        "children":[]
                    },
                    {
                        "id":5,
                        "pid":2,
                        "key":5,
                        "title":"9-1111",
                        "children":[
                            {
                                "id":7,
                                "pid":5,
                                "key":7,
                                "title":"5-1111",
                                "children":[]
                            },
                            {
                                "id":8,
                                "pid":5,
                                "key":8,
                                "title":"8-1111",
                                "children":[]
                            },
                        ]
                    },
                    {
                        "id":6,
                        "pid":2,
                        "key":6,
                        "title":"10-1111",
                        "children":[]
                    },
                ]
            },
            {
                "id":3,
                "pid":1,
                "key":3,
                "title":"9-1111",
                "children":[]
            }
        ]
    }
]

方法1.递归回溯

function depthFirstSearchRecursion (target) {
    const res = []; // 存放结果
    const dfs=(data)=> {
        data.forEach(node=> {
            res.push(node.id); // 将当前节点 id 存放进结果
            // 如果当前节点有子节点,则递归调用
            if (node.children && node.children.length > 0) {
                dfs(node.children);
            }
        })
    }
    dfs(target);
    return res;
}

方法2.栈结构

function depthFirstSearchNoRecursion (target) {
    const res = [];
    const stack = target.slice();
    // console.log(stack)
    while (stack.length > 0) {
        const node = stack.shift(); // 最上层节点出栈
        res.push(node.id);
        // 如果该节点有子节点,将子节点存入栈中,继续下一次循环
        const len = node.children && node.children.length;
        for (let i = len - 1; i >= 0; i--) {
            stack.unshift(node.children[i]);
        }
    }
    return res;
}

2. 广度优先查找(BFS)

在这里插入图片描述

采用队列,先进先出

function breadthFirstSearch(target) {
    const res = [];
    const queue = target.slice();
    while (queue.length > 0) {
        const node = queue.shift(); // 最上层节点出栈
        res.push(node.id);
        // 如果该节点有子节点,将子节点存入队列中,继续下一次循环
        const len = node.children && node.children.length;
        for (let i = 0; i < len; i++) {
            queue.push(node.children[i])
        }
    }
    return res;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值