Heaps Flashcards

1
Q

What is the purpose of heaps?

A

Heaps are used to find the min and max value in a set of values

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What Python package do you have to use to create a heap in Python?

A

heapq

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: Python heaps can be either min heaps or max heaps

A

False - heaps are always min heaps in Python by default (can get around this by multiplying by -1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the syntax to add a value to a heap?

A

minHeap = [ ]
heaps.heappush(minHeap, n)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can we get the minimum value in a heap quickly in Python?

A

minHeap[0]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can we get the maximum value in a heap?

A

heapq.heappop(minHeap)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we create a max heap in Python when Python only allows min heaps by default?

A

Can multiply the heap by -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the time complexity of building a heap in Python?

A

O(n) time (have to touch every value in an array)

heapq.heapify(arr)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly