JavaScript

超轻量级php框架startmvc

JavaScript树的深度优先遍历和广度优先遍历算法示例

更新时间:2020-07-18 11:36:01 作者:startmvc
本文实例讲述了JavaScript树的深度优先遍历和广度优先遍历算法。分享给大家供大家参考,

本文实例讲述了JavaScript树的深度优先遍历和广度优先遍历算法。分享给大家供大家参考,具体如下:

1、深度优先遍历的递归写法


function deepTraversal(node) {
 var nodes = [];
 if (node != null) {
 nodes.push(node);
 var children = node.children;
 for (var i = 0; i < children.length; i++)
 deepTraversal(children[i]);
 }
 return nodes;
}

2、深度优先遍历的非递归写法


function deepTraversal(node) {
 var nodes = [];
 if (node != null) {
 var stack = [];
 stack.push(node);
 while (stack.length != 0) {
 var item = stack.pop();
 nodes.push(item);
 var children = item.children;
 for (var i = children.length - 1; i >= 0; i--)
 stack.push(children[i]);
 }
 }
 return nodes;
}

3、广度优先遍历的递归写法:

报错:Maximum call stack size exceeded(…)


function wideTraversal(node) {
 var nodes = [];
 var i = 0;
 if (!(node == null)) {
 nodes.push(node);
 wideTraversal(node.nextElementSibling);
 node = nodes[i++];
 wideTraversal(node.firstElementChild);
 }
 return nodes;
}

4、广度优先遍历的非递归写法


function wideTraversal(selectNode) {
 var nodes = [];
 if (selectNode != null) {
 var queue = [];
 queue.unshift(selectNode);
 while (queue.length != 0) {
 var item = queue.shift();
 nodes.push(item);
 var children = item.children;
 for (var i = 0; i < children.length; i++)
 queue.push(children[i]);
 }
 }
 return nodes;
}

JavaScript 深度优先遍历 广度优先遍历
基础信息
请求
SQL查询
加载文件
配置
错误
总耗时: 88.64ms 内存: 87.09KB
请求方法: GET
请求URI: /article_35678.html
控制器: index
方法: index
PHP版本: 8.0.26
服务器: nginx/1.24.0
GET参数
无GET参数
POST参数
无POST参数
请求头
Cookie: PHPSESSID=ao03eue2i7dcqnekemkmk5tksj
Accept-Encoding: gzip, br, zstd, deflate
User-Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Accept: */*
Host: startmvc.com
Content-Length:
Content-Type:
Cookie
PHPSESSID: ao03eue2i7dcqnekemkmk5tksj
SELECT id,name FROM sm_article_category 0.00ms
SELECT a.id,a.category_id,a.title,a.keywords,a.description,a.update_time,ac.content FROM sm_article as a JOIN sm_article_content as ac ON ac.article_id = a.id WHERE a.id = '35678' LIMIT 1 0.00ms
SELECT tag_id FROM sm_tag_article WHERE article_id = '35678' 0.03ms
select id,title from sm_article where id in (select article_id from sm_tag_article where tag_id=9853) and status=1 order by id desc limit 20 0.00ms
总计:4 条SQL语句 总耗时:0.03 ms
1. /www/wwwroot/startmvc.com/public/index.php
2. /www/wwwroot/startmvc.com/startmvc/boot.php
3. /www/wwwroot/startmvc.com/startmvc/function.php
4. /www/wwwroot/startmvc.com/vendor/autoload.php
5. /www/wwwroot/startmvc.com/vendor/composer/autoload_real.php
6. /www/wwwroot/startmvc.com/vendor/composer/platform_check.php
7. /www/wwwroot/startmvc.com/vendor/composer/ClassLoader.php
8. /www/wwwroot/startmvc.com/vendor/composer/autoload_static.php
9. /www/wwwroot/startmvc.com/startmvc/core/Config.php
10. /www/wwwroot/startmvc.com/config/common.php
11. /www/wwwroot/startmvc.com/startmvc/core/Session.php
12. /www/wwwroot/startmvc.com/startmvc/core/App.php
13. /www/wwwroot/startmvc.com/startmvc/core/Exception.php
14. /www/wwwroot/startmvc.com/function/custom_function.php
15. /www/wwwroot/startmvc.com/function/global_function.php
16. /www/wwwroot/startmvc.com/startmvc/core/Request.php
17. /www/wwwroot/startmvc.com/startmvc/core/Middleware.php
18. /www/wwwroot/startmvc.com/startmvc/core/Router.php
19. /www/wwwroot/startmvc.com/config/route.php
20. /www/wwwroot/startmvc.com/app/home/controller/ArticleController.php
21. /www/wwwroot/startmvc.com/app/common/BaseController.php
22. /www/wwwroot/startmvc.com/startmvc/core/Controller.php
23. /www/wwwroot/startmvc.com/startmvc/core/Loader.php
24. /www/wwwroot/startmvc.com/startmvc/core/View.php
25. /www/wwwroot/startmvc.com/config/view.php
26. /www/wwwroot/startmvc.com/startmvc/core/Db.php
27. /www/wwwroot/startmvc.com/config/database.php
28. /www/wwwroot/startmvc.com/startmvc/core/db/DbCore.php
29. /www/wwwroot/startmvc.com/startmvc/core/db/DbInterface.php
30. /www/wwwroot/startmvc.com/runtime/temp/home/article/detail.php
31. /www/wwwroot/startmvc.com/startmvc/core/tpl/trace.php
总计:31 个文件
系统配置
运行环境: development
调试模式: 开启
时区设置: Asia/Shanghai
最大执行时间: 300秒
内存限制: 128M
上传限制: 50M
POST限制: 50M
字符集: UTF-8
错误报告级别: 32759
缓存状态
APC: 不可用
Memcached: 不可用
Redis: 不可用
OPcache: 可用
XCache: 不可用
File Cache: 可用
Autoload: 1 个加载器
Session: 已激活
框架配置
session: [Array]
trace: 1
debug: 1
default_controller: Index
default_action: index
异常信息
未捕获到异常
错误信息
未发现错误
应用运行正常,未发现错误或异常
88.64ms