1/120
Looks like no tags are added yet.
What is a queue ADT?
A collection of objects that are added and removed according to the First-In, First-Out (FIFO) principle.
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).
What determines the priority of an element in a priority queue?
Its associated key, which is provided when the element is inserted.
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.
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).
What does insert(k, v) do?
Creates an entry with key k and value v in the priority queue.
What does min() do?
Returns the entry (k, v) with the smallest key without removing it. Returns null if empty.
What does removeMin() do?
Removes and returns the entry with the smallest key. Returns null if empty.
What does size() do?
Returns the number of entries in the priority queue.
What does isEmpty() do?
Returns true if the priority queue is empty and false otherwise.
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.
What types can keys and values be?
Any type of object, provided keys can be meaningfully compared.
What is an Entry composite?
An object that combines a key (k) and a value (v) into a single object.
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.
What does Entry
An interface that stores a key of type K and a value of type V.
What does getKey() do?
Returns the key stored in an Entry.
What does getValue() do?
Returns the value stored in an Entry.
Why must keys have a total order?
So that keys can be compared meaningfully and a minimum key can always be identified.
What is a total order?
A comparison relationship that establishes a consistent linear ordering among elements.
What is a minimal key?
A key kmin such that kmin ≤ k for every other key k in the collection.
What is the Comparable interface?
A Java interface that defines the natural ordering of objects using the compareTo() method.
What does compareTo() return when a < b?
A negative integer.
What does compareTo() return when a = b?
Zero.
What does compareTo() return when a > b?
A positive integer.
What is natural ordering?
The default ordering of objects defined by their class through the Comparable interface.
What is a Comparator?
An external object that defines how two objects should be compared, independently of their natural ordering.
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.
What does compare(a, b) return when a < b?
A negative integer.
What does compare(a, b) return when a = b?
Zero.
What does compare(a, b) return when a > b?
A positive integer.
What does a StringLengthComparator compare?
The lengths of two strings rather than their alphabetical order.
What happens when comparing "Mary" and "Hadassah" alphabetically using compare()?
A negative result because "Mary" comes before "Hadassah" alphabetically.
What happens when comparing "Mary" and "Hadassah" by length?
A positive result because "Mary" has 4 characters and "Hadassah" has 8.
How can you use a Comparator to sort an ArrayList?
Use Collections.sort(list, comparator).
How does a general priority queue allow different key types?
It accepts a Comparator object through its constructor.
What happens if no Comparator is provided?
The priority queue uses the natural ordering of keys through a default comparator.
What is AbstractPriorityQueue?
An abstract base class that manages common functionality shared by priority queue implementations.
What is PQEntry?
A nested class inside AbstractPriorityQueue that implements the Entry interface.
What is an unsorted-list priority queue?
A priority queue that stores entries in a positional list without sorting them.
How does insert() work in an unsorted-list priority queue?
Creates a PQEntry and adds it to the end of the list.
What is the time complexity of insert() in an unsorted-list priority queue?
O(1).
How does min() work in an unsorted-list priority queue?
Scans the entire list to find the entry with the smallest key.
How does removeMin() work in an unsorted-list priority queue?
Scans the list to find and remove the entry with the smallest key.
What is the time complexity of min() in an unsorted-list priority queue?
O(n).
What is the time complexity of removeMin() in an unsorted-list priority queue?
O(n).
What is a sorted-list priority queue?
A priority queue that stores entries in a positional list sorted by nondecreasing keys.
How are entries organized in a sorted-list priority queue?
The smallest key is at the beginning of the list.
How does min() work in a sorted-list priority queue?
Returns the first entry in the list.
How does removeMin() work in a sorted-list priority queue?
Removes and returns the first entry in the list.
What is the time complexity of min() in a sorted-list priority queue?
O(1).
What is the time complexity of removeMin() in a sorted-list priority queue?
O(1).
How does insert() work in a sorted-list priority queue?
Scans the list to find the correct position and inserts the new entry.
What is the time complexity of insert() in a sorted-list priority queue?
O(n) worst case.
What is the main advantage of an unsorted-list priority queue?
Fast insertion: O(1).
What is the main disadvantage of an unsorted-list priority queue?
Slow minimum lookup and removal: O(n).
What is the main advantage of a sorted-list priority queue?
Fast minimum lookup and removal: O(1).
What is the main disadvantage of a sorted-list priority queue?
Slow insertion: O(n).
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.
What is a binary heap?
A binary tree-based data structure used to implement a priority queue efficiently.
What is the main advantage of a heap-based priority queue?
Both insertion and removal can be performed in O(log n) time.
What are the two main properties of a heap?
Heap-order property and complete binary tree property.
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.
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.
What is the height of a complete binary tree with n nodes?
O(log n).
Where is the minimum key stored in a min-heap?
At the root of the tree.
What happens to keys along a path from the root to a leaf in a min-heap?
They are in nondecreasing order.
Does a heap have to be completely sorted?
No. It only needs to satisfy the heap-order property and complete binary tree property.
Why must a heap be complete?
To keep its height as small as possible, allowing efficient operations.
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.
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.
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.
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.
Why is up-heap bubbling necessary?
Because inserting a new entry may violate the heap-order property.
What happens during each up-heap swap?
The new entry moves one level closer to the root.
What is the worst-case time complexity of up-heap bubbling?
O(log n).
Where is the minimum entry located before removeMin()?
At the root of the heap.
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.
What is the first step of removeMin() in a heap?
Remove the last node (rightmost node at the bottommost level).
What happens to the entry at the last position during removeMin()?
It is moved to the root to replace the minimum entry.
Why is the last node removed during removeMin()?
To maintain the complete binary tree property.
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.
Which child is selected during down-heap bubbling when a node has two children?
The child with the smaller key.
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.
What happens during down-heap bubbling if a node has only a left child?
The left child is selected for comparison.
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.
What happens if the heap has only one node during removeMin()?
The root is removed, and no down-heap bubbling is necessary.
What is the worst-case time complexity of down-heap bubbling?
O(log n).
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.
What index is assigned to the root in an array-based binary tree?
0.
What is the formula for the left child of index j?
2j + 1.
What is the formula for the right child of index j?
2j + 2.
What is the formula for the parent of index j?
floor((j - 1) / 2).
How do you check if a node at index j has a left child?
Check whether 2j + 1 < n.
How do you check if a node at index j has a right child?
Check whether 2j + 2 < n.
What is the index of the last node in an array-based heap of size n?
n - 1.
What are the valid indices in an array-based heap of size n?
0 through n - 1.
Why is an array-based representation suitable for a complete binary tree?
Because its nodes occupy contiguous array indices without gaps.
What is a LinkedHeap?
A heap implemented using a linked binary tree representation instead of an array.
What is the time complexity of min() in a heap-based priority queue?
O(1), because the minimum entry is at the root.
What is the time complexity of insert() in a heap-based priority queue?
O(log n).