0

This is one of my first posts on here so please bear with me. I am working on an assignment and have looked at various sources to create and solve this problem.

Prompt for problem I have written the max_heapify, print, build_heap, and delete_root function but the only thing I am getting on output is exited, segmentation fault I have ran similar code through the same compiler and not had any issue.

I realize my code is not perfectly indented but if anyone has any suggestions on how to make this run it would be greatly appreciated.

#include <iostream>
#include <bits/stdc++.h>
#include <math.h>
using namespace std;


void max_heapify(int arr[], int n, int i){

   int max_val = i;
   int left = 2*i +1;
   int right = 2*i +2;

      // if left child is larger than root 
      // set  root equal to left child
 if ( left < n && arr[left] > arr[max_val] )
     max_val = left;

     // if right child is larger than largest 
 if (right < n && arr[right] > max_val)
     max_val = right;

    // if largest is not root
 if (max_val != i)
 {
          //swap values
      int temp = arr[i];
      arr[i] = arr[max_val];
      arr[max_val] = temp;

      max_heapify(arr,n,i);

}

}

  void buildh(int arr[],int n)
  {
     //index of last non leaf node
    int indx = (n/2) -1;

    for (int i = indx; i >= 0; i--)
    {
      max_heapify(arr,n,i);
    }
   }

 void printh(int arr[], int n) 
    { 
      cout << "Heap is:\n"; 

       for (int i = 0; i < n; ++i) 
            cout << arr[i] << " "; 
            cout << "\n"; 
    } 

  void delete_root(int arr[], int n)
  {
  int last = arr[n-1];
  arr[0] = last;
  n = n-1;
  max_heapify(arr,n,0);
  }


    int main() {

     int arr[] = {28,12,17,5,7,22,13,12,4,11,16}; // create an array of values
     int n = sizeof(arr) / sizeof(arr[0]); 

     buildh(arr,n);

     printh(arr,n);

     delete_root(arr,n);

     return 0;


     }
Morgan
  • 9
  • 2
  • First off, using #include is a bad habit. If you are going to use it, though, then you don't need to add any other #include statements. In other words, you can get rid of #include and #include in your program. – Telescope Jun 25 '20 at 02:13
  • I get a stack overflow after `max_heapify` calls itself recursively 3802 times. – Retired Ninja Jun 25 '20 at 02:16
  • Please see https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. Now is a good time to learn how to debug your own code. – eesiraed Jun 25 '20 at 05:30
  • Whene your code return from `max_heapify` – yaodav Jun 28 '20 at 03:38

2 Answers2

1

The segfault occurring due to max_heapify() keep on calling itself recursively and not hitting the terminating condition.

Two problems in your code:

Problem 1:

In max_heapify(), in this statement

 if (right < n && arr[right] > max_val)

it should be arr[max_val]. Correct statement will be

 if (right < n && arr[right] > arr[max_val])

Problem 2:

In every call to max_heapify() you are heapifying element at ith location

      max_heapify(arr,n,i);
                        ^^

Instead, you should pass the index max_val once it identified the maximum between left and right child and swapped the a[i] and a[max_val] values

      max_heapify(arr,n,max_val);

With this the recursive call will heapify the array element at max_val location.

H.S.
  • 11,654
  • 2
  • 15
  • 32
0

I think the issue occurs in this function:

void max_heapify(int arr[], int n, int i){
    int max_val = i;
    int left = 2*i +1;
    int right = 2*i +2;

    // if left child is larger than root, set root equal to left child
    if ( left < n && arr[left] > arr[max_val] )
        max_val = left;

    // if right child is larger than largest 
    if (right < n && arr[right] > max_val)
        max_val = right;

    // if largest is not root
    if (max_val != i){
        //swap values
        int temp = arr[i];
        arr[i] = arr[max_val];
        arr[max_val] = temp;

        max_heapify(arr,n,i);
    }
}

At the very end, you call max_heapify() within itself. The problem with this code is that there is no terminating condition for the recursion; in other words, there is nothing to stop it from recursing on to infinity. As @Retired Ninja commented, this procedure is likely to be the cause of the Segmentation Fault because it exceeds the maximum allowed recursion depth.

To fix this, you need to think about ways to reduce the recursion depth of max_heapify() (Do you really need to call it recursively?).

Telescope
  • 2,068
  • 1
  • 5
  • 22