我正在尝试使用 Propel 的 NestedSet 功能。但是,我错过了一些关于插入的东西,这样树在创建时是平衡的(即水平填充它)。
假设我有这些元素:
root
r1c1 r1c2
r2c1 r2c2
我想插入 r2c3 作为 r1c2 的第一个孩子(即在第 3 行开始之前填充第 2 行)。
我的第一个尝试是创建这个函数:
function where(User $root,$depth=0)
{
$num = $root->getNumberOfDescendants();
if ( $num < 2 )
return $root;
foreach($root->getChildren() as $d)
{
if ( $d->getNumberOfChildren() < 2 )
{
return $d;
}
}
foreach($root->getChildren() as $d)
{
return where($d, $depth+1);
}
}
但是,这将在 r2c1 上插入一个孩子,而不是在我想要的 r1c2 上。
有没有办法以某种方式将条目插入到下一个可用位置的树中?
TIA 迈克