算法导论—堆

<pre name="code" class="cpp">#include <vector>
#include <iostream>

using namespace std;

class UnderflowException { };

template<typename Comparable>
class BinaryHeap
{
public:
	explicit BinaryHeap(int capacity=100)
	:array(capacity+1),currentSize(0)
	{ }

	explicit BinaryHeap(const vector<Comparable> &items)
	:array(items.size()+10),currentSize(items.size())
	{
		for(int i=0;i<items.size();++i)
			array[i+1]=items[i];
		buildMaxHeap();
	}

	bool isEmpty() const
	{
		return currentSize==0;
	}

	int currSize() const
	{
		return currentSize;
	}

	void  print()
	{
		for(int i=1;i<=currentSize;++i)
			cout<<array[i]<<" ";
		cout<<endl;
	}

	const Comparable& findMax() const
	{
		if (isEmpty())
			throw UnderflowException{ };
		return array[1];
	}

	//Increase the value in position i to key
	int increaseKey(int i,const Comparable &key)
	{
		if (key<array[i])
		{
			cout<<"new key is smaller than current key,can not change"<<endl;
			return -1;
		}
		array[i]=key;
		while(i>1&&array[i/2]<array[i])
		{
			std::swap(array[i/2],array[i]);
			i=i/2;
		}
		return 0;
	}

	//Insert the x into the heap
	void insert(const Comparable &x)
	{
		if (currentSize==array.size()-1)
			array.resize(array.size()*2);

		int hole=++currentSize;
		array[hole]=INT_MIN;
		increaseKey(hole,x);

	}

	/*
	 *Remove the maximum item and return it
	 *Throws Underflow if empty
	 */

	 int deleteMax()
	 {
	 	if(isEmpty())
	 		throw UnderflowException{ };
	 	int tmp=array[1];
	 	std::swap(array[1],array[currentSize--]);
	 	maxHeapIfy(1);
	 	return tmp;
	 }

	 /**
	  *heapSort
	  */
	  void heapSort(vector<Comparable> &vec)
	  {
	  	while(currentSize)
	  	{
	  		int tmp=deleteMax();
	  		vec.push_back(tmp);
	  	}
	  }



private:
	int currentSize;  //number of elements in heap
	vector<Comparable> array;

	/**
	 *Establish heap order property from an arbitrary
	 *arrangement of items.Runs in nlgn time.
	 */

	void buildMaxHeap()
	{
		for(int i=currentSize/2;i>0;--i)
			maxHeapIfy(i);
	}

	/**
	 *保持最大堆的性质,使根节点大于子节点
	 */
	void maxHeapIfy(int hole)
    {
        int child;
        Comparable tmp = std::move(array[hole]);
        for( ;hole*2<=currentSize;hole=child)
        {
            child=hole*2; 
            // if the right child exist,choose the max child
            if( child != currentSize && array[ child + 1 ] > array[ child ])
                ++child;
            // if the max child greater than the father,exchange
            if( array[child]>tmp)
                array[hole]=std::move(array[child]);
            else
                break;
        }
        array[hole]=std::move(tmp);
    }
};




int main()
{

	vector<int> vec{4,1,3,2,16,9,10,14,8,7};
	BinaryHeap<int> heap{vec};
	heap.print();
	heap.increaseKey(9,15);
	heap.print();
	heap.insert(10);
	heap.print();
	vector<int> vecsort;
	heap.heapSort(vecsort);
	for(auto v:vecsort)
		cout<<v<<" ";
	cout<<endl;

}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值