我正在尝试将键值插入堆中。我正在使用TestUnit。cpp查找错误。我发现以下错误:
断言失败。
应为:<[(10100),(7,70),(6,60),(5,50),(2,20),(1,10),(3,30),(4,40)]gt;
实际值:<[(7,70),(5,50),(6,60),(4,40),(2,20),(1,10),(3,30),(10100)]gt;
断言失败。
应为:<[(10100),(4,40),(5,50),(1,10),(2,20),(3,30)]>
实际值:<[(5,50),(4,40),(3,30),(1,10),(2,20),(10100)]>
断言失败。
应为:<[(9,90),(7,70),(8,80),(6,60),(2,20),(1,10),(3,30),(5,50)]gt;
实际值:<[(9,90),(7,70),(8,80),(5,50),(2,20),(1,10),(3,30),(6,60)]gt;
断言失败。
应为:<[(6,60),(5,50),(4,40),(1,10),(2,20),(3,30)]>
实际值:<[(6,60),(5,50),(3,30),(1,10),(2,20),(4,40)]>
我的插入功能是:
void insert(KeyValueType const& keyValue)
{
size_t asize = size();
if (asize == 0)
{
table.push_back(keyValue);
}
else
{
table.push_back(keyValue);
for (int i = asize / 2 - 1; i >= 0; i--)
{
heapify(i);
}
}
}
我的heapify函数是:
void heapify(size_t index)
{
auto previous_index = index;
do
{
previous_index = index;
auto largest = getLargestFromParentAndHisChildren(index);
if (index != largest)
{
std::swap(table[index], table[largest]);
index = largest;
}
} while (index != previous_index);
}