
二叉树结构 统计每一个叶子节点下的节点的数量
发布日期:2021-05-09 04:17:09
浏览次数:17
分类:博客文章
本文共 7594 字,大约阅读时间需要 25 分钟。
类如下:
query( 'set names ' . DB_CHAR ); // $sql = 'select * from ub_user_expand'; // $result=$mysqli->query($sql); // $array=array(); // while( $rows=$result->fetch_assoc() ) // { // $r['uid'] = $rows['uid']; // $r['name'] = $rows['username']; // $r['_referee'] = $rows['_referee']; // $r['_left'] = $rows['_left']; // $r['_right'] = $rows['_right']; // $r['position'] = $rows['position']; // $this->arr_all[] = $r; // } // print_r($this->arr_all);die(); $this->arr_all[0] = array('uid' => '10000000', '_referee' => '','name' => '101001', '_left' => '10000001', '_right' => '10000002', 'position' => '' ); $this->arr_all[1] = array('uid' => '10000001', '_referee' => '101001', 'name' => '102001', '_left' => '10000003', '_right' => '10000004', 'position' => 1 ); $this->arr_all[2] = array('uid' => '10000002', '_referee' => '101001', 'name' => '102002', '_left' => '10000005', '_right' => '10000006', 'position' => 2 ); $this->arr_all[3] = array('uid' => '10000003', '_referee' => '102001', 'name' => '103001', '_left' => '10000007', '_right' => '10000008', 'position' => 1 ); $this->arr_all[4] = array('uid' => '10000004', '_referee' => '102001', 'name' => '103002', '_left' => '10000009', '_right' => '10000010', 'position' => 2 ); $this->arr_all[5] = array('uid' => '10000005', '_referee' => '102002', 'name' => '103003', '_left' => '10000011', '_right' => '10000012', 'position' => 1 ); $this->arr_all[6] = array('uid' => '10000006', '_referee' => '102002', 'name' => '103004', '_left' => '10000013', '_right' => '10000014', 'position' => 2 ); $this->arr_all[7] = array('uid' => '10000007', '_referee' => '103001', 'name' => '104001', '_left' => '10000015', '_right' => '10000016', 'position' => 1 ); $this->arr_all[8] = array('uid' => '10000008', '_referee' => '103001', 'name' => '104002', '_left' => '10000017', '_right' => '10000018', 'position' => 2 ); $this->arr_all[9] = array('uid' => '10000009', '_referee' => '103002', 'name' => '104003', '_left' => '10000019', '_right' => '10000020', 'position' => 1 ); $this->arr_all[10] = array('uid' => '10000010', '_referee' => '103002', 'name' => '104004', '_left' => '10000021', '_right' => '10000022', 'position' => 2 ); $this->arr_all[11] = array('uid' => '10000011', '_referee' => '103003', 'name' => '104005', '_left' => '10000023', '_right' => '10000024', 'position' => 1 ); $this->arr_all[12] = array('uid' => '10000012', '_referee' => '103003', 'name' => '104006', '_left' => '10000025', '_right' => '10000026', 'position' => 2 ); $this->arr_all[13] = array('uid' => '10000013', '_referee' => '103004', 'name' => '104007', '_left' => '10000027', '_right' => '10000028', 'position' => 1 ); $this->arr_all[14] = array('uid' => '10000014', '_referee' => '103004', 'name' => '104008', '_left' => '10000029', '_right' => '10000030', 'position' => 2 ); $this->arr_all[15] = array('uid' => '10000015', '_referee' => '104001', 'name' => '105001', '_left' => '', '_right' => '', 'position' => 1 ); $this->arr_all[16] = array('uid' => '10000016', '_referee' => '104001', 'name' => '105002', '_left' => '', '_right' => '', 'position' => 2 ); $this->arr_all[17] = array('uid' => '10000017', '_referee' => '104002', 'name' => '105003', '_left' => '', '_right' => '', 'position' => 1 ); $this->arr_all[18] = array('uid' => '10000018', '_referee' => '104002', 'name' => '105004', '_left' => '', '_right' => '', 'position' => 2 ); $this->arr_all[19] = array('uid' => '10000019', '_referee' => '104003', 'name' => '105005', '_left' => '', '_right' => '', 'position' => 1 ); $this->arr_all[20] = array('uid' => '10000020', '_referee' => '104003', 'name' => '105006', '_left' => '', '_right' => '', 'position' => 2 ); $this->arr_all[21] = array('uid' => '10000021', '_referee' => '104004', 'name' => '105007', '_left' => '', '_right' => '', 'position' => 1 ); $this->arr_all[22] = array('uid' => '10000022', '_referee' => '104004', 'name' => '105008', '_left' => '', '_right' => '', 'position' => 2 ); $this->arr_all[23] = array('uid' => '10000023', '_referee' => '104005', 'name' => '105009', '_left' => '', '_right' => '', 'position' => 1 ); $this->arr_all[24] = array('uid' => '10000024', '_referee' => '104005', 'name' => '105010', '_left' => '', '_right' => '', 'position' => 2 ); $this->arr_all[25] = array('uid' => '10000025', '_referee' => '104006', 'name' => '105011', '_left' => '', '_right' => '', 'position' => 1 ); $this->arr_all[26] = array('uid' => '10000026', '_referee' => '104006', 'name' => '105012', '_left' => '', '_right' => '', 'position' => 2 ); $this->arr_all[27] = array('uid' => '10000027', '_referee' => '104007', 'name' => '105013', '_left' => '', '_right' => '', 'position' => 1 ); $this->arr_all[28] = array('uid' => '10000028', '_referee' => '104007', 'name' => '105014', '_left' => '', '_right' => '', 'position' => 2 ); $this->arr_all[29] = array('uid' => '10000029', '_referee' => '104008', 'name' => '105015', '_left' => '', '_right' => '', 'position' => 1 ); $this->arr_all[30] = array('uid' => '10000030', '_referee' => '104008', 'name' => '105016', '_left' => '', '_right' => '', 'position' => 2 ); // 循环处理数据 获取 叶子节点 foreach ( $this->arr_all as $key => $value ) { $this->arr_init[$value['name']] = $value; if ( empty( $value['_left'] ) && empty( $value['_right'] ) ) { $this->arr_leaf[] = $value; } } foreach ( $this->arr_leaf as $key => $value ) { $this->getUserNumInit( $this->arr_init, $value, 0 ); } print_r( $this->arr_res ); } public function getUserNumInit( $all, $leaf, $count_last ) { if ( !empty( $leaf ) && is_array( $leaf ) && !empty($leaf['_referee']) ) { // echo ''.$leaf['name'].''; $count = 0; $count_self = 1; $count_all = $count_self + $count_last; $res_key = $leaf['_referee']; $this->arr_res[$res_key]['uid'] = $this->arr_init[$leaf['_referee']]['uid']; $this->arr_res[$res_key]['uname'] = $res_key; if ( $leaf['position'] == 'left' ) { $this->arr_res[$res_key]['numLeft'] = $count_all; $count = $count_all; if ( isset( $this->arr_res[$res_key]['numRight']) ) { $count = $this->arr_res[$res_key]['numLeft'] + $this->arr_res[$res_key]['numRight']; } } elseif ( $leaf['position'] == 'right' ) { $this->arr_res[$res_key]['numRight'] = $count_all; $count = $count_all; if ( isset( $this->arr_res[$res_key]['numLeft']) ) { $count = $this->arr_res[$res_key]['numLeft'] + $this->arr_res[$res_key]['numRight']; } } else { return false; } // 获取上级、然后递归 $referee = isset($all[$leaf['_referee']])&&!empty($leaf['_referee']) ? $all[$leaf['_referee']] : ''; if ( !empty( $referee ) ) { $this->getUserNumInit( $all, $referee, $count ); } } }}
使用
\(obj = new getUserLeftRightNumber();\)obj->getUserNum();
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年04月13日 21时17分24秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
使用Azure Functions玩转Serverless
2019-03-06
.NET Core 基于Websocket的在线聊天室
2019-03-06
使用MySQL Shell创建MGR
2019-03-06
win10新版wsl2使用指南
2019-03-06
spring-boot 使用hibernate validation对参数进行优雅的校验
2019-03-06
关于我
2019-03-06
数据结构实验之栈四:后缀式求值
2019-03-06
图结构练习——最小生成树(prim算法(普里姆))
2019-03-06
sdut 2498【aoe 网上的关键路径】
2019-03-06
【PHP自定义显示系统级别的致命错误和用户级别的错误】
2019-03-06
【PHP对XML文件的操作技术【完整版】】
2019-03-06
【Java环境变量的配置问题】
2019-03-06
【JAVA多线程中使用的方法】
2019-03-06
【JAVA网络流之URL】
2019-03-06
最通俗易懂的囚徒困境
2019-03-06
递推的思维构建与技巧实现
2019-03-06
五道逻辑思维题
2019-03-06
liteide错误: 进程无法启动--解决方法
2019-03-06
Java程序中的代理作用和应用场景及实现
2019-03-06