0
点赞
收藏
分享

微信扫一扫

堆实现,堆排序和TopK问题

阎小妍 2022-04-08 阅读 50

在这里插入图片描述

文章目录

一.二叉树的存储结构

二叉树一般可以使用两种结构存储,一种顺序结构,一种是链式结构

1.顺序存储

顺序结构适用于完全二叉树

image-20220405172614663

2.链式存储

image-20220405162719421

typedef int BTDataType;
//二叉链
struct BinaryTreeNode
{
    struct BinaryTreeNode* left;
    struct BinaryTreeNode* right;
    BTDataType data;
}
//三叉链
struct BinaryTreeNode
{
    struct BinaryTreeNode*  parent;
    struct BinaryTreeNode* left;
    struct BinaryTreeNode*  right;
    BTDataType data;
}
//多叉链,使用指针数组的方式存储
//如果知道树的度
#define N 5
struct TreeNode
{
    int data;
    //利用指针数组存储子节点的指针
    struct TreeNode*subs[N];
};
//不知道树的度
struct TreeNode
{
    int data;
    //顺序表存孩子的指针
    SeqList _s1;
    vector<struct TreeNode *> _subs;
}
//孩子兄弟表示法
typedef int DataType;
struct TreeNode
{
    struct TreeNode*firstchild;
    struct TreeNode* pnextbrother;
    DataType data;
}

在这里插入图片描述

二、堆的概念和结构

image-20220405164306320
在这里插入图片描述

三、堆的实现

堆的头文件

//防止头文件被重复包含
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int HPDataType;
//顺序结构实现堆
typedef struct Heap
{
    HPDataType* a;
    //当前存储数
    size_t size;
    //最大容量
    size_t capacity;
}HP;
void swap(HPDataType*a,HPDataType*b);
//初始化堆
void HeapInit(HP*php);
//摧毁堆
void HeapDestroy(HP*php);
//打印堆元素
void HeapPrint(HP*php);
//插入数据
void HeapPush(HP*php, HPDataType x;
//删除栈顶数据
void HeapPop(HP*php);
//判断为空
bool HeapEmpty(HP*php);
size_t HeapSize(HP*php);
//返回堆顶数据
HPDateType HeapTop(HP*php);
void AdjustUp(HPDataType* a, size_t child);
void AdjustDown(HPDataType* a, size_t size, size_t root);

接口实现

void HeapInit(HP* php)
{
	assert(php);
	php->a = NULL;
	php->size = php->capacity = 0;
}

void HeapDestroy(HP* php)
{
	assert(php);
	free(php->a);
	php->a = NULL;
	php->size = php->capacity = 0;
}

void Swap(HPDataType* pa, HPDataType* pb)
{
	HPDataType tmp = *pa;
	*pa = *pb;
	*pb = tmp;
}

void HeapPrint(HP* php)
{
	assert(php);
	for (size_t i = 0; i < (php->size); ++i)
	{
		printf("%d ", php->a[i]);
	}
	printf("\n");
}

void AdjustUp(HPDataType* a, size_t child)
{
	size_t parent = (child - 1) / 2;
	while (child > 0)
	{
		if (a[child] < a[parent])
             //如果要建立大堆
			//if (a[child] > a[parent])
		{
			Swap(&a[child], &a[parent]);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
		{
			break;
		}
	}
}

void HeapPush(HP* php, HPDataType x)
{
	assert(php);

	if (php->size == php->capacity)
	{
		size_t newCapacity = php->capacity == 0 ? 4 : php->capacity * 2;
		HPDataType* tmp = realloc(php->a, sizeof(HPDataType) * newCapacity);
		if (tmp == NULL)
		{
			printf("realloc failed\n");
			exit(-1);
		}

		php->a = tmp;
		php->capacity = newCapacity;
	}

	php->a[php->size] = x;
	++php->size;

	// 向上调整,控制保持是一个小堆
	AdjustUp(php->a, php->size - 1);
}

void AdjustDown(HPDataType* a, size_t size, size_t root)
{
	size_t parent = root;
	size_t child = parent * 2 + 1;
	while (child < size)
	{
		// 1、选出左右孩子中小的那个
		if (child + 1 < size && a[child + 1] < a[child])
            //建立大堆
            //if(child+1<size&&a[child+1]>a[child])
		{
			++child;
		}

		// 2、如果孩子小于父亲,则交换,并继续往下调整
		if (a[child] < a[parent])
         //建立大堆
         //if(a[child]>a[parent])
		{
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

// 删除堆顶的数据。(最小/最大)
void HeapPop(HP* php)
{
	assert(php);
	assert(php->size > 0);

	Swap(&php->a[0], &php->a[php->size - 1]);
	--php->size;

	AdjustDown(php->a, php->size, 0);
}

bool HeapEmpty(HP* php)
{
	assert(php);

	return php->size == 0;
}

int HeapSize(HP* php)
{
	assert(php);

	return php->size;
}

HPDataType HeapTop(HP* php)
{
	assert(php);
	assert(php->size > 0);

	return php->a[0];
}

四、测试堆和二叉树

#include"Heap.h"
void TestHeap()
{
	HP hp;
	HeapInit(&hp);
	HeapPush(&hp, 1);
	HeapPush(&hp, 5);
	HeapPush(&hp, 0);
	HeapPush(&hp, 8);
	HeapPush(&hp, 3);
	HeapPush(&hp, 9);
	HeapPrint(&hp);

	HeapPop(&hp);
	HeapPrint(&hp);

	HeapDestroy(&hp);
}
//堆排序
void HeapSort(int*a,int size)
{
    //创建一个堆
    HP hp;
    HeapInit(&hp);
    //吧=把数据存放在堆中
    for(int i=0;i<size;i++)
    {
        HeapPush(&hp,a[i]);
    }
    size_t j=0;
    while(!HeapEmpty(&hp))
    {
        //返回堆顶
        a[j]=HeapTop(&hp);
        j++;
        HeapPop(&hp);
    }
    HeapStory(&hp);
    
}
int main()
{
    int a[]={4,2,7,8,5,1,0,6};
    //进行堆排序
    HeapSort(a,sizeof(a)/sizeof(int);i++);
    for(int i=0;i<sizeof(a)/sizeof(int);i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");
    return 0;
}

堆的实现

int array[] = {27,15,19,18,28,34,65,49,25,37};

image-20220406150104826

堆的创建

int a[]={1,5,3,8,7,6};

image-20220406152437821

//元素数组建堆
//通过向下调整法建立堆
void HeapSort(int *a,int size)
{
    assert(a);
    assert(size==1);
    for(size_t j=1;j<size;j++)
    {
        AdjustUp(a,j);
    }
}
//使用向下调整建堆
//向下调整的前提是左子树和右子树必须是堆
//从最后一个节点的父亲开始调
void HeapSort(int *a,int size)
{
    for(int i=(n-1-1)/2;i>=0;i--)
    {
        AdjustDown(a,n,i);
    }
}

建堆的时间复杂度

向上调整法的时间复杂度

image-20220407202025331

向下调整法的时间复杂度

image-20220407203545605

作业

image-20220406142747842

image-20220406142806131

堆的应用

堆排序

  • 升序:建立大堆

  • image-20220407205750402

    //原数组升序
    //建大堆
    for(int i=(n-1-1)/2;i>=0;i++)
    {
        AdjustDown(a,n,i);
    }
    int end=n-1;
    while(end>0)
    {
        swap(&a[end],a[0]);
        end--;
        AdjustDown(a,end,0);
    }
    
  • 降序:建立小堆

TopK问题

image-20220407211423436

image-20220407212725432

void PrintTopK(int* a, int n, int k)
{
    // 1. 建堆--用a中前k个元素建堆
    int* kmaxheap = (int*)malloc(sizeof(int) * k);
    for (int i = 0;i < k;i++)
    {
        kmaxheap[i] = a[i];
    }
    //建立堆
    for (int i = (k - 1 - 1) / 2;i >= 0;i--)
    {
        AdjustDown(kmaxheap, k, i);
    }
    // 2. 将剩余n-k个元素依次与堆顶元素交换,不满则则替换
    for (int j = k;j < n;j++)
    {
        if (a[j] > kmaxheap[0])
        {
            kmaxheap[0] = a[j];
            //向下调整
            AdjustDown(kmaxheap, k, 0);
        }
    }

    for (int i = 0;i < k;i++)
    {
        printf("%d ", kmaxheap[i]);
    }
}
void TestTopk()
{
    int n = 10000;
    int* a = (int*)malloc(sizeof(int) * n);
    srand(time(0));
    for (int i = 0; i < n; ++i)
    {
        a[i] = rand() % 1000000;
    }
    a[5] = 1000000 + 1;
    a[1231] = 1000000 + 2;
    a[531] = 1000000 + 3;
    a[5121] = 1000000 + 4;
    a[115] = 1000000 + 5;
    a[2335] = 1000000 + 6;
    a[9999] = 1000000 + 7;
    a[76] = 1000000 + 8;
    a[423] = 1000000 + 9;
    a[3144] = 1000000 + 10;
    PrintTopK(a, n, 10);
}

int main()
{
    TestTopk();
    return 0;
}

image-20220408150923880

在这里插入图片描述

举报

相关推荐

堆和堆排序

0 条评论