Priority Queues

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play

1/120

encourage image

There are no tags or description

Looks like no tags are added yet.

121 Terms

1
New cards

What is a queue ADT?

A collection of objects that are added and removed according to the First-In, First-Out (FIFO) principle.

2
New cards

What is a priority queue?

A collection of prioritized elements that allows arbitrary insertion and removal of the element with the highest priority (smallest key).

3
New cards

What determines the priority of an element in a priority queue?

Its associated key, which is provided when the element is inserted.

4
New cards

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

An airline standby list, where passengers are prioritized based on check-in time, fare paid, and frequent-flyer status.

5
New cards

What is the difference between a regular queue and a priority queue?

A regular queue follows FIFO, while a priority queue removes elements based on priority (key).

6
New cards

What does insert(k, v) do?

Creates an entry with key k and value v in the priority queue.

7
New cards

What does min() do?

Returns the entry (k, v) with the smallest key without removing it. Returns null if empty.

8
New cards

What does removeMin() do?

Removes and returns the entry with the smallest key. Returns null if empty.

9
New cards

What does size() do?

Returns the number of entries in the priority queue.

10
New cards

What does isEmpty() do?

Returns true if the priority queue is empty and false otherwise.

11
New cards

Can a priority queue contain multiple entries with equivalent keys?

Yes. min() and removeMin() may return any one of the entries with the smallest key.

12
New cards

What types can keys and values be?

Any type of object, provided keys can be meaningfully compared.

13
New cards

What is an Entry composite?

An object that combines a key (k) and a value (v) into a single object.

14
New cards

Why do priority queues use Entry objects?

To keep track of both the key and its associated value when entries move within a data structure.

15
New cards

What does Entry

An interface that stores a key of type K and a value of type V.

16
New cards

What does getKey() do?

Returns the key stored in an Entry.

17
New cards

What does getValue() do?

Returns the value stored in an Entry.

18
New cards

Why must keys have a total order?

So that keys can be compared meaningfully and a minimum key can always be identified.

19
New cards

What is a total order?

A comparison relationship that establishes a consistent linear ordering among elements.

20
New cards

What is a minimal key?

A key kmin such that kmin ≤ k for every other key k in the collection.

21
New cards

What is the Comparable interface?

A Java interface that defines the natural ordering of objects using the compareTo() method.

22
New cards

What does compareTo() return when a < b?

A negative integer.

23
New cards

What does compareTo() return when a = b?

Zero.

24
New cards

What does compareTo() return when a > b?

A positive integer.

25
New cards

What is natural ordering?

The default ordering of objects defined by their class through the Comparable interface.

26
New cards

What is a Comparator?

An external object that defines how two objects should be compared, independently of their natural ordering.

27
New cards

Why would you use a Comparator instead of Comparable?

When you want to compare objects using an alternative ordering, such as string length instead of alphabetical order.

28
New cards

What does compare(a, b) return when a < b?

A negative integer.

29
New cards

What does compare(a, b) return when a = b?

Zero.

30
New cards

What does compare(a, b) return when a > b?

A positive integer.

31
New cards

What does a StringLengthComparator compare?

The lengths of two strings rather than their alphabetical order.

32
New cards

What happens when comparing "Mary" and "Hadassah" alphabetically using compare()?

A negative result because "Mary" comes before "Hadassah" alphabetically.

33
New cards

What happens when comparing "Mary" and "Hadassah" by length?

A positive result because "Mary" has 4 characters and "Hadassah" has 8.

34
New cards

How can you use a Comparator to sort an ArrayList?

Use Collections.sort(list, comparator).

35
New cards

How does a general priority queue allow different key types?

It accepts a Comparator object through its constructor.

36
New cards

What happens if no Comparator is provided?

The priority queue uses the natural ordering of keys through a default comparator.

37
New cards

What is AbstractPriorityQueue?

An abstract base class that manages common functionality shared by priority queue implementations.

38
New cards

What is PQEntry?

A nested class inside AbstractPriorityQueue that implements the Entry interface.

39
New cards

What is an unsorted-list priority queue?

A priority queue that stores entries in a positional list without sorting them.

40
New cards

How does insert() work in an unsorted-list priority queue?

Creates a PQEntry and adds it to the end of the list.

41
New cards

What is the time complexity of insert() in an unsorted-list priority queue?

O(1).

42
New cards

How does min() work in an unsorted-list priority queue?

Scans the entire list to find the entry with the smallest key.

43
New cards

How does removeMin() work in an unsorted-list priority queue?

Scans the list to find and remove the entry with the smallest key.

44
New cards

What is the time complexity of min() in an unsorted-list priority queue?

O(n).

45
New cards

What is the time complexity of removeMin() in an unsorted-list priority queue?

O(n).

46
New cards

What is a sorted-list priority queue?

A priority queue that stores entries in a positional list sorted by nondecreasing keys.

47
New cards

How are entries organized in a sorted-list priority queue?

The smallest key is at the beginning of the list.

48
New cards

How does min() work in a sorted-list priority queue?

Returns the first entry in the list.

49
New cards

How does removeMin() work in a sorted-list priority queue?

Removes and returns the first entry in the list.

50
New cards

What is the time complexity of min() in a sorted-list priority queue?

O(1).

51
New cards

What is the time complexity of removeMin() in a sorted-list priority queue?

O(1).

52
New cards

How does insert() work in a sorted-list priority queue?

Scans the list to find the correct position and inserts the new entry.

53
New cards

What is the time complexity of insert() in a sorted-list priority queue?

O(n) worst case.

54
New cards

What is the main advantage of an unsorted-list priority queue?

Fast insertion: O(1).

55
New cards

What is the main disadvantage of an unsorted-list priority queue?

Slow minimum lookup and removal: O(n).

56
New cards

What is the main advantage of a sorted-list priority queue?

Fast minimum lookup and removal: O(1).

57
New cards

What is the main disadvantage of a sorted-list priority queue?

Slow insertion: O(n).

58
New cards

What is the trade-off between sorted and unsorted priority queues?

Unsorted lists have fast insertions but slow removals, while sorted lists have slow insertions but fast removals.

59
New cards

What is a binary heap?

A binary tree-based data structure used to implement a priority queue efficiently.

60
New cards

What is the main advantage of a heap-based priority queue?

Both insertion and removal can be performed in O(log n) time.

61
New cards

What are the two main properties of a heap?

Heap-order property and complete binary tree property.

62
New cards

What is the heap-order property?

For every node other than the root, its key must be greater than or equal to its parent's key.

63
New cards

What is the complete binary tree property?

Every level except possibly the last is completely filled, and the last level is filled from left to right.

64
New cards

What is the height of a complete binary tree with n nodes?

O(log n).

65
New cards

Where is the minimum key stored in a min-heap?

At the root of the tree.

66
New cards

What happens to keys along a path from the root to a leaf in a min-heap?

They are in nondecreasing order.

67
New cards

Does a heap have to be completely sorted?

No. It only needs to satisfy the heap-order property and complete binary tree property.

68
New cards

Why must a heap be complete?

To keep its height as small as possible, allowing efficient operations.

69
New cards

What is the first step when inserting an entry into a heap?

Add the new entry at the next available position to maintain the complete binary tree property.

70
New cards

Where is the next available position in a heap?

Just after the rightmost node at the bottom level, or the leftmost position of a new level.

71
New cards

What is up-heap bubbling?

The process of moving a newly inserted entry upward by swapping it with its parent when its key is smaller.

72
New cards

When does up-heap bubbling stop?

When the entry reaches the root or its key is greater than or equal to its parent's key.

73
New cards

Why is up-heap bubbling necessary?

Because inserting a new entry may violate the heap-order property.

74
New cards

What happens during each up-heap swap?

The new entry moves one level closer to the root.

75
New cards

What is the worst-case time complexity of up-heap bubbling?

O(log n).

76
New cards

Where is the minimum entry located before removeMin()?

At the root of the heap.

77
New cards

Why can't we simply delete the root of a heap?

Deleting the root would leave two disconnected subtrees and violate the complete binary tree property.

78
New cards

What is the first step of removeMin() in a heap?

Remove the last node (rightmost node at the bottommost level).

79
New cards

What happens to the entry at the last position during removeMin()?

It is moved to the root to replace the minimum entry.

80
New cards

Why is the last node removed during removeMin()?

To maintain the complete binary tree property.

81
New cards

What is down-heap bubbling?

The process of moving an entry downward by swapping it with its child when the heap-order property is violated.

82
New cards

Which child is selected during down-heap bubbling when a node has two children?

The child with the smaller key.

83
New cards

Why is the smaller child selected during down-heap bubbling?

To ensure the parent is smaller than or equal to both children after the swap.

84
New cards

What happens during down-heap bubbling if a node has only a left child?

The left child is selected for comparison.

85
New cards

When does down-heap bubbling stop?

When the current key is smaller than or equal to the selected child's key, or the node becomes a leaf.

86
New cards

What happens if the heap has only one node during removeMin()?

The root is removed, and no down-heap bubbling is necessary.

87
New cards

What is the worst-case time complexity of down-heap bubbling?

O(log n).

88
New cards

What is an array-based representation of a complete binary tree?

A representation where tree nodes are stored in an array according to their level-order indices.

89
New cards

What index is assigned to the root in an array-based binary tree?

0.

90
New cards

What is the formula for the left child of index j?

2j + 1.

91
New cards

What is the formula for the right child of index j?

2j + 2.

92
New cards

What is the formula for the parent of index j?

floor((j - 1) / 2).

93
New cards

How do you check if a node at index j has a left child?

Check whether 2j + 1 < n.

94
New cards

How do you check if a node at index j has a right child?

Check whether 2j + 2 < n.

95
New cards

What is the index of the last node in an array-based heap of size n?

n - 1.

96
New cards

What are the valid indices in an array-based heap of size n?

0 through n - 1.

97
New cards

Why is an array-based representation suitable for a complete binary tree?

Because its nodes occupy contiguous array indices without gaps.

98
New cards

What is a LinkedHeap?

A heap implemented using a linked binary tree representation instead of an array.

99
New cards

What is the time complexity of min() in a heap-based priority queue?

O(1), because the minimum entry is at the root.

100
New cards

What is the time complexity of insert() in a heap-based priority queue?

O(log n).