堆(heap)基于数组的实现

本文介绍了如何使用数组来实现堆数据结构,包括定义异常处理类、存储项类,详细阐述了heap类的头文件和实现文件,并通过测试验证了堆的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 定义堆类的异常处理类

#ifndef HEAP_EXCEPTION_H_
#define HEAP_EXCEPTION_H_
#include<stdexcept>
#include<string>
class HeapException:public std::logic_error
{
	public:
		HeapException(const std::string &message="")
			:std::logic_error(message.c_str())
		{}
};
#endif


定义堆类的存储项类

#ifndef KEY_ITEM_H_
#define KEY_ITEM_H_
#include<string>
typedef std::string KeyType;
class KeyItem
{
	public:
		KeyItem(){}
		KeyItem(const KeyType & keyValue)
			:searchKey(keyValue)
		{}
		KeyType getKey()const
		{
			return searchKey;
		}
	private:
		KeyType searchKey;
};
#endif


heap类的头文件

#ifndef HEAP_ARRAY_H_
#define HEAP_ARRAY_H_
#include"key_item.h"
#include"heap_exception.h"
typedef KeyItem HeapItemType;
const int MAX_HEAP = 50;
class Heap
{
	public:
		Heap();//default constructor
		//copy constructor and destructor are
		//supplied by the complier

		//heap operations :
		virtual bool empty()const;
		//detemines whether a heap is empty
		//precondition :none;
		//postcondition :return true if the heap is empty;
		//otherwise return false
		virtual void heapInsert(const HeapItemType &newitem)
			throw(HeapException);
		//inserts a item into  a heap
		//precondition :newitem is  the item to be inserted
		//postcondition :If the heap is not full,newitem is in 
		//its proper position ;otherwise HeapException is thrown

		virtual void heapDelete(HeapItemType & rootItem)
			throw(HeapException);
		//retrieve and delete the item in the root of
		//the heap .this item has the largest search key
		//in the heap
		//precondition :none
		//postcondition :if the heap is not empty ,rootitem is the
		//retrieved item ,the item is deleted from the heap .
		//however,if the item is empty.removal is 
		//impossible and the function throws heap exception
		//void display()const; 

	protected:
		void heapRebuild(int root);
		//converts the semiheap rooted at index root into
		//a heap

	private:
		HeapItemType items[MAX_HEAP];
		int size;

};
#endif


heap类的实现文件

#include"heap_array.h"
#include<iostream>
using namespace std;
Heap::Heap():size(0)
{}
bool Heap::empty()const
{
	return (size==0);
}
void Heap::heapInsert(const HeapItemType &newitem)
	throw(HeapException)
{
	if(size>=MAX_HEAP)
		throw HeapException("HeapException:the heap is full !");
	else
	{
		items[size]=newitem;
		int place=size;
		int parent=(place-1)/2;
		while((parent>=0)&&(items[place].getKey()>items[parent].getKey()))
		{
			HeapItemType temp=items[parent];
			items[parent]=items[place];
			items[place]=temp;
			place=parent;
			parent=(place-1)/2;
		}
		size++;
		
	}
}
/**
void Heap::display()const
{
	for(int i=0;i<size;i++)
		cout<<items[i].getKey()<<endl;
}
**/
void Heap::heapDelete(HeapItemType &rootItem)
	throw(HeapException)
{
	if(empty())
		throw HeapException("HeapException :the heap is empty !");
	else
	{
		rootItem=items[0];
		items[0]=items[size-1];
		--size;
		heapRebuild(0);
	}
}
void Heap::heapRebuild(int root)
{
	int child=2*root+1;
	if(child<=size-1)
	{
		int rightChild=child+1;
		if(rightChild<=size-1)
			if(items[child].getKey()<items[rightChild].getKey())
				child=rightChild;
	
		//if the root's value is smaller than the value in the
		//larger child ,swap values
		if(items[root].getKey()<items[child].getKey())
		{
			HeapItemType temp=items[root];
			items[root]=items[child];
			items[child]=temp;
			//transform the new subtree into a heap
			heapRebuild(child);
		}	
	}	
}

 

堆的测试

#include"heap_array.h"
#include<iostream>
using namespace std;
int main()
{
	Heap aheap;

	try{
	aheap.heapInsert(KeyItem("sort"));
	aheap.heapInsert(KeyItem("exception"));
	aheap.heapInsert(KeyItem("STL"));
	aheap.heapInsert(KeyItem("queue"));
	aheap.heapInsert(KeyItem("priority"));
	aheap.heapInsert(KeyItem("heap"));
	aheap.heapInsert(KeyItem("item"));
	aheap.heapInsert(KeyItem("terrda"));
	aheap.heapInsert(KeyItem("absort"));
	aheap.heapInsert(KeyItem("oppo"));
//	aheap.display();
	KeyItem item;
	cout<<"the heap is :"<<endl;
	while(!aheap.empty())
	{
		aheap.heapDelete(item);
		cout<<item.getKey()<<endl;
	}
	}catch(HeapException &e)
	{
		cout<<e.what()<<endl;
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值