Heaps and Priority Queues Flashcards
What is the main difference between a Priority Queue and a regular Queue?
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.
What are the main operations supported by a Priority Queue?
A Priority Queue supports operations such as enqueue, dequeue, peek, and isEmpty.
What is the behavior of the isEmpty operation in a Priority Queue?
The isEmpty operation in a Priority Queue returns true if there are no elements in the priority queue, and false otherwise.
What is the behavior of the enqueue operation in a Priority Queue?
The enqueue operation adds an item to the Priority Queue in the right priority order.
What is the behavior of the dequeue operation in a Priority Queue?
The dequeue operation removes the item with the highest priority from the front of the priority queue.
What is the behavior of the peek operation in a Priority Queue?
The peek operation returns the item at the front of the priority queue without removing it.
What is an ADT used to implement Priority Queues and Heaps?
The ADT used to implement Priority Queues and Heaps is called a Heap.
How are Heaps commonly implemented?
Heaps are commonly implemented using linked lists.
What is the insertion order for Max Heaps?
Max Heaps grow using the level order of the tree for insertion.
How is the Heap ADT implemented with links?
In the implementation of the Heap ADT using links, every node of the tree is greater than both its children.
How is the isEmpty operation implemented in a Heap?
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 is the enqueue operation implemented in a Heap implemented with links?
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 is the dequeue operation implemented in a Heap implemented with links?
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 is the peek operation implemented in a Heap implemented with links?
The peek operation in a Heap implemented with links returns the contents of the root if it is not null.
When performing insertions in a Heap implemented with links, is it possible for there to be multiple swaps involved?
Yes, an insertion in a Heap implemented with links can involve multiple swaps