Results 1 to 10 of 10
  1. #1
    tony404 is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default 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.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    tony404 is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    the result is infinite loop

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Change this,

    Java Code:
    while (!root.getList().isEmpty())
    as this

    Java Code:
    while (root.getList().isEmpty())

  5. #5
    tony404 is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    but then i will not enter to the loop anytime

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  7. #7
    tony404 is offline Member
    Join Date
    Jun 2008
    Posts
    8
    Rep Power
    0

    Default

    i fixed it by adding check if i visit in the node or not
    while (!node.getIparamGroupChildren().isEmpty() && !node.isVisited())

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  9. #9
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    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. #10
    kellaw is offline Member
    Join Date
    Oct 2008
    Posts
    24
    Rep Power
    0

Similar Threads

  1. i could not get the recursion in java
    By sivasayanth in forum New To Java
    Replies: 3
    Last Post: 04-23-2008, 08:08 AM
  2. Help with loop in java
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:36 AM
  3. Recursion in java
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 06:23 AM
  4. Help, loop with java
    By cachi in forum New To Java
    Replies: 5
    Last Post: 08-01-2007, 06:03 AM
  5. Enhanced For loop In Java
    By goldhouse in forum Advanced Java
    Replies: 1
    Last Post: 05-06-2007, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •