Results 1 to 6 of 6
  1. #1
    pyler is offline Member
    Join Date
    Jul 2012
    Posts
    18
    Rep Power
    0

    Default POIterator not working properly

    I have a
    Java Code:
    POIterator
    class that implements an Iterator.
    A new instance of the
    Java Code:
    POIterator
    class is supposed to traverse a binary tree in Post Order. But when I attempt to test a
    Java Code:
    POIterator
    object in my main class, it doesn't work as its supposed to. I might be implementing it wrong. Could anyone find any errors in my code?
    Java Code:
    public PostOrderIterator<E> implements Iterator<E>{
    Deque<BNode<E>> somestack = new LinkedList<BNode<E>>();
    BNode<E> position;
    public POIterator(BNode<E>root){
    somestack = new LinkedList<BNode<E>>();
    somestack.push(root);}
    
    public T next(){
      position = somestack.pop();
      if(!position.hasLeftNode())
         return position.getInfo();
      else{
         position = position.getLeftNode();
         somestack.push(position);}
      next();//recursive call
      position=somestack.pop();
      if(!position.hasRightNode())
         next();//recursive call
    
    @Override
    public T next(){
      position = somestack.pop();
      if(position.hasLeftNode(){
         while(position = position.getLeftNode();
         somestack.push(position);}
      else if(position.hasRightNode());
      next();//recurisve call
       }
       return position.getInfo();
      }
    }
    Last edited by pyler; 04-17-2014 at 02:54 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: POIterator not working properly

    Could anyone find any errors in my code?
    How are you debugging the code to find any problems you are having? Add some println() statements to see what the code is doing.

    it doesn't work as its supposed to
    Why are you saying there are errors?

    BTW The code has lost its indentation formatting. Can you fix it so it is easier to read?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pyler is offline Member
    Join Date
    Jul 2012
    Posts
    18
    Rep Power
    0

    Default Re: POIterator not working properly

    Quote Originally Posted by Norm View Post
    How are you debugging the code to find any problems you are having? Add some println() statements to see what the code is doing.


    Why are you saying there are errors?

    BTW The code has lost its indentation formatting. Can you fix it so it is easier to read?
    Is there a way to so without typing the spaces?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: POIterator not working properly

    If spaces are too hard, try using the tab key.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,272
    Rep Power
    6

    Default Re: POIterator not working properly

    Or an IDE and use its code formatting features :/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default Re: POIterator not working properly

    Now the code has hidden }s at the end of lines making it hard to see them.
    Please move the }s to their own lines so they are on a line by themselves.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. For loop not working properly
    By phyzix5761 in forum New To Java
    Replies: 5
    Last Post: 11-18-2012, 08:59 PM
  2. setCaretPosition not working properly
    By phyzix5761 in forum AWT / Swing
    Replies: 6
    Last Post: 11-17-2012, 10:11 AM
  3. If statement is not working properly
    By Alpa in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 09:40 PM
  4. TCP/IP client not working properly
    By Chrillz in forum Networking
    Replies: 2
    Last Post: 07-20-2011, 08:39 PM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 01:13 PM

Tags for this Thread

Posting Permissions

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