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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:44:41+00:00 2026-05-27T22:44:41+00:00

How do I initialise a heap data structure in simple, basic Java pseudo-code with

  • 0

How do I initialise a heap data structure in simple, basic Java pseudo-code with 7 numbers?

  • 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-27T22:44:42+00:00Added an answer on May 27, 2026 at 10:44 pm

    If you want to do it yourself and not use Java’s PriorityQueue then you have to put all X items at the right place so they uphold heap properties:

    //This will switch the current item with it's greatest child, if this child is
    //larger than the current item itself.
    private void percolateDown(int i){
        //Get n's greatest child. The two children are at position 2i+1 and 2i+2
        int n = getGreatestChild(i);
    
        //Don't do anything if the greater child is smaller or equal to the parent
        if( yourNumbersArray[n] <= yourNumbersArray[i] )
            return;
    
        //Now switch the parent with the greatest child
    
        //Make sure that the newly placed item is at the right spot by percolating it again.
        percolateDown(n);
    }
    
    private void initialize(){
        //Call the percolateDown() function for all items of your array.
        //Due to the heap nature you can leave out the second half, though. 
        int mid = yourNumbersArray.length/2;
    
    //Start in the middle and work your way towards the front.
    //This way you'll first sort the lowest level of your heap, then the second lowest, and so on.
        for(; mid>=0; --mid){
            percolateDown(mid);
        }
    }
    

    When looking for the greatest child of any item you have to remember that one or both children could be outside of your array. In this case you don’t have to take it into account, of course.

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

Sidebar

Related Questions

I'm writing a data structure in C# (a priority queue using a fibonacci heap
Is it possible to initialise an array in Java using the 'advanced' for loop?
Stylistically is it better to initialise Java fields within a constructor or at the
I have the following code: public virtual void Initialise() { this.AddHeader(SystemContext, this.UserSettings.SystemContext); } public
I've got a very basic application that boils down to the following code: char*
I would like to allocate a structure on the heap, initialize it and return
In Java, is null an existing object on the heap? I'm trying to understand
So I always heard that class fields (heap based) were initialized, but stack based
It is a best practise to initialise a variable at the time of declaration.
I was wondering how to initialise an integer array such that it's size and

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.