I have a simple tree structure where node x can have y children.
Node x's children are stored in an array.
I want to supply a node to a function and count the TOTAL number of
children for that node.
This is what I have come up with so far:
function count_total_chi ldren($tree)
{
$num_of_c = $tree->get_num_child( );
echo $num_of_c.'+';
$child_array = $tree->get_child_arr( );
for($i=0;$i<$nu m_of_c;++$i)
{
count_total_chi ldren($child_ar ray[$]);
}
}
Now this clearly does not work but its the closest I've come. What
this bit of code does is if a node has 5 children it will output
something like:
1+2+0+0+2+
Rather than print out the number I need to add it to a running total,
but I just cant seem to do it!
Ive tried using references but it doesnt work when you do the
recursive call?
Any help very greatly welcomed!
Node x's children are stored in an array.
I want to supply a node to a function and count the TOTAL number of
children for that node.
This is what I have come up with so far:
function count_total_chi ldren($tree)
{
$num_of_c = $tree->get_num_child( );
echo $num_of_c.'+';
$child_array = $tree->get_child_arr( );
for($i=0;$i<$nu m_of_c;++$i)
{
count_total_chi ldren($child_ar ray[$]);
}
}
Now this clearly does not work but its the closest I've come. What
this bit of code does is if a node has 5 children it will output
something like:
1+2+0+0+2+
Rather than print out the number I need to add it to a running total,
but I just cant seem to do it!
Ive tried using references but it doesnt work when you do the
recursive call?
Any help very greatly welcomed!
Comment