Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. package assigment5;
  2. import java.util.Iterator;
  3. import java.util.NoSuchElementException;
  4.  
  5. public class BinaryTreeInOrderIterator implements Iterator{
  6.     private Stack stack;
  7.  
  8.     public BinaryTreeInOrderIterator(BinaryNode root)
  9.     {
  10.         stack = new StackAsDynamicArray();
  11.         fillTheleft(root);
  12.     }
  13.  
  14.     public boolean hasNext()
  15.     {
  16.         return (!(stack.isEmpty()));
  17.     }
  18.     public void fillTheleft(BinaryNode current)
  19.     {
  20.         while (current != null )
  21.         {
  22.             stack.push(current);
  23.             current = current.left;
  24.         }
  25.     }
  26.    
  27.     public Object next()
  28.     {
  29.         if(!hasNext())
  30.             throw new NoSuchElementException();
  31.        
  32.         BinaryNode current =  (BinaryNode) stack.pop();
  33.         Object result = current.data;
  34.        
  35.         current = current.right;
  36.         while(current != null) {
  37.             stack.push(current);
  38.             current = current.left;
  39.         }
  40.        
  41.         return result;
  42.     }
  43.    
  44.     public void remove() {
  45.         throw new UnsupportedOperationException();
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement