473,326 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Counting nodes in a tree

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_children($tree)
{
$num_of_c = $tree->get_num_child();
echo $num_of_c.'+';
$child_array = $tree->get_child_arr();

for($i=0;$i<$num_of_c;++$i)
{
count_total_children($child_array[$]);
}
}

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!
Jul 17 '05 #1
8 4051
* bi****@hotmail.com (David) wrote:
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_children($tree)
{
$num_of_c = $tree->get_num_child();
$child_array = $tree->get_child_arr(); $num_of_sub_c = 0; for($i=0;$i<$num_of_c;++$i)
{ $num_of_sub_c += count_total_children($child_array[$]); } return num_of_c + num_of_sub_c; }


echo "Childrens: " + count_total_children($tree);

hth mabu

ps: just a short think about - errors could happen ;)

--
Are you Anonymous? Where? ... I don't think so ...

[ devnull{at}chaosfactory{dot}org | http://www.chaosfactory.org/ ]
Jul 17 '05 #2
function count_total_children($tree) {
$num_of_c = $tree->get_num_child();
echo $num_of_c.'+';
$child_array = $tree->get_child_arr();

for($i=0;$i<$num_of_c;++$i) {
count_total_children($child_array[$]);
}
}

You mean this is your code ? Should not work at all as count_total_children
is looped inside count_total_children

Savut

"David" <bi****@hotmail.com> wrote in message
news:3e**************************@posting.google.c om...
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_children($tree)
{
$num_of_c = $tree->get_num_child();
echo $num_of_c.'+';
$child_array = $tree->get_child_arr();

for($i=0;$i<$num_of_c;++$i)
{
count_total_children($child_array[$]);
}
}

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!


Jul 17 '05 #3
> You mean this is your code ? Should not work at all as count_total_children
is looped inside count_total_children


Ever hear of a little thing named recursion?
Jul 17 '05 #4
If we forget errors for the time being this does not work:
function count_total_children($tree)
{
$num_of_c = $tree->get_num_child();
**echo $num_of_c;
$child_array = $tree->get_child_arr();
$num_of_sub_c = 0;
for($i=0;$i<$num_of_c;++$i)
{
$num_of_sub_c += count_total_children($child_array[$]);
}
return num_of_c + num_of_sub_c;
}
echo "Childrens: " + count_total_children($tree);


This simply returns 0 every time. HOWEVER if we echo to the screen
$num_of_c seen at **, thie output the numbers we are trying to add up
e.g. 1+0+3...

This means the function is working but we are losing track these
variable as we go through.

The problem remains how do we keep track of these values then return
them at the end???
Jul 17 '05 #5
bi****@hotmail.com (David) wrote in
news:3e**************************@posting.google.c om:
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.
I think you mean the total number of *descendants*; when talking about tree
structures, "children" refers only to the *immediate* descendants of a
node.
This is what I have come up with so far:

function count_total_children($tree)
{
$num_of_c = $tree->get_num_child();
echo $num_of_c.'+';
$child_array = $tree->get_child_arr();

for($i=0;$i<$num_of_c;++$i)
{
count_total_children($child_array[$]);
}
}

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!


Merely echoing a bunch of numbers followed by plus signs doesn't cause PHP,
or whatever's displaying the output, to add them up.

A function should return a value. What you want is something like:

function count_descendants($tree)
{
$num_of_c = $tree->get_num_child();
$num_descendants = $num_of_c;
$child_array = $tree->get_child_arr();

for($i=0;$i<$num_of_c;++$i)
{
$num_descendants += count_descendants($child_array[$i]);
}
return($num_descendants);
}

Note that it's important here that $num_descendants be a local variable
rather than a global one, since each recursive invocation of the function
needs its own value for it.
Jul 17 '05 #6
You are close. You need to add the individual results up like this:

function count_total_children($tree)
{
$num_children = 0;

$child_array = $tree->get_child_arr();
for($i=0;$i<$num_of_c;++$i)
{
count_total_children($child_array[$]);
}

return $num_children + $tree->get_num_child();
}

bi****@hotmail.com (David) wrote in message news:<3e**************************@posting.google. com>...
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_children($tree)
{
$num_of_c = $tree->get_num_child();
echo $num_of_c.'+';
$child_array = $tree->get_child_arr();

for($i=0;$i<$num_of_c;++$i)
{
count_total_children($child_array[$]);
}
}

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!

Jul 17 '05 #7
> I think you mean the total number of *descendants*; when talking about tree
structures, "children" refers only to the *immediate* descendants of a
node.
OK!
Merely echoing a bunch of numbers followed by plus signs doesn't cause PHP,
or whatever's displaying the output, to add them up.
Give me some credit! I just wanted to illustrate that the function was
iterating correctly so the problem was the lack of a running counter.
A function should return a value. What you want is something like:

function count_descendants($tree)
{
$num_of_c = $tree->get_num_child();
$num_descendants = $num_of_c;
$child_array = $tree->get_child_arr();

for($i=0;$i<$num_of_c;++$i)
{
$num_descendants += count_descendants($child_array[$i]);
}
return($num_descendants);
}


I tried this and it works very well, but in the meantime got it to
work a different way. What do you think?

function count_total_desc($tree,&$counter){

$num_of_c = $tree->get_num_child();
$child_array = $tree->get_child_arr();

for($i=0;$i<$num_of_c;++$i){
$counter++;
count_total_children($child_array[$i],$counter);
}
return $counter;
}

/*So you call the function like this:*/

$counter = 0;
count_total_desc($tree,$counter);
Jul 17 '05 #8
Oops! There was an omission in the loop. Here is the code:

function count_total_children($tree)
{
$num_children = 0;

$child_array = $tree->get_child_arr();
for($i=0;$i<$num_of_c;++$i)
{
$num_children += count_total_children($child_array[$i]);
}

return $num_children + $tree->get_num_child();
}

ne**********@yahoo.com (php newbie) wrote in message news:<12**************************@posting.google. com>...
You are close. You need to add the individual results up like this:

function count_total_children($tree)
{
$num_children = 0;

$child_array = $tree->get_child_arr();
for($i=0;$i<$num_of_c;++$i)
{
count_total_children($child_array[$]);
}

return $num_children + $tree->get_num_child();
}

Jul 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mr Chat | last post by:
Hello all I am trying to write a specialist outlining tool, using the TreeView control (in VB5 running on W95). It has gone quite well so far. I have found some very useful info on how to...
19
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has...
4
by: sun6 | last post by:
this is a program counting words from "text_in.txt" file and writing them in "text_out.txt". it uses binary tree search, but there is an error when i use insert () thanks for any help ...
7
by: abhrajit | last post by:
I'm looking for a C/C++/Java library to create a balanced binary tree data structure given a set of leaf nodes as input. A leaf node should never become an interior node. So if I wish to create...
4
by: pmcguire | last post by:
I have a treeview with a lot of nodes. I want to load only the nodes that are initially visible when the form loads, and then continue to populate it in background and/or when the nodes are required...
0
LacrosseB0ss
by: LacrosseB0ss | last post by:
Hey all! I have just started using the TreeView object in asp. There are some other applications I have seen use it and I have copied some of the code from them at work. What happens is on a form,...
4
by: sharan | last post by:
Hello Friends I have a problem in Data Structure (In C) i want to implement a General tree having three child nodes of each parent node ...please send me any simple Example( code) by which i can...
10
by: John Rogers | last post by:
This code only counts the parent nodes or rootnodes in a treeview, how do you count all the nodes in a treeview? // one way int NodeCounter = 0; foreach (TreeNode currentNode in...
6
Sl1ver
by: Sl1ver | last post by:
I've got a problem, i got the nodes to move but 1. they copy nodes(if you drag it to 3 different places it will actually have 3 of the same nodes) 2. i want to make the nodes, if moved update the...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.