d-堆实现文件C语言
2010-11-07 18:58
302 查看
/* d-heap.c -- d-堆实现文件 */ #include <stdio.h> #include <stdlib.h> #include "d-heap.h" /* 局部函数声明 */ static int Percolate_Up (const Heap * const pheap, const Item item, const int position) ; static int Percolate_Down (const Heap * const pheap, const Item item, const int position) ; /* 接口函数定义 */ int InitializeHeap (Heap * const pheap, const int capacity, const int d) { if (capacity < 0) return 0 ; *pheap = (struct heap *) malloc (sizeof (struct heap) * (capacity + 1)) ; if (NULL == *pheap) { puts ("Out of space.[1]") ; return 0 ; } (*pheap) -> array = (Item *) malloc (sizeof (Item) * capacity) ; if (NULL == (*pheap) -> array) { puts ("Out of space.[2]") ; free (*pheap) ; return 0 ; } (*pheap) -> capacity = capacity ; (*pheap) -> size = 0 ; (*pheap) -> d = d ; (*pheap) -> array[0] = MINDATA ; return 1 ; } int HeapIsEmpty (const Heap * const pheap) { return 0 == (*pheap) -> size ; } int HeapIsFull (const Heap * const pheap) { return (*pheap) -> capacity == (*pheap) -> size ; } int Insert (const Heap * const pheap, const Item item) { int i ; if (HeapIsFull (pheap)) return 0 ; // 变量作为参数在函数间传递时后缀自增、自减不改变变量的值 i = Percolate_Up (pheap, item, ++(*pheap) -> size) ; (*pheap) -> array[i] = item ; return 1 ; } Item DeleteMin (const Heap * const pheap) { Item min, item ; int i, position; if (HeapIsEmpty (pheap)) return (*pheap) -> array[0] ; min = (*pheap) -> array[1] ; item = (*pheap) -> array[(*pheap) -> size] ; (*pheap) -> size-- ; position = 1 ; i = Percolate_Down (pheap, item, position) ; (*pheap) -> array[i] = item ; return min ; } int DecreaseKey (const Heap * const pheap, const int position, const int triangle) { Item item ; int i ; if (position > 0 && position <= (*pheap) -> size && triangle > 0) { item = (*pheap) -> array[position] -= triangle ; i = Percolate_Up (pheap, item, position) ; (*pheap) -> array[i] = item ; return 1 ; } else return 0 ; } int IncreaseKey (const Heap * const pheap, const int position, const int triangle) { Item item ; int i ; if (position > 0 && position <= (*pheap) -> size && triangle > 0) { item = (*pheap) -> array[position] += triangle ; i = Percolate_Down (pheap, item, position) ; (*pheap) -> array[i] = item ; return 1 ; } else return 0 ; } int BuildHeap (const Heap * const pheap, const Item * parray, const int size) { Item item ; int i, d, ct ; if (!HeapIsEmpty (pheap) || size <= 0) return 0 ; for (i = 0; i < size; i++) (*pheap) -> array[i + 1] = parray[i] ; (*pheap) -> size = size ; for (d = (*pheap) -> d, ct = (size + d - 2) / d; ct > 0; ct--) { item = (*pheap) -> array[ct] ; i = Percolate_Down (pheap, item, ct) ; (*pheap) -> array[i] = item ; } return 1 ; } int Delete (const Heap * const pheap, const int position) { int triangle ; if (position < 0 || position > (*pheap) -> size) return 0 ; triangle = (*pheap) -> array[position] - (*pheap) -> array[1] + 1 ; DecreaseKey (pheap, position, triangle) ; DeleteMin (pheap) ; return 1 ; } void Traversal (const Heap * const pheap, void (* pfun) (const Item item)) { int i, size ; for (i = 1, size = (*pheap) -> size; i <= size; i++) (* pfun) ((*pheap) -> array[i]) ; } void Release (const Heap * const pheap) { free ((*pheap) -> array) ; free (*pheap) ; } /* 局部函数定义 */ static int Percolate_Up (const Heap * const pheap, const Item item, const int position) { int i, d ; for (i = position, d = (*pheap) -> d; (*pheap) -> array[(i + d - 2) / d] > item; i = (i + d - 2) / d) (*pheap) -> array[i] = (*pheap) -> array [(i + d - 2) / d] ; return i ; } static int Percolate_Down (const Heap * const pheap, const Item item, const int position) { Item item_record ; int i, j, d, child, int_record; for (i = position, d = (*pheap) -> d; (i - 1) * d + 2 <= (*pheap) -> size; i = child) { child = (i - 1) * d + 2 ; for (j = 1, int_record = child, item_record = (*pheap) -> array[child]; int_record + j != (*pheap) -> size + 1 && j < d; j++) { if ((*pheap) -> array[int_record + j] < item_record) { item_record = (*pheap) -> array[int_record + j] ; child = j + int_record ; } } if (item > (*pheap) -> array[child]) (*pheap) -> array[i] = (*pheap) -> array[child] ; else break ; } return i ; }
相关文章推荐
- 用C语言实现解析简单配置文件的小工具
- C语言——Linux项目:文件复制命令的实现
- 二项队列实现文件C语言
- 自顶向下伸展树实现文件C语言
- C语言实现贪吃蛇(四)----游戏存档读档(文件操作)
- C语言实现修改文本文件中的特定行
- 读取配置文件[方式一]之纯C语言实现
- 栈数组实现实现文件C语言
- Linux下C语言实现多线程文件复制
- 在linux下用C语言实现递归查看某个目录中的所有文件
- 使用C语言实现pcm格式音频文件播放速度的改变
- C语言实现责任链二通过配置文件改变处理逻辑
- 修改文件后缀的C语言实现
- C语言实现写入日志文件
- 用c语言写cgi程序(3)---实现文件上传
- c语言实现配置文件的读写
- win7环境下实现cl.exe编译c语言 生成.i .asm .obj .exe 文件
- C语言实现修改文本文件中的特定行
- C语言实现读取目录和文件
- 分离链接散列表实现文件C语言