堆的代码实现

#include<iostream>
#include<assert.h>
#include<stdlib.h>

#define DefaultSize 1000
using namespace std;

struct car {
	string add;
	int num;
	int n;
} a[100];

template<class E>
class MinHeap {
	public:
		MinHeap(int sz = 100);           //构造函数 
		MinHeap(E arr[],int n);          //构造函数:通过一个数组建堆 
		~MinHeap() {                     //析构函数 
			delete []heap;               
		}
		bool Insert(const E &x);        //将x插入到最小堆中
		E RemoveMin();                  //删除堆顶上的最小元素 
		E IsEmpty()const {             //判断空否?空返回1,否则0 
			return (currentSize == 0) ? true : false;
		}
		bool IsFull()const {             //判断满否?满返回1,否则0 
			return (currentSize == maxHeapSize) ? true : false;
		}
		void MakeEmpty() {               // 置空堆 
			currentSize = 0;
		}
		void output();
	private:
		car *heap;                      //存放最小堆中元素的数组 
		int currentSize;                //最小堆中当前元素个数 
		int maxHeapSize;                //最小堆最多允许元素个数 
		void siftDown(int start,int m); //从Start到m下滑调整成为最小堆 
		void siftUp(int start);         // 从Start到0上滑调整成为最小堆 
};


//创建最小堆的两个构造函数 
template<class E>
MinHeap<E>::MinHeap(int sz) {
	maxHeapSize = (DefaultSize < sz) ?sz: DefaultSize;
	heap = new car[maxHeapSize];
	if(heap == NULL) {
		cerr<<"堆存储分配失败!"<<endl;
		exit(1);
	}
	currentSize = 0;
};

template<class E>
MinHeap<E>::MinHeap(E arr[],int n) {
	maxHeapSize = (DefaultSize<n)? n: DefaultSize;
	heap = new car[maxHeapSize];
	if(heap == NULL) {
		cerr<<"堆存储分配失败!"<<endl;
		exit(1);
	}
	for(int i = 0; i<n; i++) heap[i] = arr[i];
	currentSize = n;
	int currentPos = (currentSize-2)/2;
	while(currentPos >= 0) {
		siftDown(currentPos,currentSize-1);
		currentPos--;
	}
};


//最小堆的下滑调整算法 
template<class E>
void MinHeap<E>::siftDown(int start, int m) {
	int i = start,j = 2*i+1;
	E temp = heap[i].num;
	while(j<=m) {
		if(j<m&&heap[j].num>heap[j+1].num) j++;
		if(temp<=heap[j].num)break;
		else {
			heap[i].num = heap[j].num;
			i=j;
			j=2*j+1;
		}
	}
	heap[i].num = temp;
};


//最小堆的上滑调整操作 
template<class E>
void MinHeap<E>::siftUp(int start) {
	int j=start,i=(j-1)/2;
	E temp = heap[j].num;
	while(j>0) {
		if(heap[i].num<=temp) break;
		else {
			heap[j].num = heap[i].num;
			j=i;
			i = (i-1)/2;
		}
	}
	heap[j].num=temp;
};


//最小堆的插入算法 
template<class E>
bool MinHeap<E>::Insert(const E &x) {
	heap[currentSize].num=x;
	siftUp(currentSize);
	currentSize++;
	return true;

};


//最小堆的删除算法 
template<class E>
E MinHeap<E>::RemoveMin(){
	if(!currentSize){cerr<<"Heap Full"<<endl;return false;}
	E x=heap[0].num;heap[0].num=heap[currentSize-1].num;
	currentSize--;
	siftDown(0,currentSize-1);
	return x;
};


int main() {
	struct car a[100];
	int sum=0,x=0;
	int b[100];
	int i=0;
	MinHeap<int> test(100);
    cout<<"请输入当前各个路口信息:"<<endl; 
	while(cin>>a[i].add>>a[i].num)
	{
		a[i].n=1;	
		i++;
		sum++;
	}
	//cmp(a,sum);
	for(i=0;i<sum;i++){
		test.Insert(a[i].num);
	}
    for(i=0;i<sum;i++){
    	b[i]=test.RemoveMin();
	}
	cout<<"以下是最拥挤的十个路口:"<<endl;
	for(i=sum-1;i>=sum-10;i--)
		for(int j=sum-1;j>=0;j--)
		{
			if(x<=10){
				if(a[j].num==b[i]&&a[j].n==1)
				{
					a[j].n=0;
					cout<<a[j].add<<endl;
					x++;
				}
			}
			
		}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦极必反

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值