简介:
MinHeap.h

template<typename Type> class MinHeap{
public:
	MinHeap(int size):m_nMaxSize(size > defaultsize ? size : defaultsize)
			,m_pheap(new Type[m_nMaxSize]),m_ncurrentsize(0){}
	MinHeap(Type heap[],int n);		//initialize heap by a array
	~MinHeap(){
		delete[] m_pheap;
	}

public:
	bool Insert(const Type item);	//insert element
	bool Delete(const Type item);	//delete element
	bool IsEmpty() const{			
		return m_ncurrentsize == 0;
	}
	bool IsFull() const{
		reutrn m_ncurrentsize == m_nMaxSize;
	}
	void Print(const int start=0, int n=0);			

private:
    //adjust the elements of the child tree with the root of start from top to bottom
	void Adjust(const int start, const int end);	

private:
	static const int defaultsize = 100;
	const int m_nMaxSize;	
	Type *m_pheap;
	int m_ncurrentsize;
};

template<typename Type> void MinHeap<Type>::Adjust(const int start, const int end){
	int i = start,j = i*2+1;    //get the position of the child of i
	Type temp=m_pheap[i];
	while(j <= end){    
		if(j<end && m_pheap[j]>m_pheap[j+1]){   //left>right
			j++;
		}
		if(temp <= m_pheap[j]){ //adjust over
			break;
		}
		else{   //change the parent and the child, then adjust the child
			m_pheap[i] = m_pheap[j];
			i = j;
			j = 2*i+1;
		}
	}
	m_pheap[i] = temp;
}

template<typename Type> MinHeap<Type>::MinHeap(Type heap[], int n):m_nMaxSize(
		n > defaultsize ? n : defaultsize){
	m_pheap = new Type[m_nMaxSize];
	for(int i=0; i<n; i++){
		m_pheap[i] = heap[i];
	}
	m_ncurrentsize = n;
	int pos=(n-2)/2;	//Find the last child tree which has more than one element;
	while(pos>=0){
		Adjust(pos, n-1);
		pos--;
	}
}

template<typename Type> bool MinHeap<Type>::Insert(const Type item){
	if(m_ncurrentsize == m_nMaxSize){
		cerr<<"Heap Full!"<<endl;
		return 0;
	}
	m_pheap[m_ncurrentsize] = item;
	int j = m_ncurrentsize, i = (j-1)/2;    //get the position of the parent of j
	Type temp = m_pheap[j];
	while(j > 0){   //adjust from bottom to top
		if(m_pheap[i] <= temp){
			break;
		}
		else{
			m_pheap[j] = m_pheap[i];
			j = i;
			i = (j-1)/2;
		}
	}
	m_pheap[j] = temp;
	m_ncurrentsize++;
	return 1;
}

template<typename Type> bool MinHeap<Type>::Delete(const Type item){
	if(0 == m_ncurrentsize){
		cerr<<"Heap Empty!"<<endl;
		return 0;
	}
	for(int i=0; i<m_ncurrentsize; i++){
		if(m_pheap[i] == item){
			m_pheap[i] = m_pheap[m_ncurrentsize-1]; //filled with the last element
			Adjust(i,m_ncurrentsize-2);     //adjust the tree with start of i
			m_ncurrentsize--;
			i=0;
		}
	}
	return 1;
}

template<typename Type> void MinHeap<Type>::Print(const int start, int n){
	if(start >= m_ncurrentsize){
		return;
	}
	Print(start*2+2, n+1);  //print the right child tree

	for(int i=0; i<n; i++){
		cout<<"    ";
	}
	cout<< m_pheap[start] << "--->" << endl;

	Print(start*2+1, n+1);  //print the left child tree
}

test.cpp
#include <iostream>

using namespace std;

#include "MinHeap.h"

int main(){
	int init[30]={17,6,22,29,14,0,21,13,27,18,2,28,8
			,26,3,12,20,4,9,23,15,1,11,5,19,24,16,7,10,25};
	MinHeap<int> heap(init,30);
	heap.Print();
	cout<<endl<<endl<<endl;

	heap.Insert(20);
	heap.Print();
	cout<<endl<<endl<<endl;
	
	heap.Delete(20);
	heap.Print();
	cout<<endl<<endl<<endl;
	return 0;
}
目录
相关文章
|
存储 算法 索引
堆的实现(C版)
堆的实现(C版)
56 0
|
3月前
|
算法 Java
堆内存分配策略解密
本文深入探讨了Java虚拟机中堆内存的分配策略,包括新生代(Eden区和Survivor区)与老年代的分配机制。新生代对象优先分配在Eden区,当空间不足时执行Minor GC并将存活对象移至Survivor区;老年代则用于存放长期存活或大对象,避免频繁内存拷贝。通过动态对象年龄判定优化晋升策略,并介绍Full GC触发条件。理解这些策略有助于提高程序性能和稳定性。
|
6月前
|
前端开发 算法 JavaScript
最小堆最大堆了解吗?一文了解堆在前端中的应用
该文章详细解释了堆数据结构(特别是最小堆)的概念与性质,并提供了使用JavaScript实现最小堆的具体代码示例,包括堆的插入、删除等操作方法。
最小堆最大堆了解吗?一文了解堆在前端中的应用
|
算法
堆的实现以及应用
我们说堆在物理上是一个数组,逻辑上它是一个完全二叉树,我们可以通过它的下标来计算父亲和孩子之间的关系。
|
SQL 监控 Java
JVM堆内存释放不及时问题
JVM堆内存释放不及时问题
256 0
|
存储 缓存 算法
08-堆(二)
08-堆(二)
153 0
|
存储 缓存 Java
08-堆(一)
08-堆(一)
85 0
|
存储 缓存 Oracle
08-堆(三)
08-堆(三)
95 0