二叉堆--二叉堆实现文件C语言

/* binary_heap.c -- 二叉堆实现文件 */ #include <stdio.h> #include <stdlib.h> #include "binary_heap.h" /* 局部函数声明 */ /* 返回 item 在 position 位置处上滤后应该出现堆中的位置索引 */ static int Percolate_Up (const Heap * const pheap, const Item item, const int position) ; /* 返回 item 在 position 位置处下滤后应该出现堆中的位置索引 */ static int Percolate_Down (const Heap * const pheap, const Item item, const int position) ; /* 接口函数定义 */ int InitializeHeap (Heap * const pheap, const int capacity) { *pheap = (struct heap *) malloc (sizeof (struct heap)) ; if (NULL == *pheap) { puts ("Out of space.[1]") ; return 0 ; } (*pheap) -> array = (Item *) malloc (sizeof (Item) * (capacity + 1)) ; if (NULL == (*pheap) -> array) { puts ("Out of space.[2]") ; free (*pheap) ; return 0 ; } (*pheap) -> capacity = capacity ; (*pheap) -> size = 0 ; (*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 ; (*pheap) -> size++ ; i = Percolate_Up (pheap, item, (*pheap) -> size) ; (*pheap) -> array[i] = item ; return 1 ; } Item DeleteMin (const Heap * const pheap) { Item min_item, item ; int position = 1, i; if (HeapIsEmpty (pheap)) { puts ("Priority queue is empty.") ; return (*pheap) -> array[0] ; } min_item = (*pheap) -> array[1] ; item = (*pheap) -> array[(*pheap) -> size] ; (*pheap) -> size-- ; i = Percolate_Down (pheap, item, position) ; (*pheap) -> array[i] = item ; return min_item ; } int DecreaseKey (const Heap * const pheap, const int position, const Item triangle) { Item item ; int i ; if (HeapIsEmpty (pheap) || position > (*pheap) -> size || triangle < 0) return 0 ; item = (*pheap) -> array[position] -= triangle ; i = Percolate_Up (pheap, item, position) ; (*pheap) -> array[i] = item ; return 1 ; } int IncreaseKey (const Heap * const pheap, const int position, const Item triangle) { Item item ; int i ; if (HeapIsEmpty (pheap) || position > (*pheap) -> size || triangle < 0) return 0 ; item = (*pheap) -> array[position] += triangle ; i = Percolate_Down (pheap, item, position) ; (*pheap) -> array[i] = item ; return 1 ; } int Delete (const Heap * const pheap, const int position) { Item triangle ; if (HeapIsEmpty (pheap) || position > (*pheap) -> size) return 0 ; // 求出确保将 position 处的值放到二叉堆首的改变量 triangle triangle = (*pheap) -> array[position] - (*pheap) -> array[1] + 1 ; DecreaseKey (pheap, position, triangle) ; DeleteMin (pheap) ; return 1 ; } int BuildHeap (const Heap * const pheap, const Item * const array, const int array_size) { Item item ; int ct, i ; if (!HeapIsEmpty (pheap) || array_size > (*pheap) -> capacity) return 0 ; // 以结构特性将数组中的元素放入二叉堆中 for (i = 1; i <= array_size; i++) { (*pheap) -> array[i] = array[i - 1] ; (*pheap) -> size++ ; } for (ct = (*pheap) -> size / 2; ct > 0; ct--) { item = (*pheap) -> array[ct] ; i = Percolate_Down (pheap, item, ct) ; (*pheap) -> array[i] = item ; } return 1 ; } void Traversal (const Heap * const pheap, void (* pfun) (const Item item)) { int ct, size ; for (ct = 1, size = (*pheap) -> size; ct <= size; ct++) (* pfun) ((*pheap) -> array[ct]) ; } 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 ; for (i = position; (*pheap) -> array[i / 2] > item; i /= 2) (*pheap) -> array[i] = (*pheap) -> array[i / 2] ; return i ; } static int Percolate_Down (const Heap * const pheap, const Item item, const int position) { int i, child ; for (i = position; i * 2 <= (*pheap) -> size; i = child) { child = i * 2; // 条件判断的前半部分是判断当前 child 位置上的结点是否有有兄弟 后半部分是比较与其右兄弟的大小关系.这个方法真不错 if (child != (*pheap) -> size && (*pheap) -> array[child + 1] < (*pheap) -> array[child]) child++ ; if (item > (*pheap) -> array[child]) (*pheap) -> array[i] = (*pheap) -> array[child] ; else break ; } return i ; }

你可能感兴趣的:(c,struct,insert,UP,语言,traversal)