Question

In: Computer Science

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.

Input

There 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 5

5 4 3 2 1

1 9 2 13 10 5 3 15 14 11 21 7 6 4 17 16 24 28

25 32 55 97 43 62 30 2 27 59 15 81 22 97 38 42

Output

The only output is a determination whether each provided vector of integers is a min–heap or not. If it is not amin–heap, report the largest index where themin–heap order property is violated.

Sample output, corresponding to the sample input above, is provided below.

Vector is a min-heap.

Min-heap order property violated at index 1.

Vector is a min-heap.

Min-heap order property violated at index 5.

Solutions

Expert Solution

Min-Heap:

A Min Heap is a Complete Binary Tree, In which the value of the parent node is smaller than both of its children's value. And due to its property of being complete, it can be represented using an array representation.

For element at index i, its children in the array can be found at (2*i+1) (left child) and (2*i+2) (right child).

Now in order to check if a vector follows the min-heap property or not, we will have to iterate through every value in vector and check if it is less than its children.

C++ Code:

#include
using namespace std;

// this funtion returns an index i (0<=i // And returns v.size() if vector is a heap
int is_vector_heap(vector v)
{
int n = v.size();
//iterate through all values in vector
for (int i=0; i {
//find left child index
       int left = 2*i+1;
       int right = 2*i+2;
      
       // we need to check the existance of children because for leaf nodes there will be no children
       // if left child exists and its value is less than parent then return index i
if(left return i;
  
// if right child exists and its value is less than parent then return index i
if (right < n && v[left] < v[i])
return i;
}
return n;
}
int main()
{
   string line;
  
   //read input line by line until a empty line is entered
   while (std::getline(std::cin, line) && !line.empty())
   {
       //parse the line to build a vector of integers
       istringstream iss;
       iss.str(line);
       vector v;
       while (!iss.eof())
       {
       int num;
       iss >> num;
       v.push_back(num);
       }
       //check if v is a min-heap
       int i=is_vector_heap(v);
       if(i==v.size())
   {
       cout<<"Vector is a min-heap.\n";
       }
       else
       {
           cout<<"Min-heap order property violated at index "<        }
   }
}

Sample Output:


Related Solutions

How to write Prim's Algorithm with min-Heap and adjacency Lists?
How to write Prim's Algorithm with min-Heap and adjacency Lists?
Let's suppose that you have a binary min-heap with S elements. Please write an an algorithm...
Let's suppose that you have a binary min-heap with S elements. Please write an an algorithm that will find all the elements that are in the heap that are smaller than a given value X. Please make sure that the time complexity of the algorithm that you propose has to be O(K), where is the number of elements smaller than X that is in the heap. Please type answer if you can
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.
C++ Write a function that determines if there are three of the same value in a...
C++ Write a function that determines if there are three of the same value in a row in an array. The function returns a bool and is passed an array of ints and it's size as an int. Write a function that determines if there are three of the same line in a row in a file. The function returns a bool and is passed a string which contains the filename. Write a function that determines if there are two...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector...
How to write a max-heap implementation of a templated priority queue class using the STL std::vector class to hold the data array
Java Question 5: Count elements in the heap Write a function that returns the number of...
Java Question 5: Count elements in the heap Write a function that returns the number of elements in a min heap strictly less than a given number. Method signature: public static int elemNumHeap(PriorityQueue minHeap, int val) Please also include testers.
In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a,...
Hi i have this for a practical tomorrow (C++): a) Write a function vector merge(vector a, vector b) that merges two vectors, alternating elements from both vectors. If one vector is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer vector. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 Then merge returns the vector 1 9 4 7 9 4...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType...
Write a C++ PROGRAM: Add the function min as an abstract function to the class arrayListType to return the smallest element of the list. Also, write the definition of the function min in the class unorderedArrayListType and write a program to test this function.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT