资源简介
二项堆(Binomial Heap)的c语言完全实现,包括添加,删除,和找到最小值,删除最小值
代码片段和文件信息
#include
#include
#include “queue.h“
typedef int ElemType;
// basic structure of binomial heap
typedef struct Node {
ElemType key;
int degree;
struct Node *p;
struct Node *child;
struct Node *sibling;
}Node;
// the head of binomial heap
typedef struct heap{
Node * head;
}*BinomialHeap;
// generate a node valuing key
Node * generateNode(ElemType key) {
Node * x = (Node *) malloc(sizeof(Node));
if (x == NULL) {
printf(“memory allocation of node failed“);
exit(-1);
}
x->key = key;
x->p = NULL;
x->child = NULL;
x->sibling = NULL;
x->degree = 0;
return x;
}
// generate an empty binomial heap
BinomialHeap makeBinomialHeap() {
BinomialHeap h = (BinomialHeap) malloc(sizeof(struct heap));
if(!h) {
printf(“the memory allocation of heap failed!\n“);
exit(-1);
}
h->head = NULL;
return h;
}
// find the minimum key from the binomial heap
// and return thr minimum node‘s pointer
Node * binomialHeapMinimum(BinomialHeap h) {
Node * y = NULL;
Node * x = h->head;
if(x != NULL) {
int min = x->key;
y = x;
x = x->sibling;
while (x != NULL) {
if(x->key < min) {
min = x->key;
y = x;
}
x = x->sibling;
}
}
return y;
}
// links the Bk-1 tree rooted at node y to the Bk-1 tree
// rooted at node z; that is it makes z the parent of y.
// Node z thus becomes the root of a Bk tree
void binomiallink(Node * y Node *z) {
y->p = z;
y->sibling = z->child;
z->child = y;
z->degree += 1;
}
// merge the root lists of h1 and h2 into a single linked list
// that is sorted into monotonically increasing order.
Node * binomialMerge(BinomialHeap h1 BinomialHeap h2) {
Node * firstNode = NULL;
Node * p = NULL;
Node * p1 = h1->head;
Node * p2 = h2->head;
if (p1 == NULL || p2 == NULL) {
if(p1 == NULL) {
firstNode = p2;
} else {
firstNode = p1;
}
return firstNode;
}
if (p1->degree < p2->degree) {
firstNode = p1;
p = firstNode;
p1 = p1->sibling;
} else {
firstNode = p2;
p = firstNode;
p2 = p2->sibling;
}
while (p1 != NULL && p2 != NULL) {
if (p1->degree < p2->degree) {
p->sibling = p1;
p = p1;
p1 = p1->sibling;
} else {
p->sibling = p2;
p = p2;
p2 = p2->sibling;
}
}
if (p1 != NULL) {
p->sibling = p1;
} else {
p->sibling = p2;
}
return firstNode;
}
// unite heaps h1 and h2 returning the resulting heap.
BinomialHeap binomialHeapUnion(BinomialHeap *h1 BinomialHeap *h2) {
BinomialHeap h = makeBinomialHeap();
h->head = binomialMerge(*h1 *h2);
free(*h1);
*h1 = NULL;
free(*h2);
*h2 = NULL;
if (h->head == NULL) {
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-02-18 03:08 BinomialHeap\
文件 6691 2017-02-18 03:08 BinomialHeap\main.c
文件 443 2017-02-17 06:28 BinomialHeap\queue.h
文件 170 2017-02-17 05:48 BinomialHeap\CMakeLists.txt
文件 1251 2017-02-17 06:36 BinomialHeap\queue.c
目录 0 2017-02-18 03:08 BinomialHeap\cmake-build-debug\
文件 17550 2017-02-18 03:08 BinomialHeap\cmake-build-debug\BinomialHeap
文件 1416 2017-02-17 00:55 BinomialHeap\cmake-build-debug\cmake_install.cmake
文件 6072 2017-02-17 05:48 BinomialHeap\cmake-build-debug\BinomialHeap.cbp
文件 5702 2017-02-17 05:48 BinomialHeap\cmake-build-debug\Makefile
文件 32184 2017-02-17 00:55 BinomialHeap\cmake-build-debug\CMakeCache.txt
目录 0 2017-02-18 03:09 BinomialHeap\.idea\
文件 24585 2017-02-18 03:09 BinomialHeap\.idea\workspace.xm
文件 137 2017-02-17 00:57 BinomialHeap\.idea\misc.xm
文件 97 2017-02-17 00:57 BinomialHeap\.idea\BinomialHeap.iml
文件 276 2017-02-17 00:55 BinomialHeap\.idea\modules.xm
目录 0 2017-02-18 03:08 BinomialHeap\cmake-build-debug\CMakeFiles\
文件 12612 2017-02-17 00:55 BinomialHeap\cmake-build-debug\CMakeFiles\feature_tests.bin
文件 282 2017-02-17 05:48 BinomialHeap\cmake-build-debug\CMakeFiles\clion-log.txt
文件 2449 2017-02-17 05:48 BinomialHeap\cmake-build-debug\CMakeFiles\Makefile.cmake
文件 3416 2017-02-17 05:48 BinomialHeap\cmake-build-debug\CMakeFiles\Makefile2
文件 2 2017-02-17 05:48 BinomialHeap\cmake-build-debug\CMakeFiles\progress.marks
文件 675 2017-02-17 00:55 BinomialHeap\cmake-build-debug\CMakeFiles\CMakeDirectoryInformation.cmake
文件 85 2017-02-17 05:48 BinomialHeap\cmake-build-debug\CMakeFiles\cmake.check_cache
文件 10011 2017-02-17 00:55 BinomialHeap\cmake-build-debug\CMakeFiles\feature_tests.cxx
文件 688 2017-02-17 00:55 BinomialHeap\cmake-build-debug\CMakeFiles\feature_tests.c
文件 40161 2017-02-17 00:55 BinomialHeap\cmake-build-debug\CMakeFiles\CMakeOutput.log
文件 59 2017-02-17 05:48 BinomialHeap\cmake-build-debug\CMakeFiles\clion-environment.txt
文件 263 2017-02-17 05:48 BinomialHeap\cmake-build-debug\CMakeFiles\TargetDirectories.txt
目录 0 2017-02-18 03:08 BinomialHeap\cmake-build-debug\CMakeFiles\BinomialHeap.dir\
文件 5744 2017-02-17 06:36 BinomialHeap\cmake-build-debug\CMakeFiles\BinomialHeap.dir\queue.c.o
............此处省略23个文件信息
- 上一篇:GDAL进行shapefile数据栅格化
- 下一篇:通讯录管理程序设计的C语言实现
评论
共有 条评论