0
下面我對Min Heap有一個標準的插入和刪除函數,我需要做的是在T.num比較發生相等時爲這兩個函數添加一個特例,然後我需要比較T.首先彈出較低Ascii值的字母。沒有評論是標準插入和刪除,添加評論部分將是我嘗試添加新功能,對於我來說,我不明白爲什麼它不起作用。如果主要比較恰好相等,如何修改Min Heap插入和刪除功能以接受第二個比較?
void MinHeap<T>::insert(T& e)
{
int CurrNode = ++HeapSize;
while(CurrNode != 1 && heap[CurrNode/2].num >= e.num)
{
/*
if(heap[CurrNode/2].num == e.num)
if(heap[CurrNode/2].letter <= e.letter)
break;
*/
heap[CurrNode] = heap[CurrNode/2];
CurrNode /= 2;
}
heap[CurrNode] = e;
}
void MinHeap<T>::delet()
{
T LastNode = heap[HeapSize--];
int CurrNode = 1;
int child = 2;
while(child <= HeapSize)
{
if(child < HeapSize && heap[child].num >= heap[child+1].num)
{
/*
if(heap[child].num == heap[child+1].num)
if(heap[child].letter <= heap[child+1].letter)
child--;
*/
child++;
}
if(LastNode.num <= heap[child].num)
{
/*
if (LastNode.num == heap[child].num)
{
if (LastNode.letter <= heap[child].letter)
break;
}
else
*/
break;
}
heap[CurrNode] = heap[child];
CurrNode = child;
child *= 2;
}
heap[CurrNode] = LastNode;
}