Search code examples
javaconstructorstackbinary-search-treenodes

How to set up a constructor for a Iterator class with a stack?


I need help setting up this constructor for my Iterator class. The directions are as follows:

The constructor should create a new stack and push its node parameter onto it, followed by all left children accessible from the parameter. Consider a case in which the tree consists only of left children (essentially a linked list). The node with the highest value (root) would be pushed first and be on the bottom of the stack, followed by its left child just above it in the stack, followed by its left child, and so on until the leaf, which would contain the lowest value in the tree. When popping nodes from the stack, they would contain values from lowest to highest… an in-order traversal.

I am not sure how to create a new stack with the node in the parameter being a type BSTNode type.

Here is my code:

public static class Iterator<E>
    {
        private Stack<BSTNode<E>> stack;

        public Iterator(BSTNode<E> node)
        {

        }
        public boolean hasNext()
        {
            if(stack.peek() != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public E next()
        {
            stack.pop();
            E value;
            value = (E) stack.pop();
            return value;
        }
    }

As of right now, just ignore the other two methods, I just need help with the Iterator method. I'll figure those out later. Thank you.

I found out my problem was in a different class and method. I set it up as this and I want to know if this is the correct way of doing it.

The instructions for this method is

to create and return an instance of the static nested Iterator class that will be used to iterate through the elements in the tree. The tree's root should initially be passed to the iterator constructor.

Here is the following code I did for that method:

 public Iterator<E> iterator()
    {
        return new Iterator<>(root);
    }

root is the top of the binary search tree. It is in that class as a private variable.


Solution

  • Here's how I set it up.

    This is just the public that is above the class. Not inside the class. I just return a new Iterator with root being the top value.

    public Iterator<E> iterator()
        {
            return new Iterator<>(root);
        }
    

    Then inside the class below it, I create a new stack and have that stack push the nodes and the nodes to the left of it as long as it isn't null.

    public static class Iterator<E>
        {
            private Stack<BSTNode<E>> stack;
    
            public Iterator(BSTNode<E> node)
            {
                this.stack = new Stack<>();
                while (node != null)
                {
                    stack.push(node);
                    node = node.left;
                }
            }
            public boolean hasNext()
            {
                return !stack.isEmpty();
            }
            public E next()
            {
                BSTNode<E> goodDays = stack.pop();
                E result = goodDays.data;
                if (goodDays.right != null)
                {
                    goodDays = goodDays.right;
                    while (goodDays != null)
                    {
                        stack.push(goodDays);
                        goodDays = goodDays.left;
                    }
                }
                return result;
            }
        }