7 PRIORITY QUEUES AND HEAPS Flashcards

1
Q

What is a priority queue?

A

A class of data structures that retrieves items ordered by given scores for each item.

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

How do priority queues differ from stacks and queues?

A

Priority queues use an additional piece of information, the item’s priority, to determine the retrieval order.

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

What is an example of a real-world application of a priority queue?

A

Processing urgent requests first, such as choosing which patient to see first in a crowded emergency room.

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

What are the primary operations supported by a basic priority queue?

A
  • Add an item and its associated priority score. * Look up the item with the highest priority. * Remove the item with the highest priority.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a max heap?

A

A variant of the binary tree that maintains a special ordered relationship where the value at any node is larger than or equal to the values of its child nodes.

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

What is the max heap property?

A

The property that states the value at any node in the tree is larger than or equal to the values of its child nodes.

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

Who originally invented heaps?

A

Computer scientist J. W. J. Williams.

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

How are heaps typically implemented for efficiency?

A

Heaps are often visualized as trees but implemented with arrays.

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

In an array-based heap implementation, where is the root node stored?

A

At index 1 of the array.

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

What is the cost of looking up the maximum value in a max heap?

A

Constant time.

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

When adding a new element to a heap, what must be ensured?

A

The structure retains the heap property.

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

What does it mean to bubble up an element in a heap?

A

To swap the new element with its parent node until the heap property is restored.

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

What happens when the last index of the heap reaches the array size?

A

The heap size must be increased.

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

What is an efficient way to add a new element to a heap?

A

Add it to the first empty space in the bottom level and bubble it up.

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

What is a disadvantage of using a sorted linked list for a priority queue?

A

Adding new elements can be expensive as it may require traversing the entire list.

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

What is an advantage of using an unsorted linked list for a priority queue?

A

Adding new elements is trivial as they can be tagged onto the back of the list.

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

When should you prefer a sorted list over an unsorted list for a priority queue?

A

If lookups are more common.

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

What is the main challenge when both additions and lookups are common in a priority queue?

A

Prioritizing one operation over the other will lead to overall inefficiency.

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

What allows heaps to efficiently support operations required for priority queues?

A

The max heap’s simple structure.

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

What happens to the maximum value in a max heap when an element is added?

A

It remains at the root node.

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

How is the array for a heap typically preallocated?

A

It is large enough to accommodate the expected number of items.

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

In a max heap, what is the relationship between a node and its parent?

A

A node’s value must be less than or equal to its parent’s value.

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

What is the purpose of the Floor function in heap operations?

A

To compute the index of a node’s parent.

24
Q

What is the purpose of the HeapInsert function?

A

To add a new element to the heap and maintain the heap property

The function checks if there is room in the array, increases the size if necessary, and then inserts the new value while ensuring the heap structure is preserved.

25
What happens if the heap array is full during insertion?
The heap size is increased, possibly using the array-doubling technique ## Footnote This allows for the insertion of a new element without exceeding the allocated array size.
26
Describe the WHILE loop in the HeapInsert function.
It progresses up the heap, comparing the current value to its parent's value and swapping if necessary ## Footnote The loop continues until it reaches the root or finds a parent greater than or equal to the current value.
27
What analogy is used to describe the heap structure?
A warehouse floor organized with packages based on priority ## Footnote The highest priority package is at the front, with lower-priority packages behind it, reflecting the heap property.
28
What is the worst-case complexity for adding an element to a heap?
O(log N) ## Footnote This is due to the balanced nature of heaps, where the number of nodes doubles at each level.
29
What is the main operation when removing the highest-priority element from a heap?
Swapping the highest-priority node with the last node and restoring the heap property ## Footnote This involves maintaining the array structure while ensuring the heap condition is satisfied.
30
What is the first step in the HeapRemoveMax function?
Check if the heap is empty ## Footnote If the heap is empty, there is nothing to return.
31
What is done after swapping the root with the last element in HeapRemoveMax?
The last element is deleted, and the heap property must be restored ## Footnote This involves moving down the heap and comparing the new root with its children.
32
What data structure can be used to map items to their indices in a heap?
A hash table ## Footnote This allows for efficient updates when priorities of elements change.
33
What is a min heap?
A variation of the heap where the root node contains the smallest value ## Footnote This structure facilitates finding the item with the lowest score or priority.
34
How is the heap property defined in a min heap?
The value at any node is smaller than or equal to the values of its children ## Footnote This allows for efficient extraction of the minimum element.
35
Fill in the blank: The function to update an element's value in the heap is called _______.
UpdateValue
36
What happens if the priority of an element is increased?
The element is bubbled up the heap to restore the heap property ## Footnote This is similar to the insertion process.
37
What is the purpose of the IsLessThan function in a composite data structure?
To define how to compare elements based on priority ## Footnote This allows for the heap to store composite data while maintaining the correct order.
38
What is the worst-case complexity for restoring the heap property after removal?
O(log N) ## Footnote This complexity arises from the need to traverse down the tree to restore order.
39
True or False: A max heap can be used to implement a priority queue that prioritizes tasks with the lowest scores.
False ## Footnote A min heap is needed for prioritizing tasks with the lowest scores.
40
What is the structure of a TaskRecord used in a heap?
* Float: Priority * String: TaskName * String: Instructions * String: PersonWhoWillYellIfThisIsNotDone * Boolean: Completed ## Footnote This structure allows the heap to manage tasks along with their priorities.
41
What is the process for adding a new element to a min heap?
Insert the element and bubble it up if necessary, ensuring the min heap property is maintained ## Footnote The comparisons in the insertion process are adjusted to check for the minimum value.
42
What is the purpose of the min heap?
The min heap allows efficient access to the element with the lowest score.
43
What is the first step in the MinHeapInsert algorithm?
Check if heap.last_index == heap.array_size - 1.
44
In MinHeapInsert, what is done when the heap is full?
Increase Heap size.
45
What condition does the WHILE loop check in MinHeapInsert?
parent >= 1 AND (heap.array[parent] > heap.array[current])
46
What changes are made in MinHeapRemoveMin compared to max heap removal?
Replace < with > when determining whether to swap nodes.
47
What is heapsort?
An algorithm for sorting a list of items using the heap data structure.
48
What are the two phases of heapsort?
* Building a max heap from all the items * Extracting all items in decreasing sorted order
49
What is the first action taken in the heapsort algorithm?
Insert each item into a temporary heap.
50
What is the worst-case runtime for constructing a heap out of N items?
Proportional to N log2(N).
51
In heapsort, what happens during the second stage?
Items are removed from the heap one by one in order of decreasing priority.
52
What happens to the root during the extraction phase of heapsort?
The root is extracted, and the last item in the heap is swapped to the root's position.
53
What is the total worst-case runtime of the heapsort algorithm?
Proportional to N log2(N).
54
What is a key characteristic of heaps compared to binary search trees?
Heaps allow a different set of computationally efficient operations.
55
Why do both addition and deletion operations in heaps remain efficient?
They require walking at most one path between the top and bottom of the tree.
56
What is one trade-off when using heaps instead of binary search trees?
We can no longer efficiently search for a specific value.
57
Fill in the blank: Heaps are a simple variation of ______ that allow a different set of operations.
[binary trees]