《数据结构与算法分析——c语言描述》 练习6.20 答案
先删除再插入
static swapLeftRightSon(PriorityQueue h) {
PriorityQueue temp;
temp = h->right;
h->right = h->left;
h->left = temp;
}
static PriorityQueue deleteKey_internal(ElementType X, ElementType delta, int *isFind, PriorityQueue h) {
if (h) {
if (X > h->element) {
h->left = deleteKey_internal(X, delta, isFind, h->left);
if (*isFind == 0) {//没有找到
h->right = deleteKey_internal(X, delta, isFind, h->right);
}
if (h->left == NULL) {
h->left = h->right;
h->right = NULL;
h->np1 = 0;
}
else if (h->right == NULL) {
h->np1 = 0;
}
else {
if (h->right->np1 > h->left->np1)
swapLeftRightSon(h);
h->np1 = h->right->np1 + 1;
}
}
else if (X == h->element) {
PriorityQueue newMergeRoot = merge(h->left, h->right);
free(h);
*isFind = 1;
h = newMergeRoot;
}
}
return h;
}
PriorityQueue decreaseKey(ElementType X, ElementType delta, PriorityQueue h) {
int isFind = 0;
h = deleteKey_internal(X, delta, &isFind, h);
insert(X - delta, h);
return h;
}