Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualEnerjak

Posted 27 April 2012 - 09:51 PM

OK, i recently decided to practice some tree building techniques before taking on data structures in school (im doing java 2 in the fall so i have time) But i want to at least know the basics...Anyways, my problem is this:

I'm trying to get the number of nodes per node by ID. you know, here's what I have

public Node getChildren(int id)
{
  Node retNode = new Node();
  if(First != null)
  {
   retNode = First;
   First = retNode;
  }
  else
  {
   retNode = last;
   last = retNode;
  
  }
  getChildren(id);
  return retNode;
}

now if i remove the recursive call to getChildren(id) it works but it returns the same node as many times as there are children in the node
so i have this:

  Node root = new Node("Root");
  Node child = root.addChildToBack("Eggman");
  Node Child2 = root.addChildToBack("Sonic");
  Node child3 = root.addChildToFront("Tails");
  Node child4 = child.addChildToBack("Snively");
  Node child5 = Child2.addChildToBack("Sally");

  String message = "Root node has: " + root.getChildrenCount() + "\n" +
	   "Child Node has: " + child.getChildrenCount() + "\n" +
			 "Child Node 2 has: " + Child2.getChildrenCount() + "\n" +
	   "Child Node 3 has: " + child3.getChildrenCount() + "\n";

  JOptionPane.showMessageDialog(null, message);

  for(int x = 0; x < root.getChildrenCount(); x++)
  {
   Node c = root.getChildren(x);
   JOptionPane.showMessageDialog(null, " " + c.getName());
  }

now, as u can see it'll get tails 3 times since that's how many children the root has. any advice?

#1Enerjak

Posted 27 April 2012 - 09:08 PM

OK, i recently decided to practice some tree building techniques before taking on data structures in school (im doing java 2 in the fall so i have time) But i want to at least know the basics...Anyways, my problem is this:

I'm trying to get the number of nodes per node by ID. you know, here's what I have

public Node getChildren(int id)
{
  Node retNode = new Node();
  if(First != null)
  {
   retNode = First;
   First = retNode;
  }
  else
  {
   retNode = last;
   last = retNode;
  
  }
  getChildren(id);
  return retNode;
}

now if i remove the recursive call to getChildren(id) it works but it returns the same node as many times as there are children in the node
so i have this:

  Node root = new Node("Root");
  Node child = root.addChildToBack("Eggman");
  Node Child2 = root.addChildToBack("Sonic");
  Node child3 = root.addChildToFront("Tails");
  Node child4 = child.addChildToBack("Snively");
  Node child5 = Child2.addChildToBack("Sally");
 
  String message = "Root node has: " + root.getChildrenCount() + "\n" +
	   "Child Node has: " + child.getChildrenCount() + "\n" +
			 "Child Node 2 has: " + Child2.getChildrenCount() + "\n" +
	   "Child Node 3 has: " + child3.getChildrenCount() + "\n";
 
  JOptionPane.showMessageDialog(null, message);
 
  for(int x = 0; x < root.getChildrenCount(); x++)
  {
   Node c = root.getChildren(x);
   JOptionPane.showMessageDialog(null, " " + c.getName());
  }

now, as u can see it'll get tails 3 times since that's how many children the root has. any advice?

PARTNERS