二叉堆--C语言实现
来源:互联网 发布:部落冲突地震数据 编辑:程序博客网 时间:2024/05/14 11:12
binheap.h
#ifndef _BINHEAP_H_#define _BINHEAP_H_#define MinPQSize 100struct HeapStruct;typedef int ElementType;typedef struct HeapStruct *PriorityQueue;PriorityQueue Initialize(int MaxElements);void Destroy(PriorityQueue H);void MakeEmpty(PriorityQueue H);void Insert(ElementType X, PriorityQueue H);ElementType DeleteMin(PriorityQueue H);ElementType FindMin(PriorityQueue H);int IsEmpty(PriorityQueue H);int IsFull(PriorityQueue H);#endif
binheap.c
#include<stdio.h>#include<stdlib.h>#include"binheap.h"struct HeapStruct{ int Capacity; int Size; ElementType *Elements;};PriorityQueue Initialize(int MaxElements){ PriorityQueue H; if (MaxElements < MinPQSize) { printf("ERROR!\n"); return NULL; } H = (PriorityQueue)malloc(sizeof(struct HeapStruct)); if (H == NULL) { printf("FAILURE!\n"); return NULL; } H->Elements = (ElementType*)malloc(sizeof(ElementType)* (MaxElements + 1)); if (H->Elements == NULL) { printf("FAILURE!\n"); return NULL; } H->Capacity = MaxElements; H->Size = 0; H->Elements[0] = INT_MIN; return H;}void Destroy(PriorityQueue H){ if (!H) free(H->Elements);}void MakeEmpty(PriorityQueue H){ H->Size = 0;}void Insert(ElementType X, PriorityQueue H){ int i; if (IsFull(H)) { printf("ERROR!\n"); return; } 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)) { printf("ERROR!\n"); return 0; } MinElement = H->Elements[1]; LastElement = H->Elements[H->Size--]; for (i = 1; i * 2 < H->Size; i = Child) { Child = i * 2; if (Child != H->Size && H->Elements[Child + 1] < H->Elements[Child]) Child++; if (LastElement > H->Elements[Child]) H->Elements[i] = H->Elements[Child]; else break; } H->Elements[i] = LastElement; return MinElement;}ElementType FindMin(PriorityQueue H){ if(!H) return H->Elements[1]; return NULL;}int IsEmpty(PriorityQueue H){ return H->Size == 0;}int IsFull(PriorityQueue H){ return H->Size == H->Capacity;}
0 0
- 二叉堆--C语言实现
- 二叉堆C语言实现
- 二叉堆--C语言实现
- 二叉堆--二叉堆实现文件C语言
- 二叉堆的C语言实现
- 优先队列二叉堆 C语言实现
- 二叉堆的实现C语言
- 配合Dijkstra算法的二叉堆实现文件C语言
- 配合Kruskal算法的二叉堆实现文件C语言
- 堆 C语言实现
- 二叉堆--二叉堆头文件C语言
- c语言二叉堆《学习记录》poj3253
- 堆排序C语言实现
- 堆排序C语言实现
- 堆排序C语言实现
- 堆排序C语言实现
- 堆排序--C语言实现
- C语言实现堆排序
- java final关键字
- angular页面间传递数据详解
- 时间日期工具类
- 46个IOS免豆资料下载
- MySQL配置简述
- 二叉堆--C语言实现
- Hdu 2104
- Struts2 include(包含)多个配置文件
- TeleService相关杂记
- GitHub上一些有用的Repositories
- centos配置静态ip
- List控件使用---SimpleAdapter使用详解(二)
- maven-约定优于配置
- Camera知识相关