二叉堆的实现课堂笔记

手写二叉堆

Posted by 周琪岳 on July 27, 2021

基本定义

堆:完全二叉树,其左右子树仍然是堆,保证堆顶元素是该堆中元素中最大的(最小的)

存储时,按照顺序,用数组存储

基本操作

  • push:插入

  • pop:删除

  • 返回堆顶元素

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>

using namespace std;

const int MAXN = 1e3 + 100;
int heap[MAXN], size;

void push(int e) { // logn
	size ++;
	int idx = size;
	while(idx / 2) {
		if(heap[idx/2] < e) {
			heap[idx] = heap[idx/2];
			idx /= 2;
		} else {
			break;
		}
	}
	heap[idx] = e;
}
void pop() { // logn 
	int e = heap[size];
	size --;
	int idx = 1;
	while(idx*2 <= size) { // 左孩子存在 
		int maxidx = idx*2;
		if(idx*2 + 1 <= size && heap[idx*2+1] > heap[idx*2]) {
			maxidx = idx*2 + 1;		
		}
		if(heap[maxidx] > e) {
			heap[idx] = heap[maxidx];
			idx = maxidx;
		} else {
			break;
		}
	}
	heap[idx] = e;
}
int top() {
	return heap[1];
}

void show() { // 方便调试的函数
	for(int i=1; i<=size; i++)
		cout << "二叉堆上" << i << "结点的值:" << heap[i] << "\n";
	cout << endl;
}

int main() {
	push(1);
	show();
	push(2);
	show();
	push(3);
	show();
	pop();
	show();
	return 0;
}