Heaps and Priority Queues Flashcards

1
Q

What is the main difference between a Priority Queue and a regular Queue?

A

The main difference between a Priority Queue and a regular Queue is that in a Priority Queue, all items inserted into the queue have a priority, and the front of the queue is always the item with the highest priority.

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

What are the main operations supported by a Priority Queue?

A

A Priority Queue supports operations such as enqueue, dequeue, peek, and isEmpty.

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

What is the behavior of the isEmpty operation in a Priority Queue?

A

The isEmpty operation in a Priority Queue returns true if there are no elements in the priority queue, and false otherwise.

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

What is the behavior of the enqueue operation in a Priority Queue?

A

The enqueue operation adds an item to the Priority Queue in the right priority order.

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

What is the behavior of the dequeue operation in a Priority Queue?

A

The dequeue operation removes the item with the highest priority from the front of the priority queue.

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

What is the behavior of the peek operation in a Priority Queue?

A

The peek operation returns the item at the front of the priority queue without removing it.

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

What is an ADT used to implement Priority Queues and Heaps?

A

The ADT used to implement Priority Queues and Heaps is called a Heap.

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

How are Heaps commonly implemented?

A

Heaps are commonly implemented using linked lists.

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

What is the insertion order for Max Heaps?

A

Max Heaps grow using the level order of the tree for insertion.

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

How is the Heap ADT implemented with links?

A

In the implementation of the Heap ADT using links, every node of the tree is greater than both its children.

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

How is the isEmpty operation implemented in a Heap?

A

To check if a Heap is empty, the implementation checks if the root of the tree is null. If the root is null, it returns true, indicating that the Heap is empty.

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

How is the enqueue operation implemented in a Heap implemented with links?

A

The enqueue operation in a Heap implemented with links inserts an item at the leaves of the tree. If the item is out of order, it is swapped with its parent to ensure the correct order of priorities.

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

How is the dequeue operation implemented in a Heap implemented with links?

A

The dequeue operation in a Heap implemented with links removes the element at the root of the tree. After removal, if there are remaining elements in the Heap, the item previously placed at the leaves is moved to the root position, and then it is swapped with its appropriate child nodes to restore the Heap property.

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

How is the peek operation implemented in a Heap implemented with links?

A

The peek operation in a Heap implemented with links returns the contents of the root if it is not null.

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

When performing insertions in a Heap implemented with links, is it possible for there to be multiple swaps involved?

A

Yes, an insertion in a Heap implemented with links can involve multiple swaps

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

How are swaps implemented in a Heap implemented with links?

A

In a Heap implemented with links, swaps are performed by comparing the keys of the parent and child nodes. If the key of the child is greater than the key of the parent, the references of the elements are swapped.

17
Q

What is a Priority Queue and how does it differ from a regular queue?

A

A Priority Queue is a data structure that stores elements along with their associated priorities. Unlike a regular queue, where elements are processed in the order of their arrival, a Priority Queue processes elements based on their priority. The element with the highest priority is always at the front of the queue and is processed first.

18
Q

What is a Heap in the context of Priority Queues?

A

A Heap is a specialized binary tree structure used to implement Priority Queues. It ensures that the highest (or lowest) priority element is always at the root of the tree. Heaps are efficient for maintaining the priority order of elements, allowing for fast insertion and removal operations.

19
Q

What are the two types of Heaps commonly used in Priority Queues?

A

The two commonly used types of Heaps in Priority Queues are Max Heaps and Min Heaps. In a Max Heap, the element with the highest priority is at the root, while in a Min Heap, the element with the lowest priority is at the root.

20
Q

How is the enqueue operation implemented in a Priority Queue using a Max Heap?

A

To enqueue an element in a Priority Queue implemented with a Max Heap, the element is inserted at the bottom of the heap (i.e., as a new leaf node). Then, it is recursively swapped with its parent until the heap property is satisfied, which ensures that the element with the highest priority moves to the correct position at the root.

21
Q

What is the time complexity of the enqueue operation in a Priority Queue implemented with a Max Heap?

A

The time complexity of the enqueue operation in a Priority Queue implemented with a Max Heap is O(log n), where n is the number of elements in the heap. This is because the element may need to be swapped with its parent multiple times as it moves towards the root.

22
Q

How is the dequeue operation implemented in a Priority Queue using a Max Heap?

A

To dequeue an element from a Priority Queue implemented with a Max Heap, the element at the root (i.e., the element with the highest priority) is removed. Then, the last element in the heap is moved to the root position, and it is recursively swapped with its largest child until the heap property is restored.

23
Q

What is the time complexity of the dequeue operation in a Priority Queue implemented with a Max Heap?

A

The time complexity of the dequeue operation in a Priority Queue implemented with a Max Heap is O(log n), where n is the number of elements in the heap. Similar to enqueue, the element may need to be swapped with its child multiple times as it moves down the heap to its correct position.

24
Q

How is the peek operation implemented in a Priority Queue using a Max Heap?

A

The peek operation in a Priority Queue implemented with a Max Heap simply returns the element at the root, which is the element with the highest priority. The element is not removed from the heap.

25
Q

What is the time complexity of the peek operation in a Priority Queue implemented with a Max Heap?

A

The time complexity of the peek operation in a Priority Queue implemented with a Max Heap is O(1). It is a constant-time operation as it directly accesses the element at the root of the heap.