C源码@数据结构与算法->PriorityQueues

本文介绍了一种基于二叉堆的数据结构实现及其关键操作,包括初始化、插入、删除最小元素等,并提供了详细的C语言代码示例。通过本文,读者可以了解到如何利用二叉堆解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * testheap.cpp
 */
#include 
#include "binheap.h"

#define MaxSize (1000)

int main()
{
	PriorityQueue H;
	int i, j;

	H = Initialize(MaxSize);
	for (i = 0, j = MaxSize / 2; i < MaxSize; ++i, j = (j + 71) % MaxSize)
	{
		Insert(j, H);
	}

	j = 0; 
	while (!IsEmpty(H))
	{
		if (DeleteMin(H) != j++)
		{
			printf("Error in DeleteMin, %d\n", j);
		}
	}

	printf("Done...\n");

	return 0;
}

/*
 * fatal.h
 */
#include 
#include 

#define Error(Str)		FatalError(Str)
#define FatalError(Str)	fprintf(stderr, "%s\n", Str), exit(-1)	
/*
 * binheap.h
 */
#ifndef _BIN_HEAP_H
#define _BIN_HEAP_H

#ifndef NULL
#define NULL (0)
#endif

typedef int ElementType;

struct HeapStruct;
typedef struct HeapStruct *PriorityQueue;

#ifdef __cplusplus
extern "C" {
#endif

PriorityQueue Initialize(int MaxElements);
void MakeEmpty(PriorityQueue H);
int IsEmpty(PriorityQueue H);
int IsFull(PriorityQueue H);
void Insert(ElementType X, PriorityQueue H);
ElementType DeleteMin(PriorityQueue H);
ElementType FindMin(PriorityQueue H);
void Destroy(PriorityQueue H);

#ifdef __cplusplus
}
#endif

#endif /* _BIN_HEAP_H */
/*
 * binheap.cpp
 */
#include <stdlib.h>
#include "fatal.h"
#include "binheap.h"

#define MinPQSize (10)
#define MinData (-32767)

struct HeapStruct
{
	int Capacity;
	int Size;
	ElementType *Elements;
};

PriorityQueue Initialize(int MaxElements)
{
	PriorityQueue H;

	if (MaxElements < MinPQSize)
	{
		FatalError("Priority queue size is too small!");
	}

	H = (PriorityQueue)malloc(sizeof(struct HeapStruct));
	if (H == NULL)
	{
		FatalError("Out of space!");
	}

	/* Allocate the array plus one extra for sentinel */
	H->Elements = (ElementType *)malloc(sizeof(ElementType) 
		* (MaxElements + 1));
	if (H->Elements == NULL)
	{
		FatalError("Out of space!");
	}

	H->Capacity = MaxElements;
	H->Size = 0;
	H->Elements[0] = MinData;

	return H;
}

void MakeEmpty(PriorityQueue H)
{
	H->Size = 0;
}

int IsEmpty(PriorityQueue H)
{
	return H->Size == 0;
}

int IsFull(PriorityQueue H)
{
	return H->Size == H->Capacity;
}

/*
 * H->Elements[0] is a sentinel.
 *
 * Heap Order Proprety: 
 * In a heap, for every node x, the key in the parent of X is smaller than
 * (or equal to) the key in X, with the exception of the root(which has no parent).
 */
void Insert(ElementType X, PriorityQueue H)
{
	int i;

	if (IsFull(H))
	{
		FatalError("Priority queue is full!");
	}

	for (i = ++H->Size; H->Elements[i / 2] > X; i /= 2)
	{
		H->Elements[i] = H->Elements[i / 2];
	}
	H->Elements[i] = X;
}

ElementType DeleteMin(PriorityQueue H)
{
	int i, Child;
	ElementType MinElement, LastElement;

	if (IsEmpty(H))
	{
		FatalError("Priority queue is empty!");
	}
	MinElement = H->Elements[1];
	LastElement = H->Elements[H->Size--];

	for (i = 1; i * 2 <= H->Size; i = Child)
	{
		/* Find smaller child */
		Child = i * 2;
		if (Child != H->Size
			&& H->Elements[Child + 1] < H->Elements[Child])
		{
			Child++;
		}

		/* Percolate one level */
		if (LastElement > H->Elements[Child])
		{
			H->Elements[i] = H->Elements[Child];
		}
		else
		{
			break;
		}
	}
	H->Elements[i] = LastElement;

	return MinElement;
}

ElementType FindMin(PriorityQueue H)
{
	if (!IsEmpty(H))
	{
		return H->Elements[1];
	}
	else
	{
		FatalError("Priority queue is empty!");
		return -1;		/* Just for avoid warning */
	}
}

void Destroy(PriorityQueue H)
{
	free(H->Elements);
	free(H);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值