Results 1 to 10 of 10
Thread: java recursion infinite loop
- 06-25-2008, 09:07 AM #1
Member
- Join Date
- Jun 2008
- Posts
- 8
- Rep Power
- 0
java recursion infinite loop
Hi i need to go over tree in java and print all the nodes
tempdata is the node structe:
class DataType {
String name;
LinkList DataType ;
getList{
return DataType
}
}
public void printTree(DataType node) {
DataType root = node;
while (!root.getList().isEmpty())
{
LinkedList list1 = root.getlinklist();
Iterator i = list1.iterator();
while (i.hasNext()) {
dataType tempDataType= (dataType)i.next();
printTree(tempDataType);
System.out.println( tempDataType.getName());
}
}
}
this soulution is infinite i get infinte loop
can any one suggent soultion to this probelm or any easy way to go over the tree and printed it. (it;s tree of tree)
10x.
- 06-25-2008, 09:11 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 06-25-2008, 09:26 AM #3
Member
- Join Date
- Jun 2008
- Posts
- 8
- Rep Power
- 0
the result is infinite loop
- 06-25-2008, 10:00 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Change this,
as thisJava Code:while (!root.getList().isEmpty())
Java Code:while (root.getList().isEmpty())
- 06-25-2008, 10:03 AM #5
Member
- Join Date
- Jun 2008
- Posts
- 8
- Rep Power
- 0
but then i will not enter to the loop anytime
- 06-25-2008, 10:12 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Ya, that's what I want to know. Just after that try to find the number of elements have in the list. If there is specific number of elements then something going wrong with the rest of the code.
- 06-25-2008, 10:51 AM #7
Member
- Join Date
- Jun 2008
- Posts
- 8
- Rep Power
- 0
i fixed it by adding check if i visit in the node or not
while (!node.getIparamGroupChildren().isEmpty() && !node.isVisited())
- 06-25-2008, 10:56 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
It's much better. Everything is fine now? In such a case start from the most outer loop. Check the possibilities it can have, and move to inner loops. Then you can find the firing condition for infinite loop.
- 06-25-2008, 11:00 AM #9
Hello tony,
Can u tell me which tree nodes r u trying to print? Is it a JTree,binary tree or any other such tree.To finish sooner, take your own time....
Nivedithaaaa
- 10-03-2008, 01:16 PM #10
Member
- Join Date
- Oct 2008
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
i could not get the recursion in java
By sivasayanth in forum New To JavaReplies: 3Last Post: 04-23-2008, 08:08 AM -
Help with loop in java
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:36 AM -
Recursion in java
By lenny in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 06:23 AM -
Help, loop with java
By cachi in forum New To JavaReplies: 5Last Post: 08-01-2007, 06:03 AM -
Enhanced For loop In Java
By goldhouse in forum Advanced JavaReplies: 1Last Post: 05-06-2007, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks