E - The type of element that the BinarySearchTree will hold. Please note that E must implement the
KeyElementInterface or some super class of E must implement the KeyElementInterface.K - The type of key that all elements must contain. Please note that K must implement the
Comparable interface or some super class of K must implement the Comparable interface.public class TreeElementIterator<E extends KeyedElementInterface<K>,K extends java.lang.Comparable<? super K>>
extends java.lang.Object
implements java.util.Iterator<E>
| Modifier and Type | Field and Description |
|---|---|
private BinarySearchTree<E,K> |
bst
Instance variable representing the
BinarySearchTree that this iterator will operate on. |
private java.util.Vector<TreeNode<E,K>> |
treeNodes
A Vector that will be used to contain the elements in configure order (preOrder, inOrder, or postOrder).
|
| Constructor and Description |
|---|
TreeElementIterator(BinarySearchTree<E,K> binarySearchTree)
This is the constructor for the
TreeElementIterator. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
hasNext()
This method is used to verify that the
TreeElementIterator has more elements to return. |
private void |
inOrder(TreeNode<E,K> treeNode)
A recursive method that traverses the binary search tree rooted at the
specified
treeNode in in-order, adding the visited nodes
to the treeNodes Vector. |
boolean |
isEmpty()
This method is used to check if the iterator is empty.
|
E |
next()
This method is used to retrieve the next element from the
TreeElementIterator. |
private void |
postOrder(TreeNode<E,K> treeNode)
A recursive method that traverses the binary search tree rooted at the
specified
treeNode in post-order, adding the visited nodes
to the treeNodes Vector. |
private void |
preOrder(TreeNode<E,K> treeNode)
A recursive method that traverses the binary search tree rooted at the
specified
treeNode in pre-order, adding the visited nodes
to the treeNodes Vector. |
void |
setInOrder()
This method sets the order of retrieval from the
TreeElementIterator to be inOrder. |
void |
setPostOrder()
This method sets the order of retrieval from the
TreeElementIterator to be postOrder. |
void |
setPreOrder()
This method sets the order of retrieval from the
TreeElementIterator to be preOrder. |
int |
size()
This method is used to obtain the number of elements available in the iterator.
|
private BinarySearchTree<E extends KeyedElementInterface<K>,K extends java.lang.Comparable<? super K>> bst
BinarySearchTree that this iterator will operate on.public TreeElementIterator(BinarySearchTree<E,K> binarySearchTree)
TreeElementIterator. The constructor will initialize the Iterator to initially
provide the elements using inOrder traversal.binarySearchTree - The BinarySearchTree that this iterator will operate on.public int size()
TreeNode's available in the iterator.public boolean isEmpty()
true if the iterator is emtpy, false
otherwise.public boolean hasNext()
TreeElementIterator has more elements to return.hasNext in interface java.util.Iterator<E extends KeyedElementInterface<K>>true if there are more elements to return, false otherwise.public E next() throws java.util.NoSuchElementException
TreeElementIterator.next in interface java.util.Iterator<E extends KeyedElementInterface<K>>TreeElementIterator.java.util.NoSuchElementExceptionpublic void setPreOrder()
TreeElementIterator to be preOrder.
This method will call the private method preOrder() in order to populate the treeNodes Vector of elements.public void setInOrder()
TreeElementIterator to be inOrder.
This method will call the private method inOrder() in order to populate the treeNodes Vector of elements.public void setPostOrder()
TreeElementIterator to be postOrder.
This method will call the private method postOrder() in order to populate the treeNodes Vector of elements.private void preOrder(TreeNode<E,K> treeNode)
treeNode in pre-order, adding the visited nodes
to the treeNodes Vector.treeNode - The node specifying the root of the binary search tree
being traversed.private void inOrder(TreeNode<E,K> treeNode)
treeNode in in-order, adding the visited nodes
to the treeNodes Vector.treeNode - The node specifying the root of the binary search tree
being traversed.private void postOrder(TreeNode<E,K> treeNode)
treeNode in post-order, adding the visited nodes
to the treeNodes Vector.treeNode - The node specifying the root of the binary search tree
being traversed.