C++最大Heap

本文介绍了一个通用的最大堆实现,并展示了如何使用该实现进行插入、删除最大元素等基本操作。通过具体的代码示例,读者可以了解到最大堆的内部工作原理及其在实际应用中的使用方法。

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

#include <iostream>
using namespace std;
template <class T>
class MaxHeap{
	T* heap;
	int curSize;
	int maxSize;
	public:
		MaxHeap(int maxSize=10)
		{
		   maxSize=maxSize;
		   curSize=0;
		   heap=new T[maxSize+1];
		}
		~MaxHeap(){
			delete heap;
		}
		//get the curSize of this heap
		int Get_Size() const 
		{
			return curSize;
		}
		//get the max value,at the first position of the array
		T Get_Max()
		{
			if (curSize==0)
			{
				return NULL;
			}
			else
			{
				return heap[1];
			}
		}
		//insert a new element into the heap
		void insert(const T& x)
		{
			if (curSize==maxSize)
			{
				cout<<"the heap is full"<<endl;
				return;
			}
			int i=++curSize;
			while (i>=1&&x>heap[i/2])
			{
				heap[i]=heap[i/2];
				i/=2;
			}
			heap[i]=x;
			cout<<"insert successfully: "<<x<<endl;

		}
		//delete the element with the max value,and save it into the parameter
		void DeleteMax(T& x)
		{
			if (curSize==0)
			{
				cout<<"the heap is empty,can not delete"<<endl;
				x=-9999;
				return;
			}
            x=heap[1];
			heap[0]=heap[curSize--];
			int i=1;
			int ci=2;
			while (ci<=curSize)
			{
				//ci是较大的孩子的位置
				if (heap[ci]<heap[ci+1]&&ci<=curSize)
				{
					ci++;
				}
				if (heap[0]>heap[ci])
				{
					break;
				}
				else
				{
					heap[i]=heap[ci];//move up the ci
					i=ci;
					ci*=2;
				}


			}
			heap[i]=heap[0];

		}
		void Init_heap(T a[],int size,int maxsize)
		{
			delete[] heap;
			heap=new T[maxsize+1];
			this->curSize=size;
			this->maxSize=maxsize;
			for (int j=1;j<=size+1;j++)
			{
				
				heap[j]=a[j-1];
			}
			for (int i=size/2;i>=1;i--)
			{
				T y=heap[i];//the root element of the current subtree

				int c=2*i;//begin to find the position to place y	
                
				while (c<=size)
				{
				
					if (c<curSize&&heap[c+1]>heap[c])
					{
						c++;
					}
					if (y>=heap[c])
					{
						break;
					}
					heap[c/2]=heap[c];
					c*=2;
				}
				heap[c/2]=y;
			}
			
		}

};
int main()  
{  
    MaxHeap<int> hp;  
    int a[11]={-111,5,2,12,3,55,21,7,9,11,9};  
	
    cout<<"用数组a来初始化堆:"<<endl;  
    for(int i=1;i<11;i++)  
        cout<<a[i]<<"  ";  
    cout<<endl;  
	
    hp.Init_heap(a,10,15);  
	
    int max;  
    cout<<"目前堆中最大值为:"<<hp.Get_Max()<<endl;  
	
    hp.DeleteMax(max);  
    cout<<"删除的堆中最大的元素为:"<<max<<endl;  
    cout<<"删除后,现在堆中最大的元素为:"<<hp.Get_Max()<<endl;  
	
    cout<<"现在堆的大小为:"<<hp.Get_Size()<<endl;  
	
    cout<<"向堆中插入新元素"<<endl;  
	
    hp.insert(22);  
    hp.insert(45);  
    hp.insert(214);  
    hp.insert(16);  
    hp.insert(21);  
    hp.insert(121);  
    hp.insert(111);  
	
    cout<<"插入后现在堆的大小为:"<<hp.Get_Size()<<endl;  
	
    cout<<"现在由大到小输出堆中元素"<<endl;  
	
    do  
    {  
        hp.DeleteMax(max);  
        if(max== -9999)  
            break;  
        cout<<max<<"  ";  
    }while(1);  
	
    cout<<endl;  
	
    return 0;  
} 

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值