I am having problem on removing root, moving the lastnode to the new root and applying the downheap.
Here is my code snippet
#include <iostream>
#include "heap.h"
heap::heap()
{
data=new myvector<heapData> (10);
size=0;
heapData item;
item.key=0;
item.data=0;
insertItem(item);
}
heap::~heap()
{
delete data;
};
void heap::insertItem(heapData item)
{
data->insertAtRank(size,item);
size++;
heapData parentData; //upheap
int currIndex=size-1;
int parentIndex=parent(currIndex);
while (parentIndex>0)
{
//int parentIndex=parent(size-1);
parentData=data->elementAtRank(parentIndex);
if(parentData.key>item.key)
{
data->replaceAtRank(parentIndex,item);
data->replaceAtRank(size-1,parentData);
}
parentIndex=parent(parentIndex);
}
}
heapData heap::removeRoot()
{
heapData root=data->elementAtRank(1);
//remove root
//move the lastnode to the new root
//apply downheap