美文网首页Web
JS实现深度优先搜索

JS实现深度优先搜索

作者: 爱吃猫的老虎 | 来源:发表于2020-05-11 13:32 被阅读0次
const { isEmpty, find } = require('lodash');
const Tree = [
    {
        id: 1,
        parent: 0,
        children: [
            {
                id: 2,
                parent: 1,
            },
            {
                id: 3,
                parent: 1,
                children: [
                    {
                        id: 4,
                        parent: 3,
                    },
                    {
                        id: 5,
                        parent: 3,
                    },
                    {
                        id: 6,
                        parent: 3,
                        children: [
                            {
                                id: 7,
                                parent: 6,
                                children: [
                                    {
                                        id: 8,
                                        parent: 7,
                                    },
                                    {
                                        id: 9,
                                        parent: 7,
                                    },
                                ]
                            },
                            {
                                id: 10,
                                parent: 6
                            }
                        ]
                    },
                ]
            },
        ]
    }
];

const depthFirstSearch = (tree, condition, primaryKey) => {
    if (isNil(tree) || isNil(condition)) {
        return null;
    }
    const visitedNodeKeys = [];
    const stack = [];
    if (condition(tree[0])) {
        return tree[0];
    } else {
        visitedNodeKeys.push(tree[0][primaryKey]);
        stack.push(tree[0]);
    }
    while (!isEmpty(stack)) {
        const lastIndex = stack.length - 1;
        const endItem = stack[lastIndex];
        const endItemChildren = endItem.children || [];
        const notVisitedChild = find(endItemChildren, child => !some(visitedNodeKeys, key => isEqual(child[primaryKey], key)));
        if (notVisitedChild) {
            if (condition(notVisitedChild)) {
                return notVisitedChild;
            } else {
                visitedNodeKeys.push(notVisitedChild[primaryKey]);
                stack.push(notVisitedChild);
            }
        } else {
            stack.pop();
        }
    }
    return null;
};

console.log(depthFirstSearch (Tree , element => element.id === 9, 'id')));

相关文章

  • 深度优先广度优先

    深度优先搜索 广度优先搜索(队列实现)

  • JS实现深度优先搜索

  • 深度优先搜索

    深度优先搜索思路 深度优先搜索 = 回溯法可以通过遍历或者分治法的思想实现深度优先搜索而递归和迭代 (非递归)两种...

  • JS实现广度优先搜索与深度优先搜索

    图在数学及技术上的概念:一个图G = (V, E),由以下元素组成。 V: 一组顶点 E: 一组边,连接V中的...

  • leetcode第695题:岛屿的最大面积 [中等]

    题目描述 考点 深度优先搜索 递归 解题思路一:深度优先搜索,使用栈 代码实现 make_pair 构建pair;...

  • 算法与数据结构 之 搜索算法

    搜索分为广度优先搜索、深度优先搜索、A*算法。 一、广度优先算法(BFS) 1.1、基本实现和特性:BFS是从一个...

  • 5. 深度优先、广度优先

    1. 二叉树的深度优先遍历和广度优先遍历2. 深度优先搜索递归和非递归实现 深度优先(DFS):前序遍历 广度优先...

  • 图搜索算法实现

    图的深度优先搜索遍历和广度优先搜索遍历,深度优先搜索借助一个辅助栈实现,一直顺着路径往前走,每次都取出栈顶元素,一...

  • 学习回溯法中的思考

    回溯法是一种用递归来实现的穷举法,是深度搜索优先算法。 我又联想到了,深度搜索优先和广度搜索优先,与栈和队...

  • 搜索

    一、深度优先搜索 图深度优先遍历、深度优先搜索算法求有权图两点最短路径 二、广度优先搜索 图广度优先遍历、广度优先...

网友评论

    本文标题:JS实现深度优先搜索

    本文链接:https://www.haomeiwen.com/subject/iilunhtx.html