Binary Heap Flashcards

1
Q

What is a binary heap?

A

A binary heap is a complete binary tree that satisfies the heap property, where every parent node is either greater than (max heap) or less than (min heap) its children.

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

What are the invariants of a binary heap?

A

Completeness: It is a complete binary tree, with all levels fully filled except possibly the last.
Heap Property:
In a min heap, each parent node is smaller than its children.
In a max heap, each parent node is larger than its children.

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

What is the primary difference between a min heap and a max heap?

A

In a min heap, the smallest element is at the root. In a max heap, the largest element is at the root.

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

Draw an example of a min heap.

A

10
/ \
15 30
/ \
40 50

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

Draw an example of a max heap.

A

50
/ \
30 20
/ \
10 15

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

What are the steps to delete the maximum element in a max heap?

A

Remove the root (maximum element).
Replace it with the last element in the heap.
Heapify down by comparing and swapping the new root with its larger child until the heap property is restored.

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

What is the heapsort algorithm?

A

Build a max heap from the unsorted array.
Swap the root (maximum element) with the last element.
Reduce heap size and heapify down.
Repeat until all elements are sorted.

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

What is the worst-case time complexity of HeapSort?

A

O(NlogN), because building the heap takes
O(N) and each extraction and heapification takes O(logN).

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

How does HeapSort compare to MergeSort and QuickSort?

A

HeapSort: O(NlogN) in both best and worst cases.
MergeSort: O(NlogN) in both best and worst cases.
QuickSort: O(NlogN) best-case, O(N ^2) worst-case without optimizations.

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

What is a priority queue?

A

A priority queue is a data structure where each element has a priority, and the element with the highest (or lowest) priority is dequeued first.

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

What are the main operations of a priority queue?

A

add(): Add an element with a priority.
get_max(): serves the element with the highest priority (removes it from the queue and returns it).

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

Name two scenarios where a priority queue is useful.

A

Task scheduling in operating systems, where high-priority tasks execute before lower-priority ones.
Dijkstra’s algorithm for finding the shortest path in graphs.

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

What is the best-case complexity for priority queue operations using a balanced BST?

A

O(logN) for insertion, deletion, and search.

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

What is the worst-case complexity for priority queue operations using an unbalanced BST?

A

O(N) for insertion, deletion, and search.

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