Search code examples
c++arraysmin-heap

Confused with Two different implementation of Heap


Function 1

void min_heapify(int arr[],int n, int i){
    int j, temp;
    temp = arr[i];
    j = 2 * i;

    while (j <= n)
    {
        if (j < n && arr[j+1] < arr[j])
            j = j + 1;
        if (temp < arr[j])
            break;
        else if (temp >= arr[j])
        {
            arr[j/2] = arr[j];
            j = 2 * j;
        }
    }

    arr[j/2] = temp;
}

Function 2

void max_heapify(int arr[], int n, int i)    
{
    int largest = i;  // Initialize largest as root
    int l = 2*i + 1;  // left = 2*i + 1
    int r = 2*i + 2;  // right = 2*i + 2

    // If left child is larger than root
    if (l < n && arr[l] < arr[largest])
        largest = l;

    // If right child is larger than largest so far
    if (r < n && arr[r] < arr[largest])
        largest = r;

    // If largest is not root
    if (largest != i)
    {
        swap(arr[i], arr[largest]);

        // Recursively heapify the affected sub-tree
        heapify(arr, n, largest);
    }
}

Problem Details

Here the heapification work the same way to make a min_heap but the problem is, I used heap in this below problem to solve it but unfortunately function 2 which I implemented by watching MIT lecture didn't work for this problem, after looking some time in the web I found the 1st function which worked seamlessly for this problem. I'm just confused are not they the same function? ------

Problem

Yup!! The problem name reflects your task; just add a set of numbers. But you may feel yourselves condescended, to write a C/C++ program just to add a set of numbers. Such a problem will simply question your erudition. So, let’s add some flavor of ingenuity to it.

Addition operation requires cost now, and the cost is the summation of those two to be added. So, to add 1 and 10, you need a cost of 11. If you want to add 1, 2 and 3. There are several ways –

1 + 2 = 3, cost = 3
1 + 3 = 4, cost = 4
2 + 3 = 5, cost = 5
3 + 3 = 6, cost = 6
2 + 4 = 6, cost = 6
1 + 5 = 6, cost = 6
Total = 9
Total = 10
Total = 11

I hope you have understood already your mission, to add a set of integers so that the cost is minimal.

Input

Each test case will start with a positive number, N (2 ≤ N ≤ 5000) followed by N positive integers (all are less than 100000). Input is terminated by a case where the value of N is zero. This case should not be processed.

Output

For each case print the minimum total cost of addition in a single line.

SampleInput

3    
1 2 3    
4    
1 2 3 4    
0    

SampleOutput

9
19

Solution

  • Ok I find out the solution there was a mistake in the condition check, in the if condition where we checking that if (left <= n) this was previously (left < n) this why it was not working for that problem. ok thank you.

    void min_heapify(int arr[],int n, int i){    
        int lowest = i; // Initialize lowest as root
        int left = 2*i ;
        int right = 2*i + 1;
    
    
    
     // If child is lower than root
        if(left <= n && arr[left] < arr[lowest]){
            lowest = left;
        }
    
        // If right child is lower than lowest
        if(right <= n && arr[right] < arr[lowest]){
            lowest = right;
        }
        // If lowest is not root
        if(lowest != i){ // also break condition
            swap(arr[i], arr[lowest]);
    
            //Recursively heapify
            min_heapify(arr, n, lowest);
    
        }