0% found this document useful (0 votes)
2 views2 pages

318 HeapifyFunctionFasterMethodtoCreateHeapC++

The document contains a C++ program that implements heap operations, including heapify and delete functions. It defines a swap function to interchange elements in an array and demonstrates the heapification process on two example arrays. The program also includes a print function to display the contents of the arrays before and after heapification.

Uploaded by

JATHIN JAGANNATH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

318 HeapifyFunctionFasterMethodtoCreateHeapC++

The document contains a C++ program that implements heap operations, including heapify and delete functions. It defines a swap function to interchange elements in an array and demonstrates the heapification process on two example arrays. The program also includes a print function to display the contents of the arrays before and after heapification.

Uploaded by

JATHIN JAGANNATH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;

void swap(int A[], int i, int j){


int temp = A[i];
A[i] = A[j];
A[j] = temp;
}

int Delete(int A[], int n){


int x = A[0]; // Max element
A[0] = A[n-1];

int i = 0;
int j = 2 * i + 1;

while (j < n-1){


// Compare left and right children
if (A[j] < A[j+1]){
j = j+1;
}

// Compare parent and largest child


if (A[i] < A[j]){
swap(A, i, j);
i = j;
j = 2 * i + 1;
} else {
break;
}
}
return x;
}

void Heapify(int A[], int n){


// # of leaf elements: (n+1)/2, index of last leaf element's parent = (n/2)-1
for (int i=(n/2)-1; i>=0; i--){

int j = 2 * i + 1; // Left child for current i

while(j < n-1){


// Compare left and right children of current i
if (A[j] < A[j+1]){
j = j+1;
}

// Compare parent and largest child


if (A[i] < A[j]){
swap(A, i, j);
i = j;
j = 2 * i + 1;
} else {
break;
}
}
}
}
template <class T>
void Print(T& vec, int n, string s){
cout << s << ": [" << flush;
for (int i=0; i<n; i++){
cout << vec[i] << flush;
if (i < n-1){
cout << ", " << flush;
}
}
cout << "]" << endl;
}

int main() {

int A[] = {5, 10, 30, 20, 35, 40, 15};


Print(A, sizeof(A)/sizeof(A[0]), "A");

Heapify(A, sizeof(A)/sizeof(A[0]));
Print(A, sizeof(A)/sizeof(A[0]), "Heapified A");
cout << endl;

int B[] = {5, 10, 30, 20};


Print(B, sizeof(B)/sizeof(B[0]), "B");

Heapify(B, sizeof(B)/sizeof(B[0]));
Print(B, sizeof(B)/sizeof(B[0]), "Heapified B");

return 0;
}

You might also like