FAQ: Heaps: Python - Min-Heaps of Fun

This community-built FAQ covers the “Min-Heaps of Fun” exercise from the lesson “Heaps: Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

Complex Data Structures

FAQs on the exercise Min-Heaps of Fun

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

In the section where we learned the concept of heap, left_child_index is defined as (index * 2) + 1. In the code given in min_heap.py its given by idx * 2. Could someone help clarify this for me? I’m scratching my head over it. Thanks in advance :slight_smile:

Yeah I agree, I’m confused by it too - can anyone help?

Normally the root node would be at index[0] of your list, but:

print(heap.heap_list)
#        [None, 10, 13, 21, 61, 22, 23, 99]

We created our heap with the value of None in index[0] rather than an empty list. I believe this was in order to skip over some steps like checking if the list is empty when adding the first value…

This means all of our indices are already shifted to the right by +1. So the + 1 in (index * 2) + 1 is already baked into how the list was created.

2 Likes

In the example showing adding and removing of random numbers the array starts with None. Is this just a place holder, does it count towards an index? The trees look like they ignore it so I am not sure how it works.