472,779 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Need help with a recursive function

Hello All:

I'm having trouble with a recursive function.

The function is supposed to identify nested folders in a hierarchical
folder structure.

The function "searchForFolders()" is supposed to traverse sibling
nodes in each iteration, and for each sibling node, it calls itself
again, to see if there are child nodes of the current sibling.

The code below contains the function in question. For simplicity's
sake, I have replaced the images with text characters. It doesn't
look as good, but you should be able to see how the nesting works.
When you click on a "Folder", you will get debug text on the RHS of
the screen.

You will see there is an XML hierarchy. That is parsed by another
function in the page to write out the hierarchy. That is working
fine. The only functions that I am having trouble with are
"searchForFolders()" and "hideShowFolder()".

Oh yeah, a lot of this is MS only script, so you'll need IE 5.5 or
above to view it correctly.

Thanks for any help you can give me.

Matt.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<LINK REL="stylesheet" TYPE="text/css" HREF="treecontrol.css">
<title>Tree Control</title>
<XML ID="hierXml">
<main>
<tNode type="folder" label="Best Buyers">
<tNode type="object" label="Gold"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
<tNode type="object" label="Bronze"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Bronze"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Silver"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Bronze"/>
<tNode type="object" label="Gold"/>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
</tNode>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Gold"/>
</tNode>
</tNode>
</tNode>
<tNode type="folder" label="Best Buyers - Sweepstakes">
<tNode type="object" label="Bronze"/>
</tNode>
</tNode>
</main>
</XML>

</head>

<body bgcolor="#cccccc" onload="getXml()">
<script src="treeControlFns.js">
</script>
<script>
closeFolderArray = new Array();
function hideShowFolder(ref)
{
//use pixelTop property to move DIVs up and down
//
//ref.style.pixelTop += 10;

//use visibility to hide DIVs
//
//ref.style.visibility = "hidden";

//the children of the parent of the ref is all the folder/object
DIVs
//
//ref.parentNode.childNodes.length;

allNodes = ref.parentNode.childNodes;
moveInc = 0;
oneDivHeight = ref.offsetHeight;
moveArr = new Array();

//break up the clicked ref id into an array
clickedId = ref.id.split("_");

//lets find all the nested folders beneath that which was clicked
//anything that starts with the same array as the folder clicked
//and is a folder, will have to be closed
closeFolderArray = new Array();
closeFolderArray[closeFolderArray.length] = ref.id;
debug.innerHTML = "";
searchForFolders(clickedId);
debug.innerHTML += closeFolderArray.join("<BR>");
sffInd = 0;
}
sffInd = 0;

function searchForFolders(startId)
{
//indicate which iteration of sff we are in
sffInd++;
debug.innerHTML += "we are in iteration: "+sffInd+"\nstarted with:
"+startId+"<br><br>";
pushInd = 1;
startId[startId.length] = pushInd;
makeId = null;
makeId = startId.join("_");
if (!visArray[makeId])
{
return false;
}
while (visArray[makeId])
{
debug.innerHTML += "testing with "+makeId+"<br><br>";

//test to see if present id is that of a folder
if (document.all(makeId).getAttribute("objtype") == "folder")
{
closeFolderArray[closeFolderArray.length] = makeId;
debug.innerHTML += "add "+makeId+" in iteration
"+sffInd+"<br><br>";
}

//call next iteration of sff to look for child folders of present
object
if (visArray[makeId+"_1"])
{
tempArr = makeId.split("_");
debug.innerHTML += "startId is now "+startId+" and tempArr is
"+tempArr+"<br><br>";
//searchForFolders(tempArr);
}
//continue incrementing
startId[startId.length-1] = ++pushInd;
makeId = startId.join("_");
}
//return;
}

//active object
activeObj = "";
//graphic elements for folder points and object points
folderPrefix = "&nbsp;&nbsp;--[F]&nbsp;";
objectPrefix = "&nbsp;&nbsp;----(o)&nbsp;";
lastFolderPrefix = "&nbsp;&nbsp;--[F]&nbsp;";
lastObjectPrefix = "&nbsp;&nbsp;----(o)&nbsp;";
objectDivStart = "<div objtype=object style=\"visibility:visible\"
onclick=\"objectClick(this);\" class=\"objectDiv\" ";
folderDivStart = "<div objtype=folder objstate=open
style=\"visibility:visible\" class=\"folderDiv\"
onclick=\"hideShowFolder(this)\" ";
divMid = " />";
textSpanStart="<span class=\"textSpan\" id=\"";
textSpanMid="\" />";
textSpanEnd = "</span>";
holderText = "";
id = "a_";
blankGif = "--";
iGif = "";

visArray = new Array();
//xml parsing and screen rendering
function getXml()
{
//if xml is available, call recurser function on the main node
if (hierXml.readyState == "complete")
{
mainNode = hierXml.childNodes(0);
mainChildInc = 0;
curMainNode = mainNode.childNodes(0);
recurser(curMainNode,"","",1);
}
printer.innerHTML = holderText;
}

function recurser(node,gfxPrefix,prefixStr,siblingCount)
{
//loop through sibling nodes

while (node)
{
//set the prefix string, which becomes the id and the map
//back to the XML object
localPrefixStr = prefixStr+siblingCount;

//set graphical vars based on what type of node this is
//and whether it is the last sibling
if (node.getAttribute("type") == "folder")
{
itemPrefix = (node == node.parentNode.lastChild) ?
lastFolderPrefix : folderPrefix;
itemDivStart = folderDivStart;

}
else
{
itemPrefix = (node == node.parentNode.lastChild) ?
lastObjectPrefix : objectPrefix;
itemDivStart = objectDivStart;
}
itemGfxPrefix = (node == node.parentNode.lastChild) ?
gfxPrefix+blankGif : gfxPrefix+iGif;
visArray["a"+localPrefixStr] = "visible";
if (node.getAttribute("type") == "object")
{

}
//add HTML to display this node
holderText += itemDivStart+"id=\"a"+localPrefixStr+"\""+divMid+g fxPrefix+itemPrefix+textSpanStart+"span_a"+localPr efixStr+textSpanMid+node.getAttribute("label")+"
("+localPrefixStr+")"+textSpanEnd+"</div>";

//call this function to process any child nodes of the current
node
recurser(node.firstChild,itemGfxPrefix,localPrefix Str+"_",1);

//increment node and sibling count for while loop
node = node.nextSibling;
siblingCount++;
}

}
</script>
<table>
<tr>
<td>
<div class="instructions">
<ul>
<li>string in parantheses is the ID that indicates a node's
hierarchical placement
<li>selected object ID: <span style="font-weight:bold" id="counter">
</span>
<ul>
</div>
<div class="printer" id="printer">

</div>
</td>
<td>
<div id="debug">
</div>
</td>
</tr>
<table>
<br>
<br>
<br>

</body>
</html>
Jul 20 '05 #1
2 5221
Lee
LoserInYourFaceEngineer said:

Hello All:

I'm having trouble with a recursive function.


Recursion won't work when all of your variables are global.
Use the "var" keyword to declare them as local variables.

Jul 20 '05 #2
Holy ****! ! ! !

Wow. I can't believe I've been spinning my wheels for TWO DAYS on this!

Thanks man. That was a great great help. I will raise a toast in your
honor tonight.

Matt.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3

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

Similar topics

15
by: chahnaz.ourzikene | last post by:
Hi all, This is the first i post in this newsgroup, i hope my english is not too bad... Let's get straight to the point ! I have a little probleme using threads in my little training example :...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
2
by: Ryan Ternier | last post by:
I'm currently run into a snag on one of my projects. We need to create an ordered list (Mutli levels). Ie: 1. Some Title ....A....Something here ....B....Something Else 2. Another Title
4
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2,...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
9
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
0
by: Michael L | last post by:
Hi Guys(I apologize for the lengty post - Im trying to explain it as best i can) I've been cracking my head on this one for the past 24+ hours and i have tried creating the function in ten...
2
by: Anders B | last post by:
I want to make a program that reads the content of a LUA array save file.. More precicely a save file from a World of Warcraft plugin called CharacterProfiler, which dumps alot of information about...
9
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.