This week's book giveaway is in the Testing forum.
We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

traversing a simple tree in js.

 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think a pair of fresh eyes would help me here.
Does the following result in an infinite loop (processNode)
It does..how do i fix this?

<html>
<script>
//Construct a tree
function TreeNode(strText) {
this.childNodes = new Array();
this.text = strText
return this;
}

//add a child
TreeNode.prototype.addChild = function (strText) {
var objNode = new TreeNode(strText);
this.childNodes[this.childNodes.length] = objNode;
return objNode;
}

var localTree = new TreeNode("root");
var child1 = localTree.addChild("l-0");
var child2 = localTree.addChild("l-1");
var child3 = localTree.addChild("l-2");
child2.addChild("l-1.1");
child2.addChild("l-1.2");
child3.addChild("l-2.1");
child3.addChild("l-2.2");
child3.addChild("l-2.3");

processNode(localTree);

function processNode(node) {
alert(node.text);
for(i=0; i < node.childNodes.length; i++){
processNode(node.childNodes[i]);
}
}

</script>
</html>
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help? I cant understand how this can result in infinite recursion.
Is somethign wrong with my class definition? (Node and addChild).
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic