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

  • Home
  • SEARCH
  • 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 6764129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:33:27+00:00 2026-05-26T14:33:27+00:00

I have rewritten the Binary Search Tree code found in the following links to

  • 0

I have rewritten the Binary Search Tree code found in the following links to work generically – the class will work with any type parameter.
http://www.informatics.susx.ac.uk/courses/dats/DataStructures/SearchTree.java
http://www.informatics.susx.ac.uk/courses/dats/teach/code/demos/SearchTreeDemo.java

I placed my generic SearchTree.java in a package named mysearchtree, and imported that into SearchTreeDemo.java.

SearchTree.java contains:
Abstract class SearchTree.
Concrete class EmptyTree, which has a protected constructor.
Concrete class NodeTree, which also has a protected constructor.

The original, non-generic coding for the SearchTree abstract class was that a client of the code would instantiate an EmptyTree object through a static method as below:

public abstract class SearchTree {
    /**
     * Returns an empty tree.
     */
    public static SearchTree empty() {
        return new EmptyTree();
    }

    // more code
}

This would have been used as follows:

SearchTree t = SearchTree.empty();
t = t.add( new String("hello") );

When working with generics, I have re-written the class to have the following structure:

public abstract class SearchTree<T extends Comparable<T>> {
    public abstract boolean isEmpty();
    public abstract int numNodes();
    public abstract boolean contains(T key);
    public abstract SearchTree<T> add(T item);
    public abstract SearchTree<T> remove(T item);
    public abstract String toString();
}

The original plan of providing a static method that would return an EmptyTree will not work generically (you can’t use the generic type parameters of the class in a static method).

My question is: How do I provide a type-safe way of allowing a client of this code to instantiate a EmptyTree from outside the package (or some other equivalent mechanism that would replace the SearchTree t = SearchTree.empty() code)?

Update:

I have tried the following, but get an error:

public static <U> SearchTree<U> createSearchTree() { return new EmptyTree<U>(); }

Errors:

./mysearchtree/SearchTree.java:7: type parameter U is not within its bound
    public static <U> SearchTree<U> createSearchTree() { return new EmptyTree<U>(); }
                                 ^
./mysearchtree/SearchTree.java:7: type parameter U is not within its bound
    public static <U> SearchTree<U> createSearchTree() { return new EmptyTree<U>(); }
                                                                              ^
2 errors
  • 1 1 Answer
  • 3 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-05-26T14:33:28+00:00Added an answer on May 26, 2026 at 2:33 pm

    Is something like this what you are looking for? A parametrized method.

    public static <Y extends Comparable<Y>> SearchTree<Y> empty() {
       return new SearchTree<Y>();
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a prototype website written in PHP. Lately i've rewritten code to separate
I posted a similar question yesterday with the same code, I have rewritten and
So have rewritten my ugly php URL to something prettier: RewriteEngine On RewriteRule ^main/photo([^/\.]+)/?.html$
I have rewritten an old program and designed a new database for it. I
I am new to Oracle. Since we have rewritten an earlier application , we
I have a rewrite rule of the following form: RewriteRule ^foo/([a-zA-Z0-9]+)$ foo.php?arg=$1 [qsa,nc] It
On my site I have mod_rewrite rules to make the URLs more search engine
I have some applications that are written in ANSI C. Will I be able
I have rewritten the below based on the answers. I have a website that
after reading the responses I have rewritten my question. Let's say I have a

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.