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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:57:49+00:00 2026-05-27T08:57:49+00:00

I get an OutOfMemoryError: Java heap space adding an array of sorted elements to

  • 0

I get an OutOfMemoryError: Java heap space adding an array of sorted elements to array implementation of a binary search tree. I can add arrays of random Integers fine but when I try adding a sorted array I run into this problem. The root of tree is being set to the smallest or largest element of the sorted array, so when you add the rest of the elements I end up with a extremely unbalanced tree which eats up my memory. Any help on this is much appreciated.

Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
at bst.ArrayBinarySearchTree.resize(ArrayBinarySearchTree.java:386)
at bst.ArrayBinarySearchTree.addElement(ArrayBinarySearchTree.java:26)
at bst.Experiment.main(Experiment.java:90)

    public void addElement(T element) {
    if(isEmpty()){
    count++;
    array[1] = element;
    }else{

         int current = 1;
         boolean added = false;

         while (!added) {
             if(current > (array.length-(array.length*.9))){
                    resize();
                }
            if (element.compareTo(array[current]) < 0){
               if (array[current*2] == null) {
                  array[current*2]=element;
                  added = true;
               } else
                  current = current*2;
            } else{
               if (array[current*2+1] == null) {
                  array[current*2+1]=element;
                  added = true;
               } else
                  current = current*2+1;
            }
         } //while
    }
    count++;
}

    private void resize(){
    T[] temp = (T[])(new Comparable[2*array.length-(array.length/4)]);
    for(int i=0; i<array.length; i++)
        temp[i] = array[i];
    array = temp;
}
  • 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-27T08:57:50+00:00Added an answer on May 27, 2026 at 8:57 am

    well, you will get an unbalanced tree if you dont have balancing in your tree, such as AVL or red black trees. If you are just building a binary search tree without balancing, it s possible that you get a out of memory exception.

    I would recommend you to try it with smaller array, to see if you still get a outofmemory exception. if yes, you have a flaw in your code.

    I have a binary search tree in c#, take a look at it. Usually you want to use Nodes instead of using an array to represent a binary search tree. As far as i see, you are using a technique which is usually used for heap data structure for heap sort.

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

Sidebar

Related Questions

I get a java.lang.OutOfMemoryError: Java heap space message when reading from an 11g Oracle
I always get java.lang.OutOfMemoryError: Java heap space error when I try to run any
Why do I get a Exception in thread AWT-EventQueue-0 java.lang.OutOfMemoryError: Java heap space because
I get this exception: Exception in thread AWT-EventQueue-0 java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2882)
While processing ODS sheet with JOpenDoc API, getting OutOfMemoryError for java heap space. The
I get a java.lang.outOfMemoryError exception, while writing a big file to the servletOutputStream. Every
get an array of bluetooth ID's Broadcast the bluetooth signal manually
I get this error: Can't locate Foo.pm in @INC Is there an easier way
I have a thread in a Java web application that causes a java.lang.OutOfMemoryError: Java
I just can't figure it out, why i get this error. It is not

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.