#include "BinomialHeap.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
Heap make_binomial_heap_with_keys(int keys[], int n)
{
Heap h = NULL, tmp = NULL;
for(int i = 0; i < n; i++)
{
tmp = make_binomial_heap_with_key(keys[i]);
if(NULL == h)
{
h = tmp;
}
else
{
//否则插入合并两个二项堆
h = binomial_heap_union(h, tmp);
tmp = NULL;
}
}
return h;
}
Heap make_binomial_heap_with_key(int key)
{
Heap h = NULL;
h = make_binomial_heap();
h->key = key;
return h;
}
Heap make_binomial_heap()
{
Heap h = NULL;
if((h = (Heap) malloc (HEAP_NODE_SIZE))==NULL)
{
exit(-1);
}
memset(h, 0, HEAP_NODE_SIZE);
return h;
}
Heap binomial_heap_union(Heap &h1, Heap &h2)
{
Heap h = NULL;
h = binoimal_heap_merge(h1, h2);
return h;
}
Heap binoimal_heap_merge(Heap &h1,Heap &h2)
{
Heap h = NULL, l = NULL, r = NULL, q = NULL, p = NULL;// h为返回堆的首地址,l为指向h1的当前根结点,r为指向h2的当前根结点, q为指向新堆p的前结点,p为指向新堆根结点
if((h1 != NULL) && (h2 != NULL))
{
l = h1;
r = h2;
while((l != NULL) && (r != NULL))
{
if(l->degree <= r->degree)
{
p = l;
l = l->slibing;
}
else
{
p = r;
r = r->slibing;
}
if(q == NULL)
{// 找到新二项树的首根结点
q = p;
h = p;
}
else
{
q->slibing = p;
q = p;
}
}
if(l != NULL)
{
p->slibing = l;
}
else
{
p->slibing = r;
}
}
else if(h1 != NULL)
{
h = h1;
}
else
{
h = h2;
}
h1 = NULL;
h2 = NULL;
return h;
}
void print_binomial_heap(const Heap h)
{
if(NULL == h)
return;
printf("Print the binomial heap:\n");
_print_binomial_heap(h);
printf("\n\n");
}
void _print_binomial_heap(const Heap h)
{
if(NULL == h)
return;
printf("(");
printf("%d", h->key);
//显示其孩子
Heap p = h->child;
if(NULL != p)
{
printf("(");
_print_binomial_heap(p);
printf(")");
}
printf(")");
//显示其兄弟
p = h->slibing;
while(NULL != p)
{
_print_binomial_heap(p);
p = p->slibing;
}
}
void destroy_binomial_heap(Heap &h)
{
if(h == NULL)
return;
destroy_binomial_heap(h->child);
Heap s = NULL;
s = h->slibing;
while(s != NULL)
{
destroy_binomial_heap(s);
s = s->slibing;
}
destroy_binomial_heap(h);
h = NULL;
}
#define HEAP1_KEYS_N 10
int heap1_keys[HEAP1_KEYS_N] = {1, 7, 5, 3, 9, 10, 2, 4, 6, 8};
int main()
{
Heap heap1 = NULL;
heap1 = make_binomial_heap_with_keys(heap1_keys, 2);
print_binomial_heap(heap1);
destroy_binomial_heap(heap1);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
二项堆(binomial heap)的源代码
共14个文件
pdb:2个
h:1个
exe:1个
4星 · 超过85%的资源 需积分: 13 26 下载量 171 浏览量
2011-08-31
09:18:45
上传
评论 2
收藏 170KB RAR 举报
温馨提示
这是《算法导论》中第19章二项堆的源代码实现,在VC6.0下编译通过,包括插入、删除、提取最小结点等操作实现。
资源推荐
资源详情
资源评论
收起资源包目录
BinomialHeap.rar (14个子文件)
BinomialHeap
BinomialHeap.dsp 4KB
BinomialHeap.ncb 33KB
BinomialHeap.plg 1KB
BinomialHeap.opt 48KB
Debug
BinomialHeap.exe 168KB
BinomialHeap.ilk 181KB
vc60.idb 41KB
BinomialHeap.pch 221KB
vc60.pdb 52KB
BinomialHeap.pdb 417KB
BinomialHeap.obj 9KB
BinomialHeap.cpp 3KB
BinomialHeap.dsw 547B
BinomialHeap.h 1KB
共 14 条
- 1
资源评论
- 将在全站显示abc2012-12-15很棒,数据结构刚好学到了这
- wrong10302013-01-14代码风格很好,很容易理解。
- lacuss12014-02-26东西应该不错,不过不是我想要的fabonacci
软件真理与光
- 粉丝: 2w+
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 高性能量化工具 hikyuu 2.0.3 python3.9 ubuntu 安装包
- Cyclone Version 9.51
- 高性能量化回测工具 hikyuu 2.0.3 python 3.12 windows 安装包
- 省级城乡居民基本养老保险情况数据集(2010-2022年).xlsx
- 舞队填写版.cpp
- 基于BP神经网络的多输入单输出回归预测.zip
- 高性能量化回测工具 hikyuu 2.0.3 python 3.9 windows 安装包
- 省级城镇职工基本养老保险情况2000-2022年.xlsx
- 高性能量化回测工具 hikyuu 2.0.3 python 3.10 windows 安装包
- 算法部署-使用OpenVINO+C#部署PaddleOCR字符识别算法-项目源码-优质项目实战.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功