4 Priority Queues(Heaps)¶
ADT
Objects: A finite ordered list with zero or more elements.
Operations:
- PriorityQueue Initialize(int MaxElements);
- void Insert(ElementType X, PriorityQueue H);
- ElementType DeleteMin(PriorityQueue H);
- ElementType FindMin(PriorityQueue H);
4.1 Simple Implementations¶
Linked list is better since there is never more deletions than insertion.
- Array:
- Insertion -- add one item at the end ~
- Deletion
- find the largest/smallest key ~
- remove the item and shift array ~
- find the largest/smallest key ~
- Insertion -- add one item at the end ~
- Linked List:
- Insertion -- add to the front of the chain ~
- Deletion
- find the largest/smallest key ~
- remove the item ~
- find the largest/smallest key ~
- Insertion -- add to the front of the chain ~
- Ordered Array:
- Insertion
- find the proper position ~
- shift array and add the item ~
- find the proper position ~
- Deletion -- remove the item ~
- Insertion
- Ordered Linked List:
- Insertion
- find the proper position ~
- add the item ~
- find the proper position ~
- Deletion -- remove the first/last item ~
- Insertion
4.2 Binary Heap¶
4.2.1 Structure Property:¶
A binary tree with
A complete binary tree of height
has between and nodes.
if logN is not an integer, then h++.
We can use an array BT[n+1]
to represent it.(BT[0] is not used)
If a complete binary tree with
index of
index of
index of
4.2.2 Heap Order Property¶
A min tree is a tree in which the key value in each node is no larger than the key values in its children (if any). A min heap is a complete binary min tree.
4.2.3 Basic Heap Operations¶
4.2.3.1 Insertion¶
The possible position for a new node is only since a heap must be a complete binary tree.
/*H->Elements[0] is a sentinel*/
void Insert(ElementType X, PriorityQueue H)
{
int i;
if(IsFull(H))
{
Error("Priority queue is full");
return;
}
for( i=++H->Size; H->Elements[i/2]; i/=2)
H->Elements[i] = H->Elements[i/2];
H->Elements[i]=X;
}
4.2.3.2 DeleteMin¶
ElementType DeleteMin(PriorityQueue H)
{
/*Percolate Down*/
int i, Child;
ElementType MinElement, LastElement;
if(IsEmpty(H))
{
Error("Priority queue is empty");
return H->Elements[0];
}
MinElement=H->Elements[1];
LastElement=H->Elements[H->Size--];
for(i=1;i*2<=H->Size;i=Child)
{
Child=i*2;
if(Child!=H->Size && H->Elements[Child+1]<H->Elements[Child])
Child++;
if(LastElement>H->Elements[Child])
H->Elements[i]=H->Elements[Child];
else
break;
}
H->Elements[i]=LastElement;
return MinElement;
}
4.2.3.3 Other Heap Operations¶
Finding any key except the minimum one will have to take a linear scan through the entire heap.
4.2.3.3.1 DecreaseKey(P, ,H)¶
Percolate up
4.2.3.3.2 IncreaseKey(P, ,H)¶
Percolate down
4.2.3.3.3 Delete(P,H)¶
DecreaseKey(P,
4.2.3.3.4 BuildHeap(H)¶
N successive Insertions
Time complexity is too low.
First, use the keys build a complete binary tree. Then, percolate down from the last parent node in
Note
[Theorem] For the prefect binary tree of height
T(N) = O(N)
4.3 d-Heaps¶
All nodes have
children.
- Shall we make
as large as possible? - DeleteMin will take
comparsions to find the smallest child. Hence the total time complexity would be or is merely a bit shift, but or is not.- When the prtority queue is too large to fit entirely in main memory, a d-heap will become interesting.
创建日期: 2023年11月15日 17:30:52