0

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
2
Contributors
2
Replies
20
Views
4 Years
Discussion Span
Last Post by buterous
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.