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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:31:11+00:00 2026-06-09T22:31:11+00:00

I am building a Heap data structure, but the part that is driving me

  • 0

I am building a Heap data structure, but the part that is driving me nuts is checking for a null child value.

I am using a Vector, and I am comparing a parent against a child, but if there’s only one child the program crashes since something like

vectorObject.get(i) //i'th element doesn't exist 

will return an exception.

I can’t check for the null element with something like

if (vectorObject.get(i) == null)

since running the get() method will automatically break the program, so how do you ACTUALLY check for a non-existing element without some unreadable weird hack-around??

  • 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-09T22:31:12+00:00Added an answer on June 9, 2026 at 10:31 pm

    You seem very confused. I think you need to re-read whatever reference you’re using to design your heap data structure (or read the relevant section of the Wikipedia article).

    If your parent is at 0 then your children are at 2*(0)+1=1 and 2*(0)+2=2. In this case, 1 < vectorObject.size() will be true but 2 < vectorObject.size() is false, meaning there is a left child but no right child.

    Since the Vector (or ArrayList if you’ve switched as per my suggestion) is zero-based you need to check if i < vectorObject.size(), not i <= vectorObject.size(). If i < vectorObject.size() then i is a legal index.

    Update:

    There are two ways you can structure the logic here. If you have to handle the single-child case totally differently than the two-child case then this would probably work best:

    int size = vectorObject.size();
    if (2*i+2 < size) { /* Two-child case */ }
    else if (2*i+1 < size) { /* One-child case */ }
    else { /* No children case */ }
    

    If you handle the two nodes separately, then nesting might be better:

    int size = vectorObject.size();
    if (2*i+1 < size) {
      // Handle left child
      if (2*i+2 < size) {
        // Handle right child
      }
      else {
        // No right child
      }
    }
    else { /* No children */ }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

building a site using PHP and MySQL that needs to store a lot of
for my assignment I am building a heap, the data for the heap is
Building a Silverlight 5 app that runs in the browser but getting some nagging
Building a website that has English & Japanese speaking users, with the Japanese users
Building an iPhone OS application that will allow users to anonymously post information to
When building websites I commonly use tools such as Browsershots to ensure that my
I am building a class hierarchy that uses SSE intrinsics functions and thus some
Building a sample ASP.NET MVC app. Using the Membership API for authentication. For whatever
building libraries with waf is nice and I like the lib<targetname> naming scheme. But
Building a commercial product may use various open source libraries that have use of

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.