Learn

Our MaxHeap class will store two pieces of information in the form of instance attributes:

  • A Python list of the elements within the heap.
  • A count of the elements within the heap.

To make our lives easier, we’ll always keep one sentinel element at the beginning inside the list: None.

heap = MaxHeap() print(heap.heap_list) # [None] print(heap.count) # 0

A sentinel value will terminate a loop when read as input. This sentinel element of None will save us the trouble of checking whether the list is empty and simplify the methods we define in later lessons.

Instructions

1.

Within max_heap.py, define the MaxHeap class. Use a constructor that only takes self as an argument.

Inside the constructor, assign the class two instance attributes:

  • heap_list with the value set to [None]
  • count with the value set to 0
2.

Tab over to script.py. Make an instance of MaxHeap and assign it to the variable max_heap.

  • Print out max_heap.heap_list.
  • Print out max_heap.count.

Run your code!

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?