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


idx = len(self.heap_list) - 1

this would count as well, because we need the last index, not the last element

This is the first time to ask. I need a help to understand the swapping line below. Why are the variables on the right side not left side? What does it mean to assign variable to value??

self.heap_list[idx] = parent
self.heap_list[self.parent_idx(idx)] = child

Hi @tera3233728489.

You’re just modifying a single element of the list referred to by self.heap_list using idx as the index. A quick simplified example-

lst = ['a', 'b', 'c']
lst[2] = 'egg'  # modify the reference at index 2
Out: ['a', 'b', 'egg']

So in your example you’re changing self.heap_list at index idx to the name parent and whatever it references.

Why is the following line of code (instruction 4) resulting in “SyntaxError: invalid syntax”?

self.heap_list[idx] = parent

I checked the indentation, made sure there weren’t but spaces, etc., and keeps throwing SyntaxError.

Syntax errors are one of the ones that often occur from an error on a previous line, e.g. unmatched parantheses, missing separators and indentation problems. Try editing out that line and seeing if the error moves somewhere else because there’s a good chance it occurred on a previous line.

1 Like

That’s it! There was an unmatched parantheses on the previous line.
Thank you very much!

1 Like

I am sorry, I still don’t understand.
I know to find the last index of the heap_list you could type

idx = len(self.heap_list) - 1

My question is how does self.count do the same thing in this case? I thought it was just an internal counter for our minheap