堆是一种特殊的数据结构,它有两个特性:
- 底层实现结构是完全二叉树(定义:当且仅当一棵树每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。)
- 如果是大根堆,那么保证每个节点的值都比父节点小,如果是小根堆,那么保证每个节点的值都比父节点大
建堆就是把一堆数据塞进堆里,因为数据很可能是无序的,所以说为了把一堆杂乱无章的数据整合成上面的样子, 就需要一些特别的操作,有两种调整模式:
- 向上调整。也就是对于一个节点要和它的父节点比较大小,若需要调整则和父节点交换,然后把查询位置更新到父节点的位置,继续迭代查询,终止条件为当前位置是根节点的位置。
- 向下调整。也就是对于每层的有子结点的父节点来说,向下查询那个较大的子结点的值是不是大于本父节点(因为有两个子节点),如果是,就交换,然后迭代向下查询,终止条件是当前节点不存在子节点。
添加数据:就是在根末(数组尾部)添加一个元素,然后从此元素的位置进行向上调整即可
删除最大值/最小值 (分别对应大根堆和小根堆):只需将根节点和根末节点(数组尾部元素)进行交换,然后将容量减一抛出根节点,再从根节点位置进行向下调整即可。
#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
#define debug cout<<"procedures above are available"<<endl;
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 33;
inline ll lldcin()
{
ll tmp = 0, si = 1;
char c;
c = getchar();
while (c > '9' || c < '0')
{
if (c == '-')
si = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
tmp = tmp * 10 + c - '0';
c = getchar();
}
return si * tmp;
}
/*Maintain your determination.Nobody knows the magnificent landscape
at his destination before the arrival with stumble.*/
/**Last Remote**/
ll Heap[12];
ll Volume = 10;
void DownwardAdjustment(ll CurrentPosition,ll Limit)
{
while (CurrentPosition * 2 <= Limit)
{
ll downstair = CurrentPosition * 2;
if (downstair + 1 <= Limit && Heap[downstair + 1] > Heap[downstair])
downstair++;
if (Heap[CurrentPosition] < Heap[downstair])
{
swap(Heap[CurrentPosition], Heap[downstair]);
CurrentPosition = downstair;
}
else
break;
}
}
void construction(ll Limit)
{
for (int i = Limit / 2; i >= 1; i--)
DownwardAdjustment(i, Limit);
}
void UpwardAdjustment(ll CurrentPosition)
{
while (CurrentPosition > 1 && Heap[CurrentPosition] > Heap[CurrentPosition / 2])
{
swap(Heap[CurrentPosition], Heap[CurrentPosition / 2]);
CurrentPosition /= 2;
}
}
void Addition(ll Element)
{
Heap[++Volume] = Element;
UpwardAdjustment(Volume);
}
void EraseRoot(ll Root,ll &Limit)
{
swap(Heap[Root], Heap[Limit]);
Limit--;
DownwardAdjustment(Root, Limit);
}
int DETERMINATION()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
for (int i = 1; i <= Volume; i++)
Heap[i] = rand() % 100+1;
for (int i : Heap)
if(i!=0)
cout << i << " ";
cout << endl;
debug;
construction(Volume);
ll n;
cin >> n;
Addition(n);
while (Volume >= 1)
{
cout << Heap[1] << endl;
EraseRoot(1, Volume);
}
return 0;
}