C++?????????????????
???????????? ???????[ 2015/10/27 10:59:44 ] ??????????????????? .NET
//C++??????????????????
#include<iostream>
using namespace std;
class BinaryHeap
{
private:
int cap; //?????е??????
int size; //??????????
int* datas; //????????
public:
explicit BinaryHeap(int cap_) :cap(cap_)?? size(0)
{
datas = new int[cap];
}
~BinaryHeap(){ delete datas; }
void Insert(int);
int DeleteMin();
};
//?????????????????±??0?????i???????i*2+1?i???????????i*2+2?i???????????i/2?i??????
void BinaryHeap::Insert(int data)
{
int i;
for (i = size; i / 2 >= 0 && datas[i / 2] > data; i /= 2) //????
{
datas[i] = datas[i / 2];
}
datas[i] = data;
size++;
}
int BinaryHeap::DeleteMin()
{
int i = 0?? child = 0;
int lastdata = datas[--size];
int mindata = datas[0];
for (i = 0; i * 2 + 1 <= size-1; i = child) //????
{
child = i * 2 + 1;
if (child < size-1 && datas[child + 1] < datas[child])
{
child++;
}
if (lastdata > datas[child])
{
datas[i] = datas[child];
}
else
{
break;
}
}
datas[i] = lastdata;
return mindata;
}
int main()
{
BinaryHeap t(400);
for (int i = 0; i < 100; i++)
t.Insert(i);
for (int i = 200; i > 100; i--)
t.Insert(i);
for (int i = 0; i < 200; i++)
cout << t.DeleteMin() << endl;
cin.get();
return 0;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11