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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:54:46+00:00 2026-06-15T20:54:46+00:00

I’ve looked all over and can’t seem to find any help for this.. for

  • 0

I’ve looked all over and can’t seem to find any help for this.. for a school project I have a BST tree and I have to put all the ints from the tree into an int array called BSTarray.
This is what I have so far:

public int [] toBSTArray() {
    int size = 20;
    int [] BSTarray = new int [size];
    for(int i = 0; i <size; i++) {
        makeArray(root);
        BSTarray[i] = root.getValue();
}

    return BSTarray;
}

//helper method called by toBSTArray
public void makeArray(BinarySearchTreeNode node) {
    if (node != null) {
        makeArray(node.getLeft());
        makeArray(node.getRight());
        // System.out.print(node.getValue() + " ");
    }
}

I thought this method was supposed to go through the tree and add in the values it finds into different indexes in the BSTarray, but all it’s doing is adding the same number into all the indexes in the array. Am I doing something wrong with the recursion?

  • 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-15T20:54:48+00:00Added an answer on June 15, 2026 at 8:54 pm

    Try this:

    Integer[] values = extractValues(n).toArray(new Integer[] {});
    

    with that method definition:

    private static List<Integer> extractValues(Node n) {
        List<Integer> result = new ArrayList<>();
        if (n.getLeft() != null) {
            result.addAll(extractValues(n.getLeft()));
        }
    
        if (n.getRight() != null) {
            result.addAll(extractValues(n.getRight()));
        }
    
        result.add(n.getValue());
    
        return result;
    }
    

    I assumed a node structure that is similar to yours. Of course, the method doesn’t have to be static if you don’t use it in a static way.

    This method might not be the most efficient due to the list conversion but you don’t have to bother with any array sizes. If you really need the function to return an array, just wrap it into another function or let the proposed function return an array (this would make it necessary to convert the list to an array before each return).

    Concerning your code, you iterate over the ito fill the entire array (no matter where you know the size from) but you always set the value to the value of the root node. That’s why you always have the same value. Your makeArray function calls itself recursively but it doesn’t do anything (even if you add a sysout statement 😉 )

    Update:

    And for the constraint of using no lists, here is another version that uses only arrays:

    int size = 20;
    int[] results = new int[size];
    extractValues(n, results, 0);
    

    with the method definition:

    private static int extractValues(Node n, int[] results, int index) {
        if (n.getLeft() != null) {
            index = extractValues(n.getLeft(), results, index);
        }
    
        if (n.getRight() != null) {
            index = extractValues(n.getRight(), results, index);
        }
    
        results[index] = n.getValue();
    
        return index + 1;
    }
    

    Note, that the result will be in results, then. The size has to be either assumed to be larger the number of nodes or it has to be counted by traversing the tree, before.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.