php教程

超轻量级php框架startmvc

PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法

更新时间:2020-03-25 07:59:00 作者:startmvc
本文实例讲述了PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法。分享给大家

本文实例讲述了PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法。分享给大家供大家参考,具体如下:

先来看看前序遍历、中序遍历与后序遍历原理图:

根据树的前序遍历和中序遍历构造树并输出后序遍历代码如下:


<?php
class BinaryTreeNode{
 public $m_value;
 public $m_left;
 public $m_right;
}
function ConstructCore($preorder,$inorder){
 if(count($preorder)!=count($inorder) || count($preorder)==0 || count($inorder)==0)
 return null;
 $headNode=new BinaryTreeNode;
 $headNode->m_value=$preorder[0];
 if(count($preorder)==1){
 $headNode->m_left=null;
 $headNode->m_right=null;
 return $headNode;
 }
 array_shift($preorder);
 $pos=array_search($headNode->m_value,$inorder);
 $leftin=array_slice($inorder,0,$pos);
 $rightin=array_slice($inorder,$pos+1);
 $leftpre=array_slice($preorder,0,$pos);
 $rightpre=array_slice($preorder,$pos);
 $headNode->m_left=ConstructCore($leftpre,$leftin);
 $headNode->m_right=ConstructCore($rightpre,$rightin);
 return $headNode;
}
$pre=array(1,2,4,7,3,5,6,8);
$in=array(4,7,2,1,5,3,8,6);
$tree=ConstructCore($pre,$in);
function tail($tree){
 if($tree->m_right!=null)
 echo tail($tree->m_right);
 if($tree->m_left!=null)
 echo tail($tree->m_left);
 echo $tree->m_value;
}
tail($tree);
?>

运行结果:


86537421
PHP 前序遍历 中序遍历 构造树 输出 后序遍历
基础信息
请求
SQL查询
加载文件
配置
错误
总耗时: 91.88ms 内存: 88.00KB
请求方法: GET
请求URI: /article_7263.html
控制器: index
方法: index
PHP版本: 8.0.26
服务器: nginx/1.24.0
GET参数
无GET参数
POST参数
无POST参数
请求头
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
无Cookie数据
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 = '7263' LIMIT 1 0.00ms
SELECT tag_id FROM sm_tag_article WHERE article_id = '7263' 0.03ms
select id,title from sm_article where id in (select article_id from sm_tag_article where tag_id=9326) 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
异常信息
未捕获到异常
错误信息
未发现错误
应用运行正常,未发现错误或异常
91.88ms