And heap priority queue curricular templates

All data structures, algorithms and applications curricular templates please click: https://blog.csdn.net/weixin_44077863/article/details/101691360

To supplement the two concepts

Maximal tree (the smallest tree): value of each node is greater than (less than) or equal to its child nodes worth tree

Maximum heap (min heap): maximum (minimum) of the complete binary tree

Note Note that the stack is a complete binary tree, and the root of each such subtree meet a maximum (minimum conditions)

Heap's strengths is that he represents the binary tree with an array of easy to handle, and can be found in a large number of first k quickly

Then the maximum heap called large root heap, heap big top, the smallest pile Similarly, this paper explains the operation of large root heap, because the small heap root empathy

About heap, built mainly talk about his stack, insert, delete, and in order to achieve these operations will increase mentioned (siftup) and down (siftdown operation)

The so-called up-regulation refers to the current node may point greater than the parent node, and then exchange the father, may he now bigger than the parent node, then all the way to the exchange, which is siftup

The so-called down-regulation is the same reason, he may be smaller than his son, all the way down the exchange, but pay attention to the time change and the larger of the two sons of change

Insertion is simple, the current node into the final position can then siftup

Remove also very simple, because the last position if deleted directly deleted, then the value of the switching node to be deleted and the last node, delete the last node, and then to delete the original node siftdown

Built heap, you can insert one by one, the complexity of O (nlogn)

But floyd is very powerful, we use floyd built stacks way up to O (n), but only if there is a sequence of built heap, if you do not have a set number of course can only be inserted

He said that a number of groups seen as incorrect position of the stack, you can simply cut up to each non-leaf nodes from under

Do not move the last layer, the second layer down up down once, so this way to reach the O (n) level

And heap priority queue is actually the same thing, but the priority queue only (top of the heap) the operation of the first team Bale

And heap priority queue template as follows: (priority_queue have to explain heap and after the STL template, remember to look oh ~)

template <class T>
class MaxHeap{
private:
	T* a;
	int sz,maxsize;
public: 
	MaxHeap(T* array,int n,int maxn):sz(n),maxsize(maxn){
		a=new T[maxn];
		for(int i=0;i<n;i++) a[i]=array[i];
	}
	void SiftUp(int pos);//向上调整 
	void SiftDown(int left);//向下调整 
	void BuildHeap();
	bool Insert(const T& x);//向堆中插入新元素x
	bool Remove(int pos, T& x);//删除给定下标的元素
	T* getMaxHeap(){return a;}
	bool isLeaf(int pos)const{if(2*pos+1>=sz)return 1;return 0;}
	int size(){return sz;}
	T& RemoveMax(){
		T& x;
		Remove(0,x);
		return x;
	}
	int leftchild(int pos)const{
		int l=2*pos+1;
		if(l<sz) cout<<pos<<"号结点的左孩子为"<<l<<"号"<<endl;
		else cout<<pos<<"号结点是叶子结点,没有左孩子"<<endl;
		return l;
	}
	int rightchild(int pos)const{
		int r=2*pos+2;
		if(r<sz) cout<<pos<<"号结点的右孩子为"<<r<<"号"<<endl;
		else cout<<pos<<"号结点没有右孩子"<<endl;
		return r;
	}
	int parent(int pos)const{
		int fa=0;
		if(pos>0){
			fa=(pos-1)/2;
			cout<<pos<<"号结点的父结点为"<<fa<<"号"<<endl;
		}
		else cout<<pos<<"号位置无父结点"<<endl;
		return fa;
	}
};
template<class T>
void MaxHeap<T>::SiftDown(int i){
	int son=2*i+1;
	T tmp=a[i];
	while(son<sz){
		if((son<sz-1)&&(a[son]<a[son+1])) son++;//有右儿子且右儿子比左儿子大,根要和右儿子比大小,j指向右儿子 
		if (tmp<a[son]){//教材这个连续交换的处理就很巧妙,值得我们学习。 
			a[i]=a[son];//不去每次都交换,每次把下面的赋成上面,不着急上面赋成下面。放到最后赋一次。
			i=son;//不过自己写还是每次都swap舒服 
			son=2*son+1;
		}
		else break;
	}
	a[i]=tmp;
}
template<class T>
void MaxHeap<T>::SiftUp(int i){
	int fa=parent(i);
	T tmp=a[i];
	while(i){
		if(tmp>a[fa]){ 
			a[i]=a[fa];
			i=fa;
			fa=parent(i);
		}
		else break;
	}
	a[i]=tmp;
}
template<class T>
void MaxHeap<T>::BuildHeap(){
	for(int i=sz/2-1;i>=0;i--) SiftDown(i);
}
template<class T>
bool MaxHeap<T>::Insert(const T& x){
    if(sz==maxsize) return 0;
	a[sz]=x;
	SiftUp(sz++);
	return true;
}
template<class T>
bool MaxHeap<T>::Remove(int i,T& x){
	if(i>=sz) return 0;
	x=a[i];
	a[i]=a[sz-1];
	sz--;
	SiftDown(i);
	return 1;
}

Priority Queue: https://blog.csdn.net/weixin_44077863/article/details/102020177

stl_heap heap:

//STL的堆和手写的效果是一样的,默认大根堆,总共四个操作
make_heap(a,a+n,cmp);//cmp默认为less<T>(),a可以用数组,或者array、deque、vector这样的容器
pop_heap(a,a+n,cmp);
push_heap(a,a+n,cmp);
sort_heap(a,a+n,cmp);//没鸟用,又排成普通的单调序列了,和sort一样
//要注意pop_heap和push_heap具体的用法
//pop_heap()就是手写的根挪到最后然后做调整的整个过程
//push_heap()本质是把最后一个siftup,需要你手动插到最后 a[n]=xxx;然后调用 push_heap

 

Published 49 original articles · won praise 0 · Views 1708

Guess you like

Origin blog.csdn.net/weixin_44077863/article/details/103114931