K - This is the type of the Key being used by the BinarySearchTree to position TreeItems in the treeV - This is the type of the Value associated with the Keypublic class TreeIterator<K extends java.lang.Comparable<? super K>,V> extends java.lang.Object implements java.util.Iterator<TreeItem<K,V>>
| Modifier and Type | Field and Description |
|---|---|
private BinarySearchTree<K,V> |
bst |
private java.util.Vector<TreeNode<K,V>> |
treeNodes |
| Constructor and Description |
|---|
TreeIterator(BinarySearchTree<K,V> binarySearchTree)
This is the constructor for the
TreeIterator. |
| Modifier and Type | Method and Description |
|---|---|
boolean |
hasNext()
This method is used to verify that the
TreeIterator has more elements to return. |
private void |
inorder(TreeNode<K,V> 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.
|
TreeItem<K,V> |
next()
This method is used to retrieve the next element from the
TreeIterator. |
private void |
postorder(TreeNode<K,V> 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<K,V> 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
TreeIterator to be Inorder. |
void |
setPostorder()
This method sets the order of retrieval from the
TreeIterator to be Postorder. |
void |
setPreorder()
This method sets the order of retrieval from the
TreeIterator to be Preorder. |
int |
size()
This method is used to obtain the number of elements available in the iterator.
|
private BinarySearchTree<K extends java.lang.Comparable<? super K>,V> bst
public TreeIterator(BinarySearchTree<K,V> binarySearchTree)
TreeIterator.binarySearchTree - The BinaryTreeBasis 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()
TreeIterator has more elements to return.public TreeItem<K,V> next() throws java.util.NoSuchElementException
TreeIterator.public void setPreorder()
TreeIterator to be Preorder.
This method will call the private method preorder() in order to populate the queue.public void setInorder()
TreeIterator to be Inorder.
This method will call the private method inorder() in order to populate the queue.public void setPostorder()
TreeIterator to be Postorder.
This method will call the private method postorder() in order to populate the queue.private void preorder(TreeNode<K,V> 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<K,V> 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<K,V> 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.