1.插入

void put(int x) {
	heap[++len] = x;
	int fa, now = len;
	while(now > 1) {
		fa = now >> 1;
		if(heap[fa] <= heap[now]) break;
		swap(heap[fa], heap[now]);
		now = fa;
	}
	return;
}

2.返回堆顶值

int get() {
	return heap[1];
}

3.取出堆顶值

void pop() {
	heap[1] = heap[len--];
	int son, now = 1;
	while((now << 1) <= len) {
		son = now << 1;
		if(heap[son + 1] <= heap[son]) son = son + 1;
		if(heap[now] <= heap[son]) break;
		swap(heap[now], heap[son]);
		now = son;
	}
	return;
}
posted @ 2020-10-07 19:35  C2022lihan  阅读(25)  评论(0)    收藏  举报