Segmentation fault with Vectors

Hello
I am having a segmentation fault in a binary heap that I am doing for an assignment and it is baffling me! I must admit it was quite fun the first 3 days trying to find it but I am afraid time is running out. The vector name is "element" and it is a vector of strings.
here is where I managed to narrow down where it is happening:

string Heap :: heapHelp () //helper function for the heap sort
{

//initialized to one so that the tree does not include element 0
int h = 1;

//variable to be returned
string hep = "This heap is empty";

//checks the size of the vector to make sure the function does not waste its //time
int size = element.size();

if ( size != 0 )
{

//if the vector is not empty call the heapify function
hep = heapify (h);

}

return hep;
}


//it is in the function "Heapify" that the segmentation fault is occuring

string Heap :: heapify (int a)
{
//holds the size of the vector so that the comparisons don't go out of range
int temp = element.size();

//the string to be returned
string base = " ";

//base case: if the parent node "element[a]" is larger than its Left Child //"element[a*2]" and its Right Child "element[(a*2)+1], return base
if (( element[a] > element [a * 2] ) && ( element[a] > element[ (a * 2) + 1 ] ) )
{
return base;
}


//temporary variable to store the left child

string templ = " ";

//temporary variable to store the right child

string tempr = " ";



//LEFT CHILD COMPARISON & SWITCH
//if the subscript is less than the vector size
if ((a * 2) < temp )
{


if ( element[a] < element[(a * 2)])

{
templ = element[(a * 2)];

element[(a * 2)] = element[a];

element[a] = templ;

heapify( (a * 2) );

}


}

//RIGHT CHILD COMPARISON & SWITCH
//if the subscript is less than the vector size

if (((a * 2) + 1) < temp )
{
if ( element[a] < element[(a * 2) + 1])

{
tempr = element[(a * 2) + 1];

element[(a * 2) + 1] = element[a];

element[a] = tempr;

heapify( ( a * 2 ) + 1);

}

}

return base;

}


What did I do wrong?
At this point if (( element[a] > element [a * 2] ) && ( element[a] > element[ (a * 2) + 1 ] ) ) you haven't check if all elements are within the size of the vector.
so should i encase that if statement with "if (a < temp)" condition?
Topic archived. No new replies allowed.