编辑代码

 
let myTreeData = [
    {
        value:'1',
        title:'一',
        children:[
            {
                value:'1-1',
                title:'一-一',
                children:[],
            },
            {
                value:'1-2',
                title:'一-二',
                children:[],
            }
        ],
    },
    {
        value:'2',
        title:'二',
        children:[
            {
                value:'2-1',
                title:'二-一',
                children:[
                    {
                        value:'2-1-1',
                        title:'二-一-一',
                        children:[],
                    },
                    {
                        value:'2-1-1',
                        title:'二-一-二',
                        children:[],
                    }
                ],
            }
        ],
    },
    {
        value:'3',
        title:'三',
        children:[],
    },
]
//1.遍历打印
//广度优先(BFS)
function BFSConsole(treeData){
    let tempArr = [];
    treeData && treeData.map((item)=>{
        console.log("*",item.value)
        if(item.children && item.children.length>0){
            tempArr = [...tempArr, ...item.children];
        }
    })
    console.log(tempArr)
    tempArr && tempArr.length>0 && BFSConsole(tempArr);
}

//深度优先(DFS)
function DFSConsole(treeData){
    treeData && treeData.map((item)=>{
        console.log("*",item.value)
        if(item.children && item.children.length>0){
            BFSConsole(item.children);
        }
    })
}
console.log("BFS:")
BFSConsole(myTreeData)
console.log("DFS:")
DFSConsole(myTreeData)

//2.搜索指定code的元素
//广度优先搜索
function BFSQueryByCode1(treeData,code,queryResult = []){
    let tempArr = [];
    treeData && treeData.map((item)=>{
        if(item.value === code){
            queryResult.push(item);
        }
        if(item.children && item.children.length>0){
            tempArr = [...tempArr, ...item.children];
        }
    })
    tempArr && tempArr.length>0 && BFSQueryByCode1(tempArr,code,queryResult);
}
var queryResult = []
BFSQueryByCode1(myTreeData,'2-1',queryResult)
//广度优先搜索
function BFSQueryByCode(treeData,code){
    let tempArr = [];
    let queryResult = [];
    treeData && treeData.map((item)=>{
        if(item.value === code){
            queryResult.push(item);
        }
        if(item.children && item.children.length>0){
            tempArr = [...tempArr, ...item.children];
        }
    })
    if(tempArr && tempArr.length>0){
        queryResult = [...queryResult, ...BFSQueryByCode(tempArr,code)];
    }
    return queryResult
}
console.log("BFS Query:")
console.log(BFSQueryByCode(myTreeData,'2-1-1'))

//深度优先搜索
function DFSQueryByCode(treeData,code){
    let queryResult = [];
    treeData && treeData.map((item)=>{
        if(item.value === code){
            queryResult = [...queryResult,item];
        }else if(item.children && item.children.length>0){
            queryResult = [...queryResult,...DFSQueryByCode(item.children,code)];
        }
    })
    return queryResult;
}
console.log("DFS Query:")
console.log(DFSQueryByCode(myTreeData,'2-1-1'))