zgbl
发表于 2009-2-8 22:22:09
bosscome 1
MinHeap::MinHeap(const int n)
{
CurrentSize = 0;
MaxSize = n;
heapArray = new junction;
}
bool MinHeap::empty()
{
if (CurrentSize == 0)
return true;
else
return false;
}
void MinHeap::swap(int x, int y)
{
junction temp = heapArray;
heapArray = heapArray;
heapArray = temp;
}
int MinHeap::parent(int pos) const
{
return (pos-1)/2;
}
bool MinHeap::Insert(const junction& newNode)
{
if (CurrentSize == MaxSize)
return false;
heapArray = newNode;
SiftUp(CurrentSize);
CurrentSize ++;
return true;
}
void MinHeap::SiftUp(int position)
{
int temppos = position;
junction temp = heapArray;
while((temppos > 0) && (heapArray.length > temp.length))
{
heapArray = heapArray;
temppos = parent(temppos);
}
heapArray = temp;
}
junction& MinHeap::RemoveMin()
{
if (CurrentSize == 0)
{
cout<<"can't delete";
exit(1);
}
else
{
swap(0, --CurrentSize);
if(CurrentSize > 1)
SiftDown(0);
return heapArray;
}
}
void MinHeap::SiftDown(int left)
{
int i = left;
int j = 2 * left + 1;
junction temp = heapArray;
while (j < CurrentSize)
{
if ((j < CurrentSize-1) && (heapArray.length > heapArray.length))
j++;
if (temp.length > heapArray.length)
{
heapArray = heapArray;
i = j;
j = 2 * i + 1;
}
else
break;
}
heapArray = temp;
}
汗。。。半懂不懂的,学了c语言和数据结构入门,这刚好在我的理解能力的外面一点点,靠了
[ 本帖最后由 zgbl 于 2009-2-8 10:23 PM 编辑 ]
zgbl
发表于 2009-2-8 22:45:48
ask tie ren about 猪头
你向铁人打听有关『猪头』的消息。
铁人偷偷的指了指你的脑袋。