Question

In: Computer Science

Write a C++ program to test if a given array satisfies the constraints of min heap...

Write a C++ program to test if a given array satisfies the constraints of min heap and max heap.

      bool isMinHeap(int arr[],int size);

      bool isMaxHeap(int arr[],int size);


Also write code for this function:-

      int heapPlay(int arr[],int size);

The function returns an integer as follows:-

    1. If the array is a min heap return the minimum element

    2. If the array is a max heap return maximum element

    3. Return 0, if the array is a min heap and a max heap

    4. Return -1, if the array is neither a min heap nor a max heap

Ex.

Input - Contains size on line 1, an array on line 2.

Output - We will test your three methods

Input -

    3

1 2 3

Output -

isMinHeap ->   true

isMaxHeap ->   false

heapPlay ->    1

Sample Input 1:

1
5

Sample Output 1:

1 1 0

Sample Input 2:

3
9 8 7

Sample Output 2:

0 1 9

template:

bool isMinHeap(int arr[],int size)
{
//code here
}

bool isMaxHeap(int arr[],int size)
{
//code here
}

int heapPlay(int arr[],int size)
{
//code here
}

Solutions

Expert Solution

#include <iostream>

using namespace std;

bool isMinHeap(int arr[],int size)

{

for (int i=0; i<=(size-2)/2; i++)

{

if (2*i+1 < size && arr[2*i +1] < arr[i])

return false;

if (2*i+2 < size && arr[2*i+2] < arr[i])

return false;

}

return true;

}

bool isMaxHeap(int arr[],int size)

{

for (int i=0; i<=((size-2)/2); i++)

{

if (2*i+1 < size && arr[2*i +1] > arr[i])

return false;

if (2*i+2 < size && arr[2*i+2] > arr[i])

return false;

}

return true;

}

int heapPlay(int arr[],int size)

{

if(isMaxHeap(arr,size) && isMinHeap(arr,size))

return 0;

else if(isMaxHeap(arr,size) || isMinHeap(arr,size))

return arr[0];

else

return -1;

}

int main() {

// int arr[] = {9,8,7};

// int size = 3;

// cout<<"IsMaxHeap: "<<isMaxHeap(arr,size) <<endl;

// cout<<"IsMinHeap: "<<isMinHeap(arr,size) <<endl;

// cout<<"Heap PLay: "<<heapPlay(arr,size)<<endl<<endl;

int arr2[] = {5};

int size2 = 1;

cout<<"IsMaxHeap: "<<isMaxHeap(arr2,size2) <<endl;

cout<<"IsMinHeap: "<<isMinHeap(arr2,size2) <<endl;

cout<<"Heap PLay: "<<heapPlay(arr2,size2)<<endl<<endl;

// int arr3[] = {1, 2 ,3};

// int size3 = 3;

// cout<<"IsMaxHeap: "<<isMaxHeap(arr3,size3) <<endl;

// cout<<"IsMinHeap: "<<isMinHeap(arr3,size3) <<endl;

// cout<<"Heap PLay: "<<heapPlay(arr3,size3)<<endl<<endl;

}


===========================================================================
See Output


Thanks


Related Solutions

Write the code for binary min heap in c++ .
Write the code for binary min heap in c++ .
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer data element. Initialize the Min-heap to contain 5 nodes, with the values 8, 12, 24, 32, 42. The program should allow for the insertion and deletion of nodes while maintaining a Min-Heap. The program should allow the user to output data in Preorder, Inorder and Postorder. The program should loop with menu items for each of the above objectives and the choice to quit.
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer...
Write a complete C++ program to implements a Min-heap. Each node will contain a single integer data element. Initialize the Min-heap to contain 5 nodes, with the values 8, 12, 24, 32, 42. The program should allow for the insertion and deletion of nodes while maintaining a Min-Heap. The program should allow the user to output data in Preorder, Inorder and Postorder. The program should loop with menu items for each of the above objectives and the choice to quit...
write a function that determines if a given vector of integersis a min–heap. By default,...
write a function that determines if a given vector of integers is a min–heap. By default, a vector is asemi–heap satisfying the heap structure propertybut not the heap order property.InputThere is an unknown number of integer data that will be provided from standard input. Each line of input represents the contents of a vector. We do not know how many lines of input there are.1 2 3 4 55 4 3 2 11 9 2 13 10 5 3 15...
Given the following array, write a program in C++ to sort the array using a selection...
Given the following array, write a program in C++ to sort the array using a selection sort and display the number of scores that are less than 500 and those greater than 500. Scores[0] = 198 Scores[3] = 85 Scores[6] = 73 Scores[9] = 989 Scores[1] = 486 Scores[4] = 216 Scores[7] = 319 Scores[2] = 651 Scores[5] = 912 Scores[8] = 846
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
Suppose you have a min-heap in an array a[] where: ● The root isstored at...
Suppose you have a min-heap in an array a[] where: ● The root is stored at index 1 ● There are 15 elements (in a[1]...a[15]) ● There are no duplicates (this is not true in general for heaps; but assume it is true for this problem). Fill in the blanks in the statements below; the resulting statement must be true for any heap obeying the above properties.There are/is at least ____6_______ elements in theheap that are/is larger than a[3] ""There...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a C++ program to find K largest elements in a given array of integers. For...
Write a C++ program to find K largest elements in a given array of integers. For eeample, if K is 3, then your program should ouput the largest 3 numbers in teh array. Your program is not supposed to use any additional array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT