473,379 Members | 1,191 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 5274
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.