## 深度遍歷優先與廣度遍歷優先的區別
* 深度遍歷優先,先找子孫后代,再找兄弟, 有回溯(入棧彈棧)操作,所以速度慢些
* 廣度遍歷優先,先找兄弟,再找子孫,無回溯速度快,但是多了隊列的存儲,占用內存會大些
<br>
## 深度遍歷優先
```
var tree = [{
name: '文件夾0',
expand: false,
children: [{
name: '文件夾2',
expand: false,
children: [{
name: '文件夾4',
expand: false,
}, {
name: '文件夾5',
expand: false,
children: [{
name: '文件夾6',
expand: false,
children: [{
name: '文件夾7',
expand: false,
children: [{
name: '文件夾8',
expand: false,
children: [{
name: '文件夾9',
expand: false,
children: [{
name: '文件夾10',
expand: false,
children: [{
name: '文件夾11',
expand: false,
children: [{
name: '文件夾12',
expand: false,
children: [{
name: '文件夾13',
expand: false,
children: [{
name: '文件夾14',
expand: false,
children: [{
name: '文件夾15',
expand: false
}]
}]
}]
}]
}]
}]
}]
}]
}]
}]
}]
},
{
name: '文件夾3',
expand: false
}
]
},
{
name: '文件夾1',
expand: false
}
]
// 深度遍歷優先
function expandAll(tree) {
tree.forEach(node => {
node.expand = true;
if (node.children && Array.isArray(node.children) && node.children.length > 0) {
expandAll(node.children);
}
});
}
// 開始計算t1時間
console.time('t1');
for (let i = 0; i < 10000; i++) {
expandAll(tree);
}
// t1時間計算結束
console.timeEnd('t1');
console.log(tree);
```
<br>
## 廣度優先遍歷
```
var tree = [{
name: '文件夾0',
expand: false,
children: [{
name: '文件夾2',
expand: false,
children: [{
name: '文件夾4',
expand: false,
}, {
name: '文件夾5',
expand: false,
children: [{
name: '文件夾6',
expand: false,
children: [{
name: '文件夾7',
expand: false,
children: [{
name: '文件夾8',
expand: false,
children: [{
name: '文件夾9',
expand: false,
children: [{
name: '文件夾10',
expand: false,
children: [{
name: '文件夾11',
expand: false,
children: [{
name: '文件夾12',
expand: false,
children: [{
name: '文件夾13',
expand: false,
children: [{
name: '文件夾14',
expand: false,
children: [{
name: '文件夾15',
expand: false
}]
}]
}]
}]
}]
}]
}]
}]
}]
}]
}]
},
{
name: '文件夾3',
expand: false
}
]
},
{
name: '文件夾1',
expand: false
}
]
// 廣度遍歷優先
function expandAll(tree) {
let queue = [];
tree.forEach(node => {
queue.push(node);
});
for(;queue.length > 0;){
let node = queue.shift();
node.expand = true;
if(Array.isArray(node.children) && node.children.length > 0){
node.children.forEach(child => {
queue.push(child);
});
}
}
}
console.time('t2');
for (let i = 0; i < 10000; i++) {
expandAll(tree);
}
console.timeEnd('t2');
console.log(tree);
```
- 初級前端題
- 必會
- http協議
- 跨域
- cookie與storage
- 移動端問題
- 性能優化
- Vue全家桶
- 有哪些常用的es6語法?
- 項目
- 閉包
- JSON
- 數據類型與運算
- 數組
- DOM
- 字符串
- 要會
- async與await
- 正則
- this
- 數據加密
- 實時獲取數據
- 原生ajax
- 異步打印
- css相關
- 雜七雜八
- webpack
- 一般
- mvvm模式
- 異步請求
- XSS
- 其他dom問題
- 冷門
- 瀏覽器緩存機制
- 新
- 瀏覽器事件輪詢
- Promise
- 樹的深度優先與廣度優先
- 拷貝
- 繼承
- Vue
- 跨域
- 排序
- 瀏覽器
- 瀏覽器入門
- 瀏覽器內核知識
- 瀏覽器渲染原理
- 瀏覽器性能調優
- 自動化構建
- 字符編碼
- git
- 一些題目
- 其他
- 邏輯思維題
- 互聯網公司招聘信息如何閱讀
- bat面試