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

write am algorthn to add number until you pick the number zero

write an algorithom to add number until you pick the number zero

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.