Find a tree: DFS and BFS

Yesterday colleagues asked the sentence: "How do I find all of the child in accordance with the specified ID in a tree structure?"

For CURD's usually always in me still is a funny thing, I need to deal with is the structure of department

var arr = [
  {v: 1,child : [
    {v: 2,child : [
      {v: 3,child : [
        {v: 4,child : [
          {v: 5,child : [
            {v: 6,child : []},
          ]},
        ]},
      ]},{v: 7,child : [
        {v: 8,child : [
          {v: 9,child : [
            {v: 10,child : []},
          ]},
        ]},
      ]},
      {v:11,child : [
        {v: 12,child : [
          {v: 13,child : [
            {v: 14,child : [
              {v: 15,child : []},
            ]},
          ]},
        ]},
        {v: 16,child : [
          {v: 17,child : [
            {v: 18,child : []},
          ]},
        ]},
      ]},
      {v: 19,child : [
        {v: 20,child : []},
      ]},
    ]},
  ]},
]

Is a common depth and number of nodes child nodes are not definite structure.

Depth-first traversal (DFS) and breadth-first traversal (BFS)

Find this tree has two programs 深度优先and广度优先

Depth-first traversal: starting from the root node, traversing along the longitudinal direction of the left subtree until you locate the leaf nodes so far. Then go back to the previous node, traversing right subtree node until you traverse all nodes up to date.

Breadth first traversal: starting from the root, traversing the hierarchical tree of the longitudinal lateral traversing the segment based on the layer node tree.

Then realize the function

/**
 * 深度优先查找
 * @param {Array} array 要遍历的结构 
 * @param {Number} id 要寻找的
 * @param {String} str 记录路径,如果你不需要可以去掉
 */
function getchild_dfs(array,id,str){
  console.log(str);
  for (let i = 0; i < array.length; i++) {
    str += '->' + array[i]['v']; 
    if (array[i]['v'] === id) {
      return array[i].child; 
    }else{
      var res = getchild_dfs(array[i].child,id,str);
      if (res) {
        return res;
      }
    }
  }
  
}
/**
 * 广度优先
 * @param {Array} array 要遍历的结构 
 * @param {Number} id 要寻找的
 * @param {String} str 记录路径,如果你不需要可以去掉
 */
function getchild_bfs(array,id,str){
  
  var childs = [];
  console.log(str);
  for (let i = 0; i < array.length; i++) {
    str += '->' + array[i]['v']; 
    if (array[i]['v'] === id) {
      return array[i].child; 
    }else{
      childs = childs.concat(array[i].child);
    }
  }
 
  return getchild_bfs(childs,id,str);
}

const str = '0';
console.log('-------深度优先----------');
console.info(getchild_dfs(arr,17,str));
console.log('--------广度优先---------');
console.info(getchild_bfs(arr,17,str));

Print Results:

-------深度优先----------
0
0->1
0->1->2
0->1->2->3
0->1->2->3->4
0->1->2->3->4->5
0->1->2->3->4->5->6
0->1->2->3->7
0->1->2->3->7->8
0->1->2->3->7->8->9
0->1->2->3->7->8->9->10
0->1->2->3->7->11
0->1->2->3->7->11->12
0->1->2->3->7->11->12->13
0->1->2->3->7->11->12->13->14
0->1->2->3->7->11->12->13->14->15
0->1->2->3->7->11->12->16
[ { v: 18, child: [] } ]
--------广度优先---------
0
0->1
0->1->2
0->1->2->3->7->11->19
0->1->2->3->7->11->19->4->8->12->16->20
[ { v: 18, child: [] } ]

Guess you like

Origin www.cnblogs.com/Molyp/p/12069145.html