JavaAlgorithms
Elementary and no so elementary Java algorithms
treeAlgorithms/TreeNodeIterator.java
Go to the documentation of this file.
00001 
00009 package treeAlgorithms;
00010 
00011 import java.util.ArrayList;
00012 import java.util.Iterator;
00013 
00024 public class TreeNodeIterator<T extends Comparable<T>> implements
00025         Iterator<TreeNode<T>> {
00026     private ArrayList<TreeNode<T>> mSiblings;
00027     private int ix;
00028 
00035     public TreeNodeIterator(TreeNode<T> treeNode) {
00036         ix = 0;
00037         mSiblings = new ArrayList<TreeNode<T>>();
00038         if (treeNode != null) {
00039             if (treeNode.getLeft() != null) {
00040                 mSiblings.add(treeNode.getLeft());
00041             }
00042             if (treeNode.getRight() != null) {
00043                 mSiblings.add(treeNode.getRight());
00044             }
00045         }
00046     }
00047 
00048     @Override
00049     public boolean hasNext() {
00050         return (ix < mSiblings.size());
00051     }
00052 
00053     @Override
00054     public TreeNode<T> next() {
00055         TreeNode<T> nextVal = null;
00056         if (hasNext()) {
00057             nextVal = mSiblings.get(ix);
00058             ix++;
00059         }
00060         return nextVal;
00061     }
00062 
00063     @Override
00064     public void remove() {
00065         // Not implemented
00066     }
00067 }
 All Classes Namespaces Files Functions Variables