标程_堆

标程_堆

大根堆

#include<iostream>
#include<cstdio>
using namespace std;
const int maxlenth=0;
class heap
{
	private:
		int h[maxlenth];
		int len;
		void up(int x);//上移操作,把h[x]上移的合适位置
		void down(int x);//下移操作,把h[x]下移的合适位置
		bool cmp(int x,int y);//若x比y优,return 1,若y比x优,return 0 
	public:
		void init();//初始化,len=0 
		void swap(int &t1,int &t2);//交换两数
		void build(int x);//建堆  
		void sort();//堆排序
		void del(int x);//删除操作,删除h[x]
		void push(int x);//插入操作,把x加入堆的合适位置 
		int top();//返回堆顶的值
		void pop();//删除堆顶 
		bool empty();//判断堆是否空 
		int size();//返回堆的长度 
};
void heap::init()
{
	len=0;
	return;
}
bool heap::cmp(int x,int y)//若x比y优,return 1,若y比x优,return 0 
{
	return h[x]>h[y];
}
void heap::swap(int &t1,int &t2)//交换两数 
{
	int t=t1;
	t1=t2,t2=t;
	return;
}
void heap::up(int x)//上移操作,把h[x]上移的合适位置 
{
	if(x>len)return;
	for(;x>1;x>>=1)
		if(cmp(x,x>>1)) swap(h[x],h[x>>1]);
		else return;
	return;
}
void heap::down(int x)//下移操作,把h[x]下移的合适位置
{
	if(x<1)return;
	for(x<<=1;x<=len;x<<=1)
	{
		x+=(x+1<=len&&cmp(x+1,x));
		if(cmp(x,x>>1)) swap(h[x>>1],h[x]);
		else return;
	}
	return;
}
void heap::push(int x)//插入操作,把x加入堆的合适位置 
{
	h[++len]=x;
	up(len);
	return;
}
void heap::del(int x)//删除操作,删除h[x] 
{
	if(x<1||x>len)return;
	if(cmp(len,x)) h[x]=h[len--],up(x);
	else h[x]=h[len--],down(x);
	return;
}
void heap::sort()//堆排序 
{
	for(;len;h[1]=h[len--],down(1))
	{
		//a[i]=h[1];
		printf("%d",h[1]);
	}
	return;
}
void heap::build(int x)//建堆 
{
	int i;
	for(len=0,i=1;i<=x;i++)
	{
	    //h[++len]=a[i];
		scanf("%d",&h[++len]);
	    up(len);
	}
	return;
}
int heap::top()//返回堆顶的值 
{
	if(empty())//堆为空 
		return 0;//崩溃 
	return h[1];
}
void heap::pop()//删除堆顶 
{
	del(1);
	return;
}
bool heap::empty()//判断堆是否空 
{
	return !len;
}
int heap::size()//返回堆的长度 
{
	return len;
}
int main()
{
	int x,y;
	heap h;
	h.init();//初始化,len=0 
	h.build(x);//建堆  
	h.sort();//堆排序
	h.del(x);//删除操作,删除h[x]
	h.push(x);//插入操作,把x加入堆的合适位置 
	h.top();//返回堆顶的值
	h.pop();//删除堆顶 
	h.empty();//判断堆是否空 
	h.size();//返回堆的长度 
	h.swap(x,y);//交换两数
	return 0;
}

小根堆

#include<iostream>
#include<cstdio>
using namespace std;
const int maxlenth=0;
class heap
{
	private:
		int h[maxlenth];
		int len;
		void up(int x);//上移操作,把h[x]上移的合适位置
		void down(int x);//下移操作,把h[x]下移的合适位置
		bool cmp(int x,int y);//若x比y优,return 1,若y比x优,return 0 
	public:
		void init();//初始化,len=0 
		void swap(int &t1,int &t2);//交换两数
		void build(int x);//建堆  
		void sort();//堆排序
		void del(int x);//删除操作,删除h[x]
		void push(int x);//插入操作,把x加入堆的合适位置 
		int top();//返回堆顶的值
		void pop();//删除堆顶 
		bool empty();//判断堆是否空 
		int size();//返回堆的长度 
};
void heap::init()
{
	len=0;
	return;
}
bool heap::cmp(int x,int y)//若x比y优,return 1,若y比x优,return 0 
{
	return h[x]<h[y];
}
void heap::swap(int &t1,int &t2)//交换两数 
{
	int t=t1;
	t1=t2,t2=t;
	return;
}
void heap::up(int x)//上移操作,把h[x]上移的合适位置 
{
	if(x>len)return;
	for(;x>1;x>>=1)
		if(cmp(x,x>>1)) swap(h[x],h[x>>1]);
		else return;
	return;
}
void heap::down(int x)//下移操作,把h[x]下移的合适位置
{
	if(x<1)return;
	for(x<<=1;x<=len;x<<=1)
	{
		x+=(x+1<=len&&cmp(x+1,x));
		if(cmp(x,x>>1)) swap(h[x>>1],h[x]);
		else return;
	}
	return;
}
void heap::push(int x)//插入操作,把x加入堆的合适位置 
{
	h[++len]=x;
	up(len);
	return;
}
void heap::del(int x)//删除操作,删除h[x] 
{
	if(x<1||x>len)return;
	if(cmp(len,x)) h[x]=h[len--],up(x);
	else h[x]=h[len--],down(x);
	return;
}
void heap::sort()//堆排序 
{
	for(;len;h[1]=h[len--],down(1))
	{
		//a[i]=h[1];
		printf("%d",h[1]);
	}
	return;
}
void heap::build(int x)//建堆 
{
	int i;
	for(len=0,i=1;i<=x;i++)
	{
	    //h[++len]=a[i];
		scanf("%d",&h[++len]);
	    up(len);
	}
	return;
}
int heap::top()//返回堆顶的值 
{
	if(empty())//堆为空 
		return 0;//崩溃 
	return h[1];
}
void heap::pop()//删除堆顶 
{
	del(1);
	return;
}
bool heap::empty()//判断堆是否空 
{
	return !len;
}
int heap::size()//返回堆的长度 
{
	return len;
}
int main()
{
	int x,y;
	heap h;
	h.init();//初始化,len=0 
	h.build(x);//建堆  
	h.sort();//堆排序
	h.del(x);//删除操作,删除h[x]
	h.push(x);//插入操作,把x加入堆的合适位置 
	h.top();//返回堆顶的值
	h.pop();//删除堆顶 
	h.empty();//判断堆是否空 
	h.size();//返回堆的长度 
	h.swap(x,y);//交换两数
	return 0;
}

结构体

#include<iostream>
#include<cstdio>
using namespace std;
const int maxlenth=0;
struct jgt
{
	int s;
};
class heap
{
	private:
		jgt h[maxlenth];
		int len;
		void up(int x);//上移操作,把h[x]上移的合适位置
		void down(int x);//下移操作,把h[x]下移的合适位置
		bool cmp(int x,int y);//若x比y优,return 1,若y比x优,return 0 
	public:
		void init();//初始化,len=0 
		void swap(jgt &t1,jgt &t2);//交换两数
		void build(int x);//建堆  
		void sort();//堆排序
		void del(int x);//删除操作,删除h[x]
		void push(jgt x);//插入操作,把x加入堆的合适位置 
		jgt top();//返回堆顶的值
		void pop();//删除堆顶 
		bool empty();//判断堆是否空 
		int size();//返回堆的长度 
};
void heap::init()
{
	len=0;
	return;
}
bool heap::cmp(int x,int y)//若x比y优,return 1,若y比x优,return 0 
{
	return h[x].s<h[y].s;
}
void heap::swap(jgt &t1,jgt &t2)//交换两数 
{
	jgt t=t1;
	t1=t2,t2=t;
	return;
}
void heap::up(int x)//上移操作,把h[x]上移的合适位置 
{
	if(x>len)return;
	for(;x>1;x>>=1)
		if(cmp(x,x>>1)) swap(h[x],h[x>>1]);
		else return;
	return;
}
void heap::down(int x)//下移操作,把h[x]下移的合适位置
{
	if(x<1)return;
	for(x<<=1;x<=len;x<<=1)
	{
		x+=(x+1<=len&&cmp(x+1,x));
		if(cmp(x,x>>1)) swap(h[x>>1],h[x]);
		else return;
	}
	return;
}
void heap::push(jgt x)//插入操作,把x加入堆的合适位置 
{
	h[++len]=x;
	up(len);
	return;
}
void heap::del(int x)//删除操作,删除h[x] 
{
	if(x<1||x>len)return;
	if(cmp(len,x)) h[x]=h[len--],up(x);
	else h[x]=h[len--],down(x);
	return;
}
void heap::sort()//堆排序 
{
	for(;len;h[1]=h[len--],down(1))
	{
		//a[i]=h[1];
		printf("%d",h[1].s);
	}
	return;
}
void heap::build(int x)//建堆 
{
	int i;
	for(len=0,i=1;i<=x;i++)
	{
	    //h[++len]=a[i];
		scanf("%d",&h[++len].s);
	    up(len);
	}
	return;
}
jgt heap::top()//返回堆顶的值 
{
	jgt tem;
	if(empty())//堆为空 
		return tem;//崩溃 
	return h[1];
}
void heap::pop()//删除堆顶 
{
	del(1);
	return;
}
bool heap::empty()//判断堆是否空 
{
	return !len;
}
int heap::size()//返回堆的长度 
{
	return len;
}
int main()
{
	int x;
	jgt y,z;
	heap h;
	h.init();//初始化,len=0 
	h.build(x);//建堆  
	h.sort();//堆排序
	h.del(x);//删除操作,删除h[x]
	h.push(y);//插入操作,把x加入堆的合适位置 
	h.top();//返回堆顶的值
	h.pop();//删除堆顶 
	h.empty();//判断堆是否空 
	h.size();//返回堆的长度 
	h.swap(y,z);//交换两数
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值