why do varibles not work, in this remove function.

this function is suppose to rebalance a heap after the max value has been remove from a seperate function. while I was writing it I first tried a varible for Left and Right. The program freezes when it tries to run the remove_max function. This is just something that was confusing me and I was unable to find out why it doesn't work with the varibles.

With varibles.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void MaxHeap::makeHeap_remove(int i) {
    int left = 2*i+1;
    int right = 2*i+2;

	while (isNodeValid(left)&& isNodeValid(right)){
	if(heapArray[right]>heapArray[left]){
        if(heapArray[i] < heapArray[right]){
            int temp = heapArray[i];
            heapArray[i] = heapArray[right];
            heapArray[right] = temp;

        }
        i=right;
        }
    else{
        if(heapArray[i] < heapArray[left]){
           int temp = heapArray[i];
           heapArray[i] = heapArray[left];
           heapArray[left] = temp;

        }
        i=left;
    }

	}
}


Without
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void MaxHeap::makeHeap_remove(int i) {

	while (isNodeValid(2*i+1)&& isNodeValid(2*i+2)){
	if(heapArray[2*i+2]>heapArray[2*i+1]){
        if(heapArray[i] < heapArray[2*i+2]){
            int temp = heapArray[i];
            heapArray[i] = heapArray[2*i+2];
            heapArray[2*i+2] = temp;

        }
        i=2*i+2;
        }
    else{
        if(heapArray[i] < heapArray[2*i+1]){
           int temp = heapArray[i];
           heapArray[i] = heapArray[2*i+1];
           heapArray[2*i+1] = temp;

        }
        i=2*i+1;
    }

	}
}


This function calls the above funtion.
1
2
3
4
5
6
7
int MaxHeap::removeMax() {
	int temp = heapArray[0];
	heapArray[0] = heapArray[heapSize-1];
	heapArray[--heapSize] = temp;
        makeHeap_remove(0);
    return temp;
}
left and right are keywords that may be confusing your compiler...try using a different name or capitalizing the first letter.
originally I used LC for left child and rc for right child, just changed it to left and right for this posting. I'm using code blocks IDE if that matters.
left and right are keywords that may be confusing your compiler


whaaaaaaaat?


Anyway, a freezing program is a sign of an infinite loop. Check your isNodeValid function and make sure it is working properly.
I have it working as long as I don't use the variables and just use 2*i+1 and 2*i+2. It should work with the variables as define just doesn't was wondering why they wouldn't function properly.
Topic archived. No new replies allowed.