Heaps Flashcards
What is the purpose of heaps?
Heaps are used to find the min and max value in a set of values
What Python package do you have to use to create a heap in Python?
heapq
True or False: Python heaps can be either min heaps or max heaps
False - heaps are always min heaps in Python by default (can get around this by multiplying by -1)
What is the syntax to add a value to a heap?
minHeap = [ ]
heaps.heappush(minHeap, n)
How can we get the minimum value in a heap quickly in Python?
minHeap[0]
How can we get the maximum value in a heap?
heapq.heappop(minHeap)
How can we create a max heap in Python when Python only allows min heaps by default?
Can multiply the heap by -1
What is the time complexity of building a heap in Python?
O(n) time (have to touch every value in an array)
heapq.heapify(arr)