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 6755863
In Process

The Archive Base Latest Questions

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

Implementation Use: Data Structure Lab Exercise for October/28/2011 To do: Implement a Binary Search

  • 0

Implementation Use: Data Structure Lab Exercise for October/28/2011
To do: Implement a Binary Search Tree

Problem: K[] return of methods preOrder(), inOrder() and postOrder()

Problem Details:
The BST must only have its root as a parameter. The methods above mentioned have been described in an interface given by our professor as the following:

    /**
    * Returns an array of keys filled according
    * to the pre-order traversing in a BST.  
    */      
    public K[] preOrder();      
    public K[] order();         
    public K[] postOrder();

I could instantiate the generic array with the following code:

public K[] preOrder() {

    if (root == null) { return null; }

    ArrayList<K> list = new ArrayList<K>();
    preOrderRecursive(root,list);
    K[] toReturn = (K[]) Array.newInstance(this.getRoot().getKey().getClass(), list.size());

    for (int i = 0; i < list.size(); i++) {
        toReturn[i] = list.get(i);
    }

    return toReturn;
}

But, when i tested the method using a testing class also provided by our professor, i got a nullPointerException, wich i think is referring to the root of the BST, that has been once instantiated, but has been removed at a point in the test, and when it calls the method again, the method returns null, not an empty array as expected by the test:

    (...)

    tree1 = new BSTImpl<Integer, Integer>();
    for (int i = 0; i < SIZE; i++) {
        tree1.insert(i, i);
    }
    tree1.remove(1);
    tree1.remove(2);
    tree1.remove(3);
    tree1.remove(4);
    assertArrayEquals(new Integer[]{},tree1.preOrder());

    (...)

Knowing that i can’t change the return type nor the parameters of the method, what can i do to avoid this Exception? Can i somehow get the component type and use it to instantiate the empty array (how’d i do this?)?

Any tips to improve my code are also welcome.

  • 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-05-26T13:27:13+00:00Added an answer on May 26, 2026 at 1:27 pm

    Question is: Do you really need an Integer array returned? – I don’t think so.

    I assume assertArrayEquals() does not check the component type of the arrays but rather only compares the contained values.

    With the code you showed, I’d say anything else would not be possible due to type erasure.

    So try to return an Object[] or whatever super-type applies in your case:

    return new Object[0];

    and

    return list.toArray();

    (unchecked casts required, but that should be o.k.)

    EDIT:

    Ok, so <K extends Comparable>?
    In that case use:

    return new Comparable[0];

    and

    return (Comparable[])list.toArray(new Comparable[]);

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

Sidebar

Related Questions

Do you know any function implementation I could use for decrypting data encrypted by
I intend to use RADIX / MTRIE as my preferred data-structure for a routing
I'm trying to decide which data structure to use for the following. Lets say
I'm writing a Perl script and would like to use a n-ary tree data
Is there any Queue data structure implementation that comes with C or will I
What would be a smart data structure to use to represent a Sudoku puzzle?
I'd like to implement the following data structure in c++ (pseudo code): Map<Integer, Integer>
I'm writing a Ruby script and would like to use a n-ary tree data
What is the optimal (easy to maintain, reasonably fast, robust) implementation of tree-like data
Which Tree data structure in Java allows querying for different levels of children? I

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.