@Override
    public ExpressionTree next() {
        ExpressionTree result = stack.peek();

        if (!stack.isEmpty()) {
            ExpressionTree temp = stack.pop();

            if (!stack.isEmpty() && stack.peek().left().getRoot() != temp.getRoot() && stack.peek().right().getRoot() != temp.getRoot()) {
                temp = stack.peek();

                while (!temp.isNull()) {
                    if (!temp.right().isNull()) {
                        stack.push(temp.right());
                    }
                    if (!temp.left().isNull()) {
                        stack.push(temp.left());
                        temp = temp.left();
                    }
                    else {
                        temp = temp.right();
                    }
                }
            }
        }

        return result;
    }