dorseysen / One-Date-One-Question

this is a space for self-improvement of mine
1 stars 0 forks source link

2019-08-15:BFS #100

Open dorseysen opened 5 years ago

dorseysen commented 5 years ago

请写一个简单的广度优先搜索(BFS)实现过程。

此处提供tree.js的树结构对象以供搜索。


export const Tree = [
    {
        id: "1",
        name: "xiaoming",
        children: [
            {
                id: "11",
                name: "xiaohua",
                children: [
                    {
                        id: "111",
                        name: "xiaohong",
                        children: [
                            {
                                id: "1111",
                                name: "xiaoqiang",
                                children: [
                                    {
                                        id: "11111",
                                        name: "xiaozhao"
                                    },
                                    {
                                        id: "11112",
                                        name: "xiaoxin"
                                    },
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                id: "12",
                name: "xiaotian",
                children: [
                    {
                        id: "121",
                        name: "xiaohe"
                    }
                ]
            },
            {
                id: "13",
                name: "xiaoli",
                children: [
                    {
                        id: "131",
                        name: "xiaobing",
                        children: [
                            {
                                id: "1311",
                                name: "xiaoqin"
                            }
                        ]
                    }
                ]
            },
        ]
    },
    {
        id: "2",
        name: "xiaoyi",
        children: [
            {
                id: "21",
                name: "xiaobi",
                children: [
                    {
                        id: "211",
                        name: "xiaosheng",
                        children: [
                            {
                                id: "2111",
                                name: "xiaoguang",
                                children: [
                                    {
                                        id: "21111",
                                        name: "xiaomi"
                                    },
                                    {
                                        id: "21112",
                                        name: "xiaoxing"
                                    },
                                ]
                            }
                        ]
                    }
                ]
            },
            {
                id: "22",
                name: "xiaozhong",
                children: [
                    {
                        id: "221",
                        name: "xiaoqing"
                    }
                ]
            },
            {
                id: "23",
                name: "xiaogua",
                children: [
                    {
                        id: "231",
                        name: "xiaoguai",
                        children: [
                            {
                                id: "2311",
                                name: "xiaolin"
                            }
                        ]
                    }
                ]
            },
        ]
    }
]
dorseysen commented 5 years ago

//  2019-08-15:BFS

//  请写一个简单的广度优先搜索(BFS)实现过程

import { Tree } from './tree';

class BFS {

    constructor (tree, target) {

        this.tree = tree;

        this.flag = true;

        this.count = 0;

        this.init(target);
    }

    init (target) {

        this.flag && this.breathFirst(this.tree, target);

        // console.log("this.count = " + this.count);
    }

    breathFirst (nodeList, target) {

        let nextNodeList = [];

        for(let item of nodeList) {

            this.count ++;

            if(item.id === target) {

                // console.log(item.name);

                this.name = item.name;

                this.flag = false;

                return;
            }

            if( !!item.children && item.children.length) {

                nextNodeList.push(...item.children);
            }
        }

        this.flag && nextNodeList.length && this.breathFirst(nextNodeList, target);
    }
}
return new BFS(Tree, '1111').name;