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>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
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>
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<