《数据结构与算法分析——c语言描述》 练习6.32 答案

这篇博客提供了《数据结构与算法分析——c语言描述》一书中练习6.32的答案,主要内容涉及如何优化合并过程,避免无效操作,并确保较少的树总是被合并到较大的树中。
摘要由CSDN通过智能技术生成

《数据结构与算法分析——c语言描述》 练习6.32 答案


避免merge(H,H)

H2中没有树留下且Carry树为NULL,修改merge例程以终止合并

修改merge使得较少的树总被合并到较大的树中


binomialqueue.h

#ifndef _BinomialQueue_H
#define _BinomialQueue_H
typedef int ElementType;

struct BinNode;

typedef struct BinNode *BinTree;
typedef struct Collection *BinQueue;

BinQueue initialize(void);
ElementType findMin(BinQueue h);
int isEmpty(BinQueue h);
BinQueue merge(BinQueue &h1, BinQueue &h2);
void insert(ElementType X, BinQueue h);
BinQueue deleteMin(BinQueue h);
#endif // !_BinHeap_H


binomialqueue.cpp

#include"binomialqueue.h"
#include"fatal.h"

#define MAXTREES 25
#define CAPACITY ((1<<MAXTREES)-1)//容量是2^0+2^1+2^3+2^(MAXTREES-1)
typedef struct BinNode *Position;

struct BinNode {
	ElementType element;
	Position leftChild;
	Position nextSibling;
};

struct Collection {
	int currentSize;
	BinTree theTrees[MAXTREES];
};

BinQueue initialize(void) {
	BinQueue h = (BinQueue)malloc(sizeof(struct Collection));
	if (h == NULL)
		Error("OUT OF MEMORY");
	for (int i = 0; i < MAXTREES; i++)
		h->theTrees[i] = NULL;
	h->currentSize = 0;
	return h;
}

ElementType findMin(BinQueue h) {
	if (isEmpty(h))
		Error("
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值