# 优先队列

《算法导论》第3版6.5讲解了优先队列

1) insert(S, x): 把元素x插入集合S中，这一操作等价于S=S∪{x}

2) maximum(S): 返回S中具有最大键字的元素

3) extract_max(S): 去掉并返回S中具有最大键字的元素

4) increase_key(S, x, k): 将元素x的关键字增加到k，这里假设k的值不小于x的原关键字值

#### 1）max_heap_insert:

void max_heap_insert(int A[], int key)
{
++heap_size;
A[heap_size - 1] = INT_MIN;
heap_increase_key(A, heap_size - 1, key);
}

#### 2）heap_maximum:

int heap_maximum(int A[])
{
return A[0];
}

#### 3）heap_extract_max:

int heap_extract_max(int A[])
{
if (heap_size < 1)
{
cout << "heap underflow" << endl;
return -1;
}

int max = A[0];
A[0] = A[heap_size - 1];
--heap_size;
max_heapify(A, 0);

return max;
}

#### 4）heap_increase_key:

void heap_increase_key(int A[], int i, int key)
{
if (key < A[i])
{
cout << "new key is smaller than current key" << endl;
return;
}

A[i] = key;
while (i > 0 && A[parent(i)] < A[i])
{
swap(A[i], A[parent(i)]);
i = parent(i);
}
}

void heap_increase_key_oneassign(int A[], int i, int key)
{
if (key < A[i])
{
cout << "new key is smaller than current key" << endl;
return;
}

while (i > 0 && A[parent(i)] < key)
{
A[i] = A[parent(i)];
i = parent(i);
}

A[i] = key;
}

heap_delete(A, i)操作能够将结点i从堆A中删除。对于一个包含n个元素的堆，请设计一个能够在O(logn)时间内完成的heap_delete操作。

void heap_delete(int A[], int i)
{
swap(A[i], A[heap_size - 1]);
--heap_size;
max_heapify(A, i);
}

void heap_delete_thevalue(int A[], int target)
{
int tag = -1;
for (int i = 0; i < heap_size; ++i)
{
if (A[i] == target)
{
tag = i;
break;
}
}

if (tag != -1)
heap_delete(A, tag);
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客