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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:44:03+00:00 2026-06-14T01:44:03+00:00

I have an array representation that holds a max heap. For an array of

  • 0

I have an array representation that holds a max heap. For an array of ‘d’,’b’,’c’ it’s holding : b, d, c although it is supposed to be b, c, d. This is my add method:

public boolean add (E e) {
  ensureCapacity(objectCount + 1);
  pq[objectCount++] = e;

  //percolate up
  for (int i = objectCount - 1; i >= 0; i--) {
     if (priorityComparator.compare(pq[i], pq[parent(i)]) >= 0) {
          E tmp = pq[parent(i)];
          pq[parent(i)] = pq[i];
          pq[i] = tmp;

     }
  }

  modCount++;

  return true;
  }

If I add ‘a’ next it changes successfully to a,b,c,d. How can I fix the method so that it check the left child (2*i+1) and right child(2*i+2) and swaps their values according to which has the higher priority queue (a has higher priority than b, etc)?

  • 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-14T01:44:04+00:00Added an answer on June 14, 2026 at 1:44 am

    I think you have a MIN heap and not a MAX heap.

    I haven’t looked at your code but:

    A min heap guarantees the following:

    1. The root is always the min (element 0 in your case)
    2. Tree is always left-ordered (this is true for any heap)

    The output b, d, c is perfectly legit in your case. Try removing b and call heapify again, the output will be c, d.

    Wiki link: http://en.wikipedia.org/wiki/Binary_heap

    EDIT:
    If in your case you consider giving ‘a’ a higher priority than ‘b’, then yes, you do have a MAX heap.

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

Sidebar

Related Questions

i have a math class that stores representation of expressions in n-array lists i
I have a string representation of an array: '[item1, item2, item3]' I'm trying to
I have a little bit seen the representation of an array in memory with
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have the need of get the array representation of a Doctrine 2 Entity.
I have been doing some research on Binary trees, and the array list representation.
I have an array a of 10 booleans (or equivalently the binary representation of
I have a byte array that can be of size 2,3 or 4. I
I have byte array as input. I would like to convert that array to

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.