473,661 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursion problems - how does it work?

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="javas cript">
function values(key, row, desc, parent)
{
this.key = key;
this.row = row;
this.desc = desc;
this.parent = parent
}
</script>

<script language="javas cript">
function countLeafNodes( valuesCell)
{
var sum = 0;
temp = valuesCell.key;

for (n=0; n < valuesCell.leng th; 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>
Jul 20 '05 #1
3 4692
csx
I've changed it around a bit. But still no luck. I've been reading some
Javascript sites and I was wondering, do I need to use
arguments.calle e
to get the recursion working? The examples are confusing. Some use it some
dont.

<html>
<head>
</head>
<body>

<script language="javas cript">
function information(key , row, desc, parent)
{
this.key = key;
this.row = row;
this.desc = desc;
this.parent = parent
}

function countLeafNodes( valuesCell)
{
var sum = 0;
temp = valuesCell.key;

for (n=0; n < values.length; n++)
{
if (temp == values[n].parent)
{
sum += countLeafNodes( n);
}
}

if (sum == 0) {
return 1; }
else {
return sum;
}
}

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>
Jul 20 '05 #2
On Sun, 5 Oct 2003 20:19:54 +0000 (UTC), "csx" <cs*@ms.com> wrote:
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
}

The comment on the last line is wrong...

So are you a C programmer or did you find this example somewhere and
tried to translate it to this javascript:
function countLeafNodes( valuesCell)
{
var sum = 0;
temp = valuesCell.key; This must be "var temp ...". var makes the variable a local variable
while not using var makes it global. You have to be especially aware
of the scope of your variables when you're dealing with recursion.

for (n=0; n < values.length; n++) Must be "var n=0". See above.
{
if (temp == values[n].parent)
{
sum += countLeafNodes( n); This is a problem I'll come back to shortly...
}
}

if (sum == 0) {
return 1; }
else {
return sum;
}
I would have left this written as
return ((sum==0)?1:sum );
or as
if (0 == sum)
sum = 1;
return sum;
for readability.

My guess is that you didn't write that C code, because if you had
you'd have been able to write the same thing in Javascript. You'll
have trouble writing a recursive function if you don't understand
what's going on. Try following your code and you'll find out pretty
quickly why you get a result of 1 instead of 4.

Code: var cols = countLeafNodes( values[2]);
Result: countLeafNodes gets the key property of values[2] which is 3
and then loops through the values array looking for items with a
parent property equal to 3. It finds the values[2] node, and calls
countLeafNodes( n).

<execution recurses into countLeafNodes>

Code: sum += countLeafNodes( n)
Result: countLeafNodes gets the key property of n which is undefined
because n is a number, not an object. (This is your main problem.)
Then it loops through the values array, looking for items with a key
property of undefined and obviously finds 0 nodes.

Code: if (sum == 0) return 1
Result: sum is 0, so returns 1

<execution leaves the recursive call to countLeafNodes, back to the
original countLeafNodes call>

The original countLeafNodes call is still in the loop. No more nodes
have a key value of 3 so no more calls to countLeafNodes are made and
at the end of the loop sum is still equal to 1.
I've changed it around a bit. But still no luck. I've been reading some
Javascript sites and I was wondering, do I need to use
arguments.calle e
to get the recursion working? The examples are confusing. Some use it some
dont.


Recursion is primarily used for tree-like data structures. Your data
structure is flat (a simple array) though it logically describes a
tree which is not particularly an efficient way of doing things. That
said, this is the correct translation of the C code to work with your
flat data structure.

function countLeafNodes( valuesCell)
{
var sum = 0;
var temp = valuesCell.key;

for (var n=0; n < values.length; n++)
{
if (temp == values[n].parent)
{
sum += countLeafNodes( values[n]);
}
}

return (sum == 0 ? 1 : sum);
}

If you want to learn something, step through and figure out why you
need to use "var" with your temp and n variables, and why you have to
pass values[n] instead of n to countLeafNodes.

Now that the flat data structure is out of the way, here's the same
thing but with a tree structure.

<script language="javas cript">
function information(key , desc)
{
this.key = key;
this.desc = desc;
this.parent = null;
this.row = 1;
this.subnodes = new Array();

this.addSubNode = function (node)
{
// Add the node to the subnodes array
this.subnodes.p ush(node);

// Set the node's parent and row
node.parent = this;
node.row = this.row + 1;

return node;
}

this.findNodeBy Key = function (key)
{
var node = null;

if (this.key == key)
{
// This is the node we're looking for
node = this;
}
else
{
// Search each of the subnodes
for (var i=0; i<this.subnodes .length; i++)
{
// Another recursive function call...
node = this.subnodes[i].findNodeByKey( key);

// Stop looping when we've found the node
if (node)
break;
}
}

return node;
}
}

function countLeafNodes( node)
{
var sum = 0;

if (node.subnodes. length == 0)
{
// There are no subnodes so this is a leaf node
sum = 1;
}
else
{
// Recurse down through the subnodes
for (var i=0; i<node.subnodes .length; i++)
sum += countLeafNodes( node.subnodes[i]);
}

return sum;
}
// Build the tree structure.
// Notice that there is no parent or row information here.
// They are set correctly by addSubNode.

root = new information(1, 'A');

root.addSubNode (new information(2, 'B'));
C_node = root.addSubNode (new information(3, 'C'));

D_node = C_node.addSubNo de(new information(4, 'D'));
C_node.addSubNo de(new information(5, 'E'));

D_node.addSubNo de(new information(6, 'F'));
D_node.addSubNo de(new information(7, 'G'));
D_node.addSubNo de(new information(8, 'H'));

// Count the leaf nodes under the C node
// Note: I could have used C_node instead of root.findNodeBy Key(3)
// but that would have been no fun...
var cols = countLeafNodes( root.findNodeBy Key(3));

document.write( "The number of cols is: " + cols + "<br>");

</script>

Regards,
Steve
Jul 20 '05 #3
csx
Steve,
Thankyou so much for the time and effort in helping!

kind regards!
Jul 20 '05 #4

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

Similar topics

10
2507
by: paulw | last post by:
Hi Please give problems that "HAS TO" to use recursion (recursive calls to itself.) Preferrably real world examples, not knights tour. I'm thinking about eliminating the use of stack... Thanks.
5
1281
by: Ioannis Vranos | last post by:
The following code is supposed to print all folder names of a folder but it does not work. Why? Change the folder in main() to a folder suitable for your system so as to test it. I created this proof-of-concept example, because I encountered similar recursion problems in a more complex project.
9
1691
by: John Pass | last post by:
I am trying to understand how recursion exactly works. See example (section) below: Say i = 4, then Number = 4 during 1st loop. Is Factorial(number - 1) automatically recognised as a recursive calculation? During the next loop Number = 3. Ultimately 4 x 3 x 2 x 1 has to be calculated to get the correct answer. Where are the intermediate values for 4, 3 and 2 stored? I don't see any code for it!It does work, so apparently it is not needed,...
2
1562
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm looking for problems that have double (or more) recursion, where the split is not clean (ie. where there may be overlap). Let's call this overlapped recursion, where the
19
2276
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
12
5820
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an asterisk is in a blob, an asterisk that is contiguous to it is in the same blob. If a blob has more than two asterisks, then each asterisk in the blob is contiguous to at least one other asterisk in the blob. For example this 12x12 grid has 6 blobs. ...
10
3316
by: elventear | last post by:
Hello everyone, I am runing into recursion limit problems. I have found that the culprit was related to the __hash__ function that I had assigned to the objects that were added to a set. Basically my __hash__ function is the following: def __hash__(self): out_int = 0
20
2986
by: athar.mirchi | last post by:
..plz define it.
30
8290
by: Jeff Bigham | last post by:
So, it appears that Javascript has a recursion limit of about 1000 levels on FF, maybe less/more on other browsers. Should such deep recursion then generally be avoided in Javascript? Surprisingly, deep recursion actually isn't that slow for what I'm doing. Programming this task recursively is so much more straightforward to me, but currently I'm forced to use an ugly hack to avoid going over the 1000 level limit. Of course, it could...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7364
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.