6-28 最小堆插入元素和删除堆顶(无哨兵元素) (20 分)

这篇博客介绍了如何在C语言中实现一个最小堆(优先队列),包括向堆中插入元素和删除堆顶元素的函数。提供的代码示例展示了插入和删除操作,并给出了测试用例及输出。通过`insertIntoHeap`和`deleteMin`两个函数,实现了堆数据结构的基本操作。
摘要由CSDN通过智能技术生成

对于给定的最小堆(优先队列),分别实现插入元素和删除堆顶的函数。

函数接口定义:

int insertIntoHeap(struct Heap* h, int x);  // 向堆中插入元素x
int deleteMin(struct Heap* h, int* pElement);  //将堆顶元素拷贝到pElement所指变量并删除堆顶元素

其中,hxpElement为参数,h是堆结构体指针,x是待插入元素的值,pElement指向的变量用于存放删除的堆顶元素。
堆结构体定义如下:

struct Heap{
    int *data;  // 堆元素存储空间指针,堆元素从data[1]开始存放,data[0]不使用,无哨兵元素
    int capacity; // 堆容量
    int size; // 堆元素数量
};

裁判测试程序样例:

#include <stdio.h>
#include <stdlib.h>
struct Heap{
    int *data;
    int capacity;
    int size;
};
struct Heap* initHeap(int capacity){   // 初始化堆
    struct Heap* h;
    h = (struct Heap*)malloc(sizeof(struct Heap));
    if(!h) return NULL;
    h->data = (int*)malloc(sizeof(int)*capacity+1);
    if(h->data == NULL){
        free(h);
        return NULL;
    }
    h->capacity = capacity;
    h->size = 0;
    return h;
};
void printHeap(struct Heap* h){  // 打印堆元素
    /* 细节省略 */
}
int insertIntoHeap(struct Heap* h, int x);
int deleteMin(struct Heap* h, int* pElement);
int main(){
    struct Heap *h;
    int n;
    scanf("%d", &n);   // 输入堆容量
    h = initHeap(n);
    int op, x;
    scanf("%d", &op);
    while(op){    // 输入操作: -1表示删除   1表示插入   0表示结束
        if(op == 1){
            scanf("%d", &x);
            printf("Insertion %s. ", insertIntoHeap(h, x) ? "succeeded" : "failed" );
            printHeap(h);
        }
        else{
            if (deleteMin(h, &x) ) printf("%d deleted. ", x);
            else printf("Deletion failed. ");
            printHeap(h);
        }
        scanf("%d", &op);
    }
    return 0;
}

/*你提交的代码将被嵌在这里 */

输入样例:

对于样例测试程序规定的输入格式:

3
1 10
1 20
1 5
1 40
-1
-1
-1
-1
0

输出样例:

样例测试程序的输出:

Insertion succeeded. 10, 
Insertion succeeded. 10, 20, 
Insertion succeeded. 5, 20, 10, 
Insertion failed. 5, 20, 10, 
5 deleted. 10, 20, 
10 deleted. 20, 
20 deleted. 
Deletion failed. 
int insertIntoHeap(struct Heap* h, int x) {
	if (h->capacity == h->size)return 0;
	int i = ++h->size;
	for (; h->data[i / 2] > x && i > 1; i /= 2)
		h->data[i] = h->data[i / 2];
	h->data[i] = x;
	return 1;
}
int deleteMin(struct Heap* h, int* pElement) {
	if (!h->size)return 0;
	*pElement = h->data[1];
	int t = h->data[h->size--];
	int parent, child;
	for (parent = 1; parent * 2 <= h->size; parent = child) {
		child = parent * 2;
		if (child != h->size && h->data[child] > h->data[child + 1])
			child++;
		if (h->data[child] > t)break;
		else h->data[parent]=h->data[child]
	}
	h->data[parent] = t;
	return 1;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值