FAQ: Heaps: Python - Adding an Element: Heapify Up II

This community-built FAQ covers the “Adding an Element: Heapify Up II” 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 Adding an Element: Heapify Up II

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!

First assignment is:

Inside of .heapify_up() , declare a variable called idx and set it to the last index of the internal list.

I used the below, but it returned incorrectly and told me to use self.count:

idx = self.heap_list[-1]

Why would this not work?

Edited to reflect that it told me to use self.count

1 Like

The only reason why I guess this would not work is because the first (and only, after instantiating) element in self.heap_list is “None”, while self.count = 0.

Would you not want to use self.heap_list[-1] because that is “None”?


You are to set it to the last index, not the last element of the list.


while I was working on the heapify up funktion. I thought it would be great to actually see the tree and get a better understanding of what I’m doing.
Therefore I wrote a tree_update helper function. Maybe it can be some help for others as well.

put the maximum idx and you can call it after and before you add a new element to see the chances.
Or you can integrate it into the add and heapify_up function by calling self.tree_update(len(self.heap_list))

On a side note: The function could be smarter, but I tried to name everything that it hopefully is self explaining. Feel free to chance it and feedback of course is always welcome.

def tree_update(self,idx):
	tree_range = range(1,idx+1)
	level_count = (len(tree_range) % 2) +(int((len(tree_range)/2)-1))
	print("\nThis Binary Tree has {levels} levels.".format(levels=(str(level_count))))
	print("Every parent has max. two children.\n")
	new_tree_list = []
	spaces = int(idx)*2

	i = 1
	while level_count > 0:
		sibbling_list = []
		i = (i + i)
		level_count -= 1

	element_count = 1
	level = 1
	for element in new_tree_list:
		print(("Level: ") + (str(level)) + ("/") + (str(element_count)) + " Child max.")
		print((" ")*(int(spaces)) + (str(element)))
		element_count = (element_count) + (element_count)
		level += 1
		spaces = int(spaces)- (element_count)

min_heap.heap_list = [None, 10, 13, 21, 61, 22, 23, 99] # = 8 elements in list > max idx = 8