C源码@数据结构与算法->LeftistHeap

/*
 * testleft.cpp
 */
#include 
  
  
   
   
#include "leftheap.h"

#define MaxSize 5000

int main()
{
	PriorityQueue H;
	int i, j;

	H = Initialize();
	for (i = 0, j = MaxSize / 2; i < MaxSize; ++i, j = (j + 17) % MaxSize)
	{
		Insert(j, H);
	}

	j = 0;
	while (!IsEmpty(H))
	{
		if (FindMin(H) != j++)
		{
			printf("Error in DeleteMin, %d\n", j);
		}
		else
		{
			H = DeleteMin1(H);
		}
	}
	printf("done!\n");

	return 0;
}
  
  

/*
 * fatal.h
 */
#include 
   
   
    
    
#include 
    
    
     
     

#define Error(Str)		FatalError(Str)
#define FatalError(Str)	fprintf(stderr, "%s\n", Str), exit(-1)	
    
    
   
   
/*
 * leftheap.h
 */
#ifndef _LEFT_HEAP_H
#define _LEFT_HEAP_H

#ifndef NULL
#define NULL (0)
#endif

typedef int ElementType;

struct TreeNode;
typedef struct TreeNode *PriorityQueue;

/*
 * Minimal set of priority queue operations.
 * Note that nodes will be shared among several leftist heaps after a merge;
 * the user must make sure to not use the old leftist heaps.
 */

#ifdef __cplusplus
extern "C" {
#endif

PriorityQueue Initialize();
ElementType FindMin(PriorityQueue H);
int IsEmpty(PriorityQueue H);
PriorityQueue Merge(PriorityQueue H1, PriorityQueue H2);

#define Insert(X, H) ( H = Insert1((X), H))
PriorityQueue Insert1(ElementType X, PriorityQueue H);
PriorityQueue DeleteMin1(PriorityQueue H);

#ifdef __cplusplus
}
#endif

#endif /* _LEFT_HEAP_H */
/*
 * leftheap.cpp
 */
#include 
   
   
    
    
#include "fatal.h"
#include "leftheap.h"

struct TreeNode
{
	ElementType Element;
	PriorityQueue Left;
	PriorityQueue Right;
	int Npl;	/* NULL Path Length */
};

PriorityQueue Initialize()
{
	return NULL;
}

static PriorityQueue Merge1(PriorityQueue H1, PriorityQueue H2);

PriorityQueue Merge(PriorityQueue H1, PriorityQueue H2)
{
	if (H1 == NULL)
	{
		return H2;
	}
	if (H2 == NULL)
	{
		return H1;
	}
	if (H1->Element < H2->Element)
	{
		return Merge1(H1, H2);
	}
	else
	{
		return Merge1(H2, H1);
	}
}

void SwapChildren(PriorityQueue H)
{
	PriorityQueue Tmp;
	
	Tmp = H->Left;
	H->Left = H->Right;
	H->Right = Tmp;
}

static PriorityQueue Merge1(PriorityQueue H1, PriorityQueue H2)
{
	if (H1->Left == NULL)	/* Single node */
	{						/* H1->Right is already NULL */
		H1->Left = H2;
	}
	else
	{
		H1->Right = Merge(H1->Right, H2);
		if (H1->Left->Npl < H1->Right->Npl)
		{
			SwapChildren(H1);
		}
		H1->Npl = H1->Right->Npl + 1;
	}

	return H1;
}

PriorityQueue Insert1(ElementType X, PriorityQueue H)
{
	PriorityQueue SingleNode;

	SingleNode = (PriorityQueue)malloc(sizeof(struct TreeNode));
	if (SingleNode == NULL)
	{
		FatalError("Out of space!");
	}
	else
	{
		SingleNode->Element = X;
		SingleNode->Npl = 0;
		SingleNode->Left = NULL;
		SingleNode->Right = NULL;
		H = Merge(SingleNode, H);
	}

	return H;
}

PriorityQueue DeleteMin1(PriorityQueue H)
{
	PriorityQueue LeftHeap, RightHeap;

	if (IsEmpty(H))
	{
		FatalError("Priority queue is empty!");
	}
	
	LeftHeap = H->Left;
	RightHeap = H->Right;
	free(H);
	return Merge(LeftHeap, RightHeap);
}

ElementType FindMin(PriorityQueue H)
{
	if (!IsEmpty(H))
	{
		return H->Element;
	}
	else
	{
		FatalError("Priority queue is empty!");
		return -1;		/* Return value used to avoid warning */
	}
}

int IsEmpty(PriorityQueue H)
{
	return H == NULL;
}
   
   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值