Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:53:17+00:00 2026-06-12T11:53:17+00:00

these are my fields: public class BSTSet <E> extends AbstractSet <E> { // Data

  • 0

these are my fields:

public class BSTSet <E> extends AbstractSet <E> {

    // Data fields
    private BSTNode root;
    private int count = 0;
    private Comparator<E> comp;   // default comparator

    /** Private class for the nodes.
     *  Has public fields so methods in BSTSet can access fields directly. 
     */
    private class BSTNode {

        // Data fields

        public E value;
        public BSTNode left = null;
        public BSTNode right = null;

        // Constructor

        public BSTNode(E v) {
            value = v;
        }

        //creates a method called contains so that i can call it later on for my find method
        public boolean contains(Object item) {
            return contains(item);//root.value.equals(item);
        }

        public int height() {
            return height();
        }

    }
    // Constructors - can either use a default comparator or provide one
    public BSTSet() {
        comp = new ComparableComparator();      // Declared below
    }

    public BSTSet(Comparator <E> c) {
        comp = c;
    }
}

and this is what i am trying to complete:

private class BSTSetIterator implements Iterator<E> {

    private Stack<BSTNode> stack = new Stack<BSTNode>();
    private BSTNode current = root;

    public BSTSetIterator(BSTNode root) {

         return new BSTSetIterator();

    }

    public boolean hasNext() {

        boolean hasNext = false;
        hasNext = !stack.isEmpty() || current != null;
        return hasNext;

    }

    public E next() {

        BSTNode next = null;

        while (current != null) {
            stack.push(current);
            current = current.left;
        }
        next = stack.pop();
        current = next.right;

        return next;

    }

    public void remove() {
        throw new UnsupportedOperationException();
    }
}
// Comparator for comparable 

private class ComparableComparator implements Comparator<E> {
    public int compare(E ob1, E ob2) {
        return ((Comparable)ob1).compareTo(ob2);
    }
}

So far the code fails at lines return new BSTSetIterator(); and return next;. For return next it says that it is the wrong data type to return. How would I go about fixing these methods so that I can iterate through a BST using a Stack?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:53:18+00:00Added an answer on June 12, 2026 at 11:53 am
    BSTSetIterator();
    

    This doesn’t work, because your constructor expects a root and you didn’t pass that parameter. If you have a BSTSet object called ‘tree’, and you want to create a new iterator, then you should create the iterator this way:

    BSTSetIterator iterator = new BSTSetIterator(tree.getRoot());
    

    However, you don’t have a getter in your BSTSet class and your root is private. Don’t worry, the solution for that problem is to create a public getter inside your BSTSetIterator class, like this:

    public BSTNode getRoot()
    {
        return this.root;
    }
    

    Constructors don’t return values, this is incorrect:

     public BSTSetIterator(BSTNode root) {
             return new BSTSetIterator();
        }
    

    Instead, write your construtor this way:

    public BSTSetIterator(BSTNode root)
    {
        this.current = root;
    }
    

    Also, this definition is incorrect, because root is out of reach:

    private BSTNode current = root;
    

    You should have this instead:

    private BSTNode current;
    

    As for your other problem,

    BSTNode next = null;
    

    means that your variable called ‘next’ is of BSTNode type.

    public E next()
    

    means that your method called next is of E type. as E and BSTNode is not the same, your return:

    return next;
    

    is incorrect. I could give you more help, but I have realized you are learning now the language and it’s better to let you explore yourself the technology and programming in general, because this way you will become quicker. “Give a man a fish, and you feed him for a day. Teach a man how to fish, and you feed him for a lifetime.”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some class with lots of fields; public class CrowdedHouse { public int
I am confused with the data types of these fields. I want to get
I have the following class: public class DocketType : Enumeration<DocketType, int, string> { public
If I have for example one class like public class User{ public int Id
I have a class: public class TaskDiplayModel { public int taskId { get; set;
I have one supertype defined as: public abstract class AType<T> { .... private T
Imagine you have two fields in your Model: public class MyModel { [Required(ErrorMessageResourceType =
I have a class which has a number of public fields that are marked
I have table consisting of these fields: id | date_from | date_to | price
I have table consisting of these fields: id | date_from | date_to | price

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.