i need to write the code for the void insert into a heap. I have an error that fail compilation.
here is my code, can someone... help me out please
what should do
void insert (const KEY &key, const VALUE &value)
I wrote something like this:
here is my code, can someone... help me out please
what should do
void insert (const KEY &key, const VALUE &value)
I wrote something like this:
Code:
void insert (const KEY &key, const VALUE &value)
************************************************************
#pragma once
#include "UnderFlowException.h"
#include "overFlowException.h"
#include "Node.h"
template <typename KEY, typename VALUE>
class Heap
{
private:
int maximumSize;
int lastUsedIndex;
Node<KEY,VALUE> *array;
public:
Heap (int maximumSize): maximumSize(maximumSize), lastUsedIndex(-1)
{
array = new Node<KEY,VALUE>[maximumSize];
}
//************************************************************
void insert (const KEY &key, const VALUE &value)
// insert code here
{
for (VALUE i = 0; i <maximumSize; i++)
KEY current = key;
VALUE currentIndex = *array;
/*Node = new Node<KEY,VALUE>;
heap.push_back(unit);*/
//reheapUp(maximumSize);
}
//*****************************************************
VALUE remove ( )
{
VALUE result;
if (!isEmpty())
{
result = array[0].value;
array[0] = array[lastUsedIndex--];
reheapDown();
}
else
throw UnderFlowException();
return result;
}
bool isEmpty()
{
return getSize() == 0;
}
bool isFull()
{
return getSize() == getMaximumSize();
}
int getSize()
{
return lastUsedIndex + 1;
}
int getMaximumSize()
{
return maximumSize;
}
private:
static int parentIndexOf(int index)
{
return (index - 1) / 2;
}
static int leftChildOf(int index)
{
return index * 2 + 1;
}
static int rightChildOf(int index)
{
return index * 2 + 2;
}
int largestChildOf (int index)
{
int result;
int rightChildIndex = rightChildOf(index);
int leftChildIndex = leftChildOf(index);
if (rightChildIndex <= lastUsedIndex)
{
if (array[rightChildIndex].key > array[leftChildIndex].key)
result = rightChildIndex;
else
result = leftChildIndex;
}
else if (leftChildIndex <= lastUsedIndex)
result = leftChildIndex;
else
result = -1;
return result;
}
void swap (int i, int j)
{
Node<KEY,VALUE> temp = array[i];
array[i] = array[j];
array[j] = temp;
}
//*********************************************************
void reheapUp () // insert code here
{
int parent;
Node temp;
if(bottom > root)
{
parent = (bottom - 1) / 2;
if(array[parent].getKey() < array[bottom].getKey())
{
temp = array[parent];
array[parent] = array[bottom];
array[bottom] = temp;
reheapUp(root, parent);
}
}
//********************************************************************
}
void reheapDown ()
{
int current = 0;
int largestChildIndex = largestChildOf(current);
while (largestChildIndex > -1 && array[largestChildIndex].key > array[current].key)
{
swap(largestChildIndex,current);
current = largestChildIndex;
largestChildIndex = largestChildOf(current);
}
}
};
Comment