C
csx
Hi all,
Ive got a problem with recursion in Javascript.
For this tree:
http://www.pcm.uklinux.net/structure.jpg
If you input node 3 (i.e. C) which is represented as 'values[2]' in the
array, it should return all the 'leaf' nodes. In this case 4!
Node 4 (i.e. D) is not included in the count.
In C, this function works perfectly:
int countLeafNodes(node& parentNode)
{
int sum = 0;
for (int j=0; j<nodes.size(); j++) {
if (parentNode.key == nodes[j].parent)
{
sum += countLeafNodes(nodes[j]);
}
}
return ((sum==0)?1:sum); // if node has no children sum is 0
}
I've tried to replicate this code in Javascript. But the recursion just
doesnt work. Has anyone got any suggestions? Any help would be much
appreciated.
Here's the JavaScript code at the moment.
<html>
<head>
<script language="javascript">
function values(key, row, desc, parent)
{
this.key = key;
this.row = row;
this.desc = desc;
this.parent = parent
}
</script>
<script language="javascript">
function countLeafNodes(valuesCell)
{
var sum = 0;
temp = valuesCell.key;
for (n=0; n < valuesCell.length; n++)
{
if (temp == valuesCell[n].parent)
{
sum += countLeafNodes(valuesCell[n]);
}
}
if (sum == 0) {
return 1; }
else {
return sum;
}
</head>
<body>
values = new Array();
values[0] = new information(1, 1,'A',0);
values[1] = new information(2, 2,'B',1);
values[2] = new information(3, 2,'C',1);
values[3] = new information(4, 3,'D',3);
values[4] = new information(5, 3,'E',3);
values[5] = new information(6, 4,'F',4);
values[6] = new information(7, 4,'G',4);
values[7] = new information(8, 4,'H',4);
var cols = countLeafNodes(values[2]);
document.write("The number of cols is: " + cols + "<br>");
</script>
</body>
</html>
Ive got a problem with recursion in Javascript.
For this tree:
http://www.pcm.uklinux.net/structure.jpg
If you input node 3 (i.e. C) which is represented as 'values[2]' in the
array, it should return all the 'leaf' nodes. In this case 4!
Node 4 (i.e. D) is not included in the count.
In C, this function works perfectly:
int countLeafNodes(node& parentNode)
{
int sum = 0;
for (int j=0; j<nodes.size(); j++) {
if (parentNode.key == nodes[j].parent)
{
sum += countLeafNodes(nodes[j]);
}
}
return ((sum==0)?1:sum); // if node has no children sum is 0
}
I've tried to replicate this code in Javascript. But the recursion just
doesnt work. Has anyone got any suggestions? Any help would be much
appreciated.
Here's the JavaScript code at the moment.
<html>
<head>
<script language="javascript">
function values(key, row, desc, parent)
{
this.key = key;
this.row = row;
this.desc = desc;
this.parent = parent
}
</script>
<script language="javascript">
function countLeafNodes(valuesCell)
{
var sum = 0;
temp = valuesCell.key;
for (n=0; n < valuesCell.length; n++)
{
if (temp == valuesCell[n].parent)
{
sum += countLeafNodes(valuesCell[n]);
}
}
if (sum == 0) {
return 1; }
else {
return sum;
}
</head>
<body>
values = new Array();
values[0] = new information(1, 1,'A',0);
values[1] = new information(2, 2,'B',1);
values[2] = new information(3, 2,'C',1);
values[3] = new information(4, 3,'D',3);
values[4] = new information(5, 3,'E',3);
values[5] = new information(6, 4,'F',4);
values[6] = new information(7, 4,'G',4);
values[7] = new information(8, 4,'H',4);
var cols = countLeafNodes(values[2]);
document.write("The number of cols is: " + cols + "<br>");
</script>
</body>
</html>