如何递归求出从根节点出发的每一条分支线上的所有节点(有先后顺序,除去返回父节点的节点)

kel 2006-09-08 11:53:51
假设有一个节点关系数组如下:
$allnode = array
(
'0' => array
(
'from' => 3349,
'to' => 3350
),

'1' => array
(
'from' => 3349,
'to' => 3352
),

'2' => array
(
'from' => 3350,
'to' => 3622
),

'3' => array
(
'from' => 3351,
'to' => 3623
),

'4' => array
(
'from' => 3352,
'to' => 3628
),

'5' => array
(
'from' => 3353,
'to' => 3631
),

'6' => array
(
'from' => 3354,
'to' => 3355
),

'7' => array
(
'from' => 3355,
'to' => 3356
),

'8' => array
(
'from' => 3622,
'to' => 3350
),

'9' => array
(
'from' => 3622,
'to' => 3351
),

'10' => array
(
'from' => 3622,
'to' => 4346
),

'11' => array
(
'from' => 3623,
'to' => 3351
),

'12' => array
(
'from' => 3623,
'to' => 3352
),

'13' => array
(
'from' => 3628,
'to' => 3352
),

'14' => array
(
'from' => 3628,
'to' => 3353
),

'15' => array
(
'from' => 3631,
'to' => 3353
),

'16' => array
(
'from' => 3631,
'to' => 3354
),

'17' => array
(
'from' => 4346,
'to' => 4347
),

'18' => array
(
'from' => 4347,
'to' => 3352
),

'19' => array
(
'from' => 4347,
'to' => 4346
)

);
$node_rows = array();
$node_from = array();
$flow_branch = array();//每一条分支上的节点
$first_id = 3349;//起始节点
function getBranch($next_id){
global $node_rows,$node_from,$allnode;
array_push($node_from, $next_id);
foreach($allnode as $key1=>$arr){
if($arr['from'] == $next_id){
if(!in_array($arr['to'],$node_from)){
array_push($node_rows,$arr['to']);
getBranch($arr['to']);
}
}
}
return $node_rows;
}

foreach($allnode as $key2=>$arr2){
if($first_id == $arr2['from']){
$node_rows = array($first_id,$arr2['to']);
$node_from = array();
$next_row = getBranch($arr2['to']);
if(!in_array($next_row,$flow_branch)){
$flow_branch[] = $next_row;
}
}
}
print_r($flow_branch);
这样只求出了从起点分叉的分支节点,没求出从中间分叉的节点。
...全文
223 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
kel 2006-09-12
  • 打赏
  • 举报
回复
虚心学习
颓废的老猫 2006-09-11
  • 打赏
  • 举报
回复
学习
xuzuning 2006-09-11
  • 打赏
  • 举报
回复
不过是个图的遍历问题

function branch($from, &$nodes, $flow=array()) {
static $ret;
if(empty($flow)) $ret = array();
if(in_array($from, $flow)) {
$flow[] = -$from; //有环路
$ret[] = $flow;
return;
}
$flow[] = $from;
$t = array_filter($nodes, create_function('$v', "return \$v['from'] == $from;"));
if($t) {
foreach($t as $v) {
branch($v['to'], $nodes, $flow);
}
}else {
$ret[] = $flow;
// return;
}
return $ret;
}

print_r(branch(3349, $allnode));
iasky 2006-09-09
  • 打赏
  • 举报
回复
mark
kel 2006-09-09
  • 打赏
  • 举报
回复
我是想求出从起点开始,一直到没有下节点的所有分支线上的节点。如果有两条分支,其中深度相同,而那个节点又不是终节点。这样子就会有多余的分支节点。还要另外加条件才能实现,多谢楼上的答疑
johnpanq 2006-09-08
  • 打赏
  • 举报
回复
应该除去返回所有上级节点的节点,否则会出现死循环。

function getBranch($node,$depth=1)
{
global $allnode,$flow_branch,$branch;

//当前节点分支数
$sub_branch = 0;
for ($i=0;$i<count($allnode);$i++)
{

$v = $allnode[$i];
if ($node == $v['from'])
{
//分支不存在时,初始化该分支数组
if (!isset($flow_branch[$branch]))
{
$flow_branch[$branch] = array();
//初始化根结点
array_push($flow_branch[$branch], $node);
}
//当前节点有2个以上的子结点时,要添加新的分支。
if ($sub_branch > 0)
{
$branch++;
//新分支与上一分支在当前深度以上,节点是相同的
$flow_branch[$branch] = array_slice($flow_branch[$branch-1],0,$depth);
}
//下一节点不在当前分支时,才将节点存入分支数组
if (!in_array($v['to'],$flow_branch[$branch]))
{

array_push($flow_branch[$branch], $v['to']);
//当前节点的子节点数+1
$sub_branch++;
//寻找下一节点的子节点,深度+1
getBranch($v['to'],$depth+1);
}
}
}
}

$flow_branch = array(); //分支数组
$branch=0; //分支数
$start_node=3349; //起点
getBranch($start_node);
echo "<pre>";
print_r($flow_branch);
echo "</pre>";

21,889

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧