大家好,这里是小编的博客频道
小编的博客:就爱学编程
很高兴在
CSDN
这个大家庭与大家相识,希望能在这里与大家共同进步,共同收获更好的自己!!!
那接下来就让我们开始遨游在知识的海洋!
堆是一种特殊的树形数据结构,它完全是一棵二叉树。堆分为最大堆和最小堆两种类型:
最大堆
:在最大堆中,父节点的键值总是大于或等于任何一个子节点的键值。换句话说,根节点是整个堆中键值最大的节点。最小堆
:在最小堆中,父节点的键值总是小于或等于任何一个子节点的键值。也就是说,根节点是整个堆中键值最小的节点。
堆通常用于实现优先队列,并且能在对数时间内完成插入、删除以及查找最值等操作。
- 堆一般使用数组来实现,这是因为堆是完全二叉树的一种特殊形式,其性质使得我们可以利用数组的索引关系来方便地表示堆的结构。对于一个包含n个元素的零基数组来说:
对于任意给定的索引i:
左子节点的索引为(2*i + 1)
右子节点的索引为(2*i + 2)
父节点的索引为((i - 1) / 2)
(注意这是整数除法)
这种表示方法使得堆的操作变得非常简单高效。
向堆中插入一个新元素的过程如下:
将新元素添加到堆的末尾(即数组的最后一个位置)。
然后,从该位置开始向上调整堆,以维护堆的性质。这通常是通过“上浮”(bubble up)或“筛选”(sift up)操作来实现的。
具体步骤如下(以最大堆为例):
void heapInsert(int arr[], int &heapSize, int element) {
// Step 1: Add the new element at end
arr[heapSize] = element;
heapSize++;
// Step 2: Fix the max heap property if it is violated
int i = heapSize - 1;
while (i != 0 && arr[(i - 1) / 2] < arr[i]) {
// Swap current node with its parent
swap(&arr[i], &arr[(i - 1) / 2]);
i = (i - 1) / 2; // Move to parent index
}
}
从堆中删除最大或最小值的过程如下(以最大堆为例):
将根节点(即最大值)与堆的最后一个元素交换。
减少堆的大小(即将堆的最后一个元素移除)。
从新的根节点开始向下调整堆,以维护堆的性质。这通常是通过“下沉”(bubble down)或“筛选”(sift down)操作来实现的。
具体步骤如下:
int heapExtractMax(int arr[], int &heapSize) {
if (heapSize <= 0) return INT_MIN; // Return an invalid value if heap is empty
// Store the maximum value, and remove it from heap
int root = arr[0];
arr[0] = arr[heapSize - 1];
heapSize--;
// Fix the max heap property if it is violated
maxHeapify(arr, 0, heapSize);
return root;
}
void maxHeapify(int arr[], int i, int heapSize) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left child index
int right = 2 * i + 2; // right child index
// If left child is larger than root
if (left < heapSize && arr[left] > arr[largest])
largest = left;
// If right child is larger than largest so far
if (right < heapSize && arr[right] > arr[largest])
largest = right;
// If largest is not root
if (largest != i) {
swap(&arr[i], &arr[largest]);
// Recursively heapify the affected sub-tree
maxHeapify(arr, largest, heapSize);
}
}
将一个无序数组构建成一个堆的过程可以通过以下步骤实现:
从最后一个非叶子节点开始,依次对每个节点执行“下沉”操作,直到根节点为止。
具体步骤如下(以最大堆为例):
void buildMaxHeap(int arr[], int n) {
int startIdx = (n / 2) - 1; // Last non-leaf node
for (int i = startIdx; i >= 0; i--) {
maxHeapify(arr, i, n);
}
}
注意:这是小编自己写的,可能有不足之处,仅供参考!!!
#pragma once
#include
#include
#include
typedef int HPDataType;
typedef struct Heap
{
HPDataType* _a;
int _size;
int _capacity;
}Heap;
//堆的初始化
void HeapInit(Heap* php);
// 堆的销毁
void HeapDestory(Heap* hp);
// 堆的插入
void HeapPush(Heap* hp, HPDataType x);
// 堆的删除
void HeapPop(Heap* hp);
// 取堆顶的数据
HPDataType HeapTop(Heap* hp);
// 堆的数据个数
int HeapSize(Heap* hp);
// 堆的判空
int HeapEmpty(Heap* hp);
//对调
void Swap(HPDataType* ptr1, HPDataType* ptr2);
//向上调整(使用条件:除了child对应的数据,其他的构成堆)
void AujustUp(HPDataType* a, int child);
//向下调整(使用条件:左右子树都是大堆或小堆)
void AujustDown(HPDataType* a, int size, int parent);
#include"heap.h"
//堆的初始化
void HeapInit(Heap* php) {
assert(php);
HPDataType* temp = (HPDataType*)malloc(sizeof(HPDataType) * 4);
if (temp ==NULL) {
perror("malloc fail");
return;
}
php->_a = temp;
php->_capacity = 4;
php->_size = 0;
}
// 堆的销毁
void HeapDestory(Heap* php) {
assert(php);
free(php->_a);
php->_a = NULL;
php->_capacity = 0;
php->_size = 0;
}
//对调
void Swap(HPDataType* ptr1, HPDataType* ptr2) {
assert(ptr1 && ptr2);
HPDataType temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
//向上调整(使用条件:除了child对应的数据,其他的构成堆)
void AujustUp(HPDataType* a, int child) {
assert(a);
int parent = (child - 1) / 2;
while (child > 0) {
if (a[child] > a[parent]) {
Swap(&a[child], &a[parent]);
}
else {
break;
}
child = parent;
parent = (child - 1) / 2;
}
}
// 堆的插入(从尾插入,向上调整)
void HeapPush(Heap* php, HPDataType x) {
assert(php);
if (php->_capacity == php->_size) {
HPDataType* temp = (HPDataType*)realloc(php->_a, sizeof(HPDataType) * 2 * php->_capacity);
if (temp == NULL) {
perror("realloc fail");
return;
}
php->_a = temp;
php->_capacity *= 2;
}
php->_a[php->_size] = x;
++php->_size;
AujustUp(php->_a, php->_size - 1);
}
//向下调整(使用条件:左右子树都是大堆或小堆)
void AujustDown(HPDataType* a, int size, int parent) {
assert(a);
int bigchild = parent * 2 + 1; //先假设大孩子是左孩子
while (bigchild < size) {
//先找到真正数值大的孩子的下标
if (bigchild + 1 < size && a[bigchild] < a[bigchild + 1]) //小堆'<'改成'>'
{
++bigchild;
}
if (a[bigchild] > a[parent]) { //小堆'>'改成'<'
Swap(&a[bigchild], &a[parent]);
parent = bigchild;
bigchild = parent * 2 + 1; //还是先假设大孩子是左孩子
}
else {
break;
}
}
}
// 堆的删除(删掉堆顶,向下调整)
void HeapPop(Heap* php) {
assert(php && php->_size);
//采用堆顶数据和堆尾数据对调,再向下调整的优势在于效率高,且不会改变大部分的父子关系
Swap(&php->_a[0], &php->_a[php->_size - 1]);
--php->_size;
AujustDown(php->_a, php->_size, 0);
}
//
void HeapInitArray(Heap* php, int* a, int n)
{
assert(php);
php->_a = (HPDataType*)malloc(sizeof(HPDataType) * n);
if (php->_a == NULL)
{
perror("malloc fail");
return;
}
php->_size = n;
php->_capacity = n;
// 建堆
for (int i = (n - 2) / 2; i >= 0; --i)
{
AdjustDown(php->_a, php->_size, i);
}
}
// 取堆顶的数据
HPDataType HeapTop(Heap* php) {
assert(php && php->_size);
return php->_a[0];
}
// 堆的数据个数
int HeapSize(Heap* php) {
assert(php && php->_size);
return php->_size;
}
// 堆的判空
int HeapEmpty(Heap* php) {
assert(php);
return php->_size == 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include"heap.h"
int main() {
Heap heap;
//堆的初始化
HeapInit(&heap);
//堆插入
HeapPush(&heap, 1);
HeapPush(&heap, 2);
HeapPush(&heap, 17);
HeapPush(&heap, 16);
HeapPush(&heap, 18);
HeapPush(&heap, 111);
HeapPush(&heap, 21);
HeapPush(&heap, 14);
HeapPush(&heap, 61);
//堆删除
HeapPop(&heap);
HeapPop(&heap);
HeapPop(&heap);
HeapPop(&heap);
//根据自己需求观察堆内数据(大堆)
int k = 0;
scanf("%d", &k);
while (!HeapEmpty(&heap) && k--)
{
printf("%d ", HeapTop(&heap));
HeapPop(&heap);
}
printf("\n");
return 0;
}
堆是实现优先队列的理想数据结构。在优先队列中,每个元素都有一个优先级,出队顺序是按照优先级的高低来进行的。最大堆可以用于实现一个最大优先队列,而最小堆则可以用于实现一个最小优先队列。
堆排序的具体步骤如下:
- 构建最大堆(
Build Max Heap
):将待排序序列构造成一个大顶堆。
交换堆顶元素与末尾元素(Swap and Reduce Heap Size
):将当前堆顶元素(最大值)与末尾元素交换,并将堆的大小减1。
重新调整堆(Heapify
):对新的堆顶元素进行调整,使其满足堆的性质。
重复步骤2和3,直到堆的大小为1。
具体代码实现如下:
#include
#include
// Function to swap two elements
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
// Function to heapify a subtree rooted with node i which is an index in arr[]
void heapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int left = 2 * i + 1; // left = 2*i + 1
int right = 2 * i + 2; // right = 2*i + 2
// See if left child of root exists and is greater than root
if (left < n && arr[left] > arr[largest])
largest = left;
// See if right child of root exists and is greater than root
if (right < n && arr[right] > arr[largest])
largest = right;
// Change root, if needed
if (largest != i) {
swap(&arr[i], &arr[largest]);
// Heapify the root.
heapify(arr, n, largest);
}
}
// Main function to do heap sort
void heapSort(int arr[], int n) {
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// One by one extract an element from heap
for (int i = n - 1; i >= 0; i--) {
// Move current root to end
swap(&arr[0], &arr[i]);
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
// A utility function to print array of size n
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("
");
}
// Driver program to test above functions
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array:
");
printArray(arr, n);
heapSort(arr, n);
printf("Sorted array:
");
printArray(arr, n);
return 0;
}
nlogn
)。Min-Heap
)或最大堆(Max-Heap
)来维护当前的前K大(或小)元素。当遍历到新的元素时,如果它大于(或小于)堆顶元素,则替换堆顶并调整堆。最终,堆中的元素即为所求的前K大(或小)元素。这种方法的时间复杂度为O(nlogk
),适合处理大规模数据。
综上所述,Top K问题是一个具有广泛应用背景和重要价值的问题。通过理解和掌握其相关知识,可以更好地分析和理解数据,提高决策效率和质量。
代码实现:
#define _CRT_SECURE_NO_WARNINGS
//TOP-k问题
//想要找出N个数据中最大(小)的K个:最简单的思路就是排序,但排序在数据量很大的时候效率低下且有可能无法全部加载进内存。最好的办法就是用堆的办法。
//用堆也有两种思路:(1)把N个数据全部建大(小)堆,然后再top后popK次-----不足:数据只能全部在内存上,在数据量很大时数据可能无法全部加载进内存;
//(2)把前K个数据建小(大)堆,然后再使剩余的N-K个数据依次与堆顶进行比较,比它大就取代,再向下调整。到最后剩下的数据就是最大(小)的K个。
#include
#include
#include
#include
//对调数据
void Swap(int* ptr1, int* ptr2) {
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
//向上调整(使用条件:除了child对应的数据,其他的构成堆)
void AdjustUp(int* a, int child) {
assert(a);
int parent = (child - 1) / 2;
while (child > 0) {
if (a[child] > a[parent]) {
Swap(&a[child], &a[parent]);
}
else {
break;
}
child = parent;
parent = (child - 1) / 2;
}
}
//向下调整(使用条件:左右子树都是大堆或小堆)
void AdjustDown(int* a, int size, int parent) {
assert(a);
int bigchild = parent * 2 + 1; //先假设大孩子是左孩子
while (bigchild < size) {
//先找到真正数值大(小)的孩子的下标
if (bigchild + 1 < size && a[bigchild] > a[bigchild + 1]) //小堆后面的'<'改成'>'
{
++bigchild;
}
if (a[bigchild] < a[parent]) { //小堆'>'改成'<'
Swap(&a[bigchild], &a[parent]);
parent = bigchild;
bigchild = parent * 2 + 1; //还是先假设大孩子是左孩子
}
else {
break;
}
}
}
//造数据
void Cratedata(int n) {
srand(time(0)); //生成随机数
FILE* fin = fopen("data.txt", "w");
if (NULL == fin) {
perror("fopen error");
return;
}
while (n--) {
int data = rand() % 10000;
fprintf(fin,"%d\n", data);
}
fclose(fin);
}
//Top-K(第二种解决办法)
void Top_k(int n, int k) {
//1.读取前K个数据并建堆
//打开文件
FILE* fout = fopen("data.txt", "r");
if (NULL == fout){
perror("fopen error");
return;
}
//申请空间存储读取的K个数据
int* a = (int*)malloc(sizeof(int) * k);
assert(a);
for (int i = 0; i < k; i++) {
fscanf(fout, "%d", &a[i]);
}
//把这K个数据进行建堆----找最大,建小堆(向下调整)----可以防止数据的进入堆不受阻
for (int i = (k - 1 - 1) / 2; i > 0; --i) {
AdjustDown(a, k, i);
}
//2.然后再使剩余的N-K个数据依次与堆顶进行比较,比它大就取代,再向下调整。到最后剩下的数据就是最大(小)的K个。
int data = 0;
while (fscanf(fout, "%d", &data) != EOF) {
if (data > a[0]) {
a[0] = data;
AdjustDown(a, k, 0);
}
}
fclose(fout);
for (int i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
}
void Test() {
//Cratedata(100);
Top_k(100, 10);
}
int main() {
Test();
return 0;
}