📄 maxheap.cpp
字号:
// MaxHeap.cpp: implementation of the CMaxHeap class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "sort.h"
#include "MaxHeap.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMaxHeap::CMaxHeap(int * h,int num,CHeapSort* owner)
{
MyOwner=owner;
Heap=h;n=num;buildHeap();
}
CMaxHeap::~CMaxHeap()
{
}
int CMaxHeap::siftdown(int pos)
{
while(!isLeaf(pos))
{
if (MyOwner->GetState2()==-1) return -1;
if (MyOwner->GetCanSleep()) MyOwner->Sleep();
int j=leftChild(pos); int rc=rightChild(pos);
if ((rc<n)&&Heap[j]<Heap[rc]) j=rc;
if (Heap[pos]>=Heap[j]) return 0;
int temp=Heap[pos];
Heap[pos]=Heap[j];
Heap[j]=temp;
pos=j;
}
return 0;
}
int CMaxHeap::removemax()
{
if (n==0) return 0;
n--;
int temp=Heap[0];
Heap[0]=Heap[n];
Heap[n]=temp;
if (n!=0)
{
if (-1==siftdown(0)) return -2;
}
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -