堆的基本操作

#include"Heapp.h"
#include<string.h>
int Less(HeapType a, HeapType b){
return  a < b ? 1 : 0;


}
int Greater(HeapType a, HeapType b){
return  a > b ? 1 : 0;
}
void HeapInit(Heap* heap, Compare cmp){
if (heap == NULL)
{
return;
}
heap->size = 0;
heap->cmp = cmp;
return;

}

#pragma once
#include<stdio.h>
#include<stdlib.h>
#define HeapMaxSize 1024
typedef char HeapType;
typedef int(*Compare)(HeapType a, HeapType b);


typedef struct Heap{
HeapType data[HeapMaxSize];
size_t size;
Compare cmp;
}Heap;


int Greater(HeapType a, HeapType b);
void HeapInit(Heap* heap, Compare cmp);


int Heaproot(Heap* heap, HeapType* value);
void HeapRemove(Heap* heap, Compare);


void HeapInsert(Heap* heap, HeapType value);


void HeapPrintf(Heap* heap, const char* msg);
int Heaproot(Heap* heap, HeapType* value);
void HeapErase(Heap* heap);
void HeapCreat(Heap* heap, HeapType array[], size_t size);
void HeapSort(HeapType array[], size_t size);



void HeapRemove(Heap* heap, Compare cmp)
{
if (heap == NULL)
{
return;
}
heap->size = 0;
heap->cmp = NULL;


return;
}
void Swap(HeapType* a, HeapType* b)
{
HeapType tmp = *a;
*a = *b;
*b = tmp;
return;
}
void AdjustUp(Heap*heap, size_t index){
size_t child = index;
size_t parent = (child - 1) / 2;


while (child>0)


{
if (!heap->cmp(heap->data[parent], heap->data[child])){
Swap(&heap->data[parent], &heap->data[child]);
}
child = parent;
parent = (child - 1) / 2;
}
}
void AdjustDown(Heap*heap, size_t index){
size_t parent = index;
size_t child = parent * 2 + 1;
while (parent>0){
if (child + 1<heap->size&&heap->cmp(heap->data[child + 1], heap->data[child])){
child = child + 1;
}


if (heap->cmp(heap->data[child], heap->data[parent])){
Swap(&heap->data[child], &heap->data[parent]);


}
else {
break;
}
parent = child;
child = parent * 2 + 1;
}
}
void HeapInsert(Heap* heap, HeapType value)
{
if (heap == NULL)
{
return;
}
if (heap->size >= HeapMaxSize)
{
return;
}
heap->data[heap->size++] = value;
AdjustUp(heap, heap->size - 1);
}
void HeapPrintf(Heap* heap, const char* msg)
{
printf("\n[%s]\n", msg);
size_t i = 0;
for (i = 0; i<heap->size; i++)
{
printf("[%c|%lu]", heap->data[i], i);
}
}
int Heaproot(Heap* heap, HeapType* value)
{
if (heap == NULL)
{
return  0;
}
if (heap->size == 0)
{
return 0;
}
*value = heap->data[0];
return 1;
}
void HeapErase(Heap* heap){
if (heap == NULL)
{
return;
}
if (heap->size == 0)
{
return;
}
Swap(&heap->data[0], &heap->data[heap->size - 1]);
--heap->size;
AdjustDown(heap, 0);
return;
}
void HeapCreat(Heap* heap, HeapType array[], size_t size)
{
if (heap == NULL)
{
return;
}
size_t i = 0;
for (; i<size; ++i)
HeapInsert(heap, array[i]);
return;
}


void HeapSort(HeapType array[], size_t size)
{
Heap heap;
HeapInit(&heap, Greater);
HeapCreat(&heap, array, size);
while (heap.size>0)
{
HeapErase(&heap);
}
memcpy(array, heap.data, size*sizeof(HeapType));
return;

}

#include"Heapp.h"
#define TEST_HEADER printf("\n=============%s==============\n",__FUNCTION__)
void TestInit(){
TEST_HEADER;
Heap heap;
HeapInit(&heap, Greater);
printf("size expected 0, actual %lu\n", heap.size);
}
void TestInsert(){
TEST_HEADER;
Heap heap;
HeapInit(&heap, Greater);
HeapInsert(&heap, 'c');
HeapInsert(&heap, 'b');
HeapInsert(&heap, 'a');
HeapInsert(&heap, 'e');
HeapInsert(&heap, 'f');
HeapInsert(&heap, 'd');
HeapPrintf(&heap, "给堆插如6个元素\n");
HeapType value;
int ret = Heaproot(&heap, &value);
printf("ret expected 1,actual %d\n ", ret);
printf("value expected f,actual %c\n ", value);
HeapPrintf(&heap, "root\n");
HeapErase(&heap);
HeapPrintf(&heap, "删除堆中一个元素\n");
}
void TestSort(){
TEST_HEADER;
HeapType array[] = { 'a', 'b', 'c', 'd' };
HeapSort(array, sizeof(array) / sizeof(array[0]));
size_t i = 0;
for (; i<sizeof(array) / sizeof(array[0]); ++i)
{
printf("[%c|%u]\n", array[i]);


}
printf("\n");
}
int  main()
{
TestInit();
TestInsert();
    TestSort();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值