Click to See Complete Forum and Search --> : Having trouble with this recursive function


edelmatt
11-11-2003, 01:25 PM
I've used recursive functions before but this one is really giving me a rough time.

The idea is that there is a hierarchy (like a folder/object hierarchy), and an object is identified in the hierarchy this way:

a1: parent of all nodes
a1_1: first child of a1
a1_2: second child of a1 and sibling of a1_1
a1_1_1: first child of a1_1

and so on.

I want the function below to be able to identify all children of a given node. First, you pass in a node id (below it is set to "a1", but you should be able to reset it to another node). From there, it recurses.

The while loop in each recursion should identify all sibling nodes of the current node, and for each sibling, call recurse() again, to find children of the sibling.

If I comment out line 50, it correctly identifies all siblings.

If I un-comment, it recurses and finds the first child of the active node. BUT, after it recurses, the current while loop stops. How do I avoid this???

you may need to use IE 5.5+ in order for this to function properly. Any help is greatly appreciated...

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
<html>
<head>

</head>
<body>
<div id="debug">

</div>
<script>


//we want a recursive function

objArr = new Array();
objArr["a1"] = "true";
objArr["a1_1"] = "true";
objArr["a1_2"] = "true";
objArr["a1_2_1"] = "true";
objArr["a1_2_2"] = "true";
objArr["a1_2_2_1"] = "true";
objArr["a1_2_2_2"] = "true";
objArr["a1_2_2_2_1"] = "true";
objArr["a1_2_2_2_2"] = "true";
objArr["a1_2_2_2_2_1"] = "true";
objArr["a1_2_2_2_2_2"] = "true";
objArr["a1_2_2_2_2_2_1"] = "true";
objArr["a1_2_2_3"] = "true";
objArr["a1_2_2_3_1"] = "true";
objArr["a1_3"] = "true";
objArr["a1_3_1"] = "true";



debug.innerHTML += "objArr length is"+objArr.length+"<BR><BR>";
recurseInd = 0

function recurse(startId)
{
//recurse function will go straight down looking
//for sibling nodes
//for each sibling, recurse will be called again
//to see if there are children of that sibling
recurseInd++;

newIdInc = 1;
newId = startId + "_"+newIdInc++;
while (objArr[newId])
{
debug.innerHTML += newId+"<br>";
recurse(newId);
newId = startId + "_"+newIdInc++;
debug.innerHTML += "try with "+newId+"<br>";
}
}

//call the function first with "1"
debug.innerHTML += "1<br>";
recurse("1");

</script>
</body>
</html>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

edelmatt
11-11-2003, 01:36 PM
use this instead. sorry. and it's line 51, not 50 that you should comment/uncomment as mentioned above:

<html>
<head>

</head>
<body>
<div id="debug">

</div>
<script for=window event=onload>

//we want a recursive function

objArr = new Array();

objArr["a1"] = "true";
objArr["a1_1"] = "true";
objArr["a1_2"] = "true";
objArr["a1_2_1"] = "true";
objArr["a1_2_2"] = "true";
objArr["a1_2_2_1"] = "true";
objArr["a1_2_2_2"] = "true";
objArr["a1_2_2_2_1"] = "true";
objArr["a1_2_2_2_2"] = "true";
objArr["a1_2_2_2_2_1"] = "true";
objArr["a1_2_2_2_2_2"] = "true";
objArr["a1_2_2_2_2_2_1"] = "true";
objArr["a1_2_2_3"] = "true";
objArr["a1_2_2_3_1"] = "true";
objArr["a1_3"] = "true";
objArr["a1_3_1"] = "true";

recurseInd = 0

matchArray = new Array();

function recurse(startId)
{
//recurse function will go straight down looking
//for sibling nodes
//for each sibling, recurse will be called again
//to see if there are children of that sibling
recurseInd++;
debug.innerHTML += "recurse Ind "+recurseInd+"<BR><BR>";
newIdInc = 1;
newId = startId + "_"+newIdInc++;
while (objArr[newId] == "true")
{
matchArray[matchArray.length] = newId;
debug.innerHTML += newId+"<br>";
newId = startId + "_"+newIdInc++;
recurse(newId);
//debug.innerHTML += "try with "+newId+"<br>";
}
}

//call the function first with "1"

matchArray[matchArray.length] = "a1";
recurse("a1");

debug.innerHTML += "match array is <br>"+matchArray.join("<BR>");
</script>
</body>
</html>

edelmatt
11-11-2003, 02:00 PM
Hey. I fixed the problem. "Lee" on the "comp.lang.javascript" newsgroup informed me that because I was not adding the keyword "var" to the variables defined in the recurse() function, they were set as global variables, which would make the desired recursion fail.

So, the fixed function would be:

function recurse(startId)
{
//recurse function will go straight down looking
//for sibling nodes
//for each sibling, recurse will be called again
//to see if there are children of that sibling
recurseInd++;
debug.innerHTML += "recurse Ind "+recurseInd+"<BR><BR>";
var newIdInc = 1;
var newId = startId + "_"+newIdInc++;
while (objArr[newId] == "true")
{
matchArray[matchArray.length] = newId;
debug.innerHTML += newId+"<br>";
recurse(newId);
newId = startId + "_"+newIdInc++;
//debug.innerHTML += "try with "+newId+"<br>";
}
}

Thanks for those of you that read this...

Matt