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;
}