什么是深度优先和广度优先
1、概念:深度优先是自上而下的遍历搜索,广度优先则是逐层遍历。比如,现有如下树结构:
graph TD;
1((1))-->2-1
1((1))-->2-2
1((1))-->2-3
2-1((2-1))-->3-1((3-1))
2-1((2-1))-->3-2((3-2))
2-2((2-2))-->3-3((3-3))
2-3((2-3))-->3-4((3-4))
graph LR;
1((1))-->2-1
2-1((2-1))-->3-1((3-1))
3-1((3-1))-->3-2((3-2))
3-2((3-2))-->2-2((2-2))
2-2((2-2))-->3-3((3-3))
3-3((3-3))-->2-3((2-3))
2-3((2-3))-->3-4((3-4))
graph LR;
1((1))-->2-1
2-1((2-1))-->2-2((2-2))
2-2((2-2))-->2-3((2-3))
2-3((2-3))-->3-1((3-1))
3-1((3-1))-->3-2((3-2))
3-2((3-2))-->3-3((3-3))
3-3((3-3))-->3-4((3-4))
代码实现
1、现有如下树结构数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| const tree = { data: { no: 1 }, children: [ { data: { no: 2 .1 }, children: [ { data: { no: 3.1 }, children: [] }, { data: { no: 3.2 }, children: [] } ] }, { data: { no: 2.2 }, children: [ { data: { no: 3.3 }, children: [] } ] }, { data: { no: 2.3 }, children: [ { data: { no: 3.4 }, children: [] } ] }, ] }
|
1 2 3 4 5 6 7 8 9 10 11 12
| function breadthFirst (data) { let result = []; result.push(data.data.no); function recursion (data) { data.forEach(item => { result.push(item.data.no); item.children && recursion(item.children); }) } recursion(data.children); return result.join(','); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| function depthFirst (data) { let result = []; let queue = data; result.push(queue.data.no); while (queue.children.length > 0) { const first = queue.children[0]; result.push(first.data.no); first.children && (queue.children.push(...first.children)); queue.children.shift(); } return result.join(','); }
|