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
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
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
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...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Write a function getStats(fname) that determines the following statistics for the input file, fname, and write...
Write a function getStats(fname) that determines the following statistics for the input file, fname, and write the results to the text file, FileStats.txt. • Number of occurrences of each day of the week in the file. • Number of lines (determined by end of line character “\n”) • Number of words • Numbers of characters (excludes spaces between words) The output from getStats() should • print the message ‘To view the results go to FileStats.txt’ • return ‘Thanks for using...
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
This C++ assignment asks to write a function that determines if a C-string begins with a...
This C++ assignment asks to write a function that determines if a C-string begins with a specified prefix. It should have the following signature: bool starts(char *str, char *prefix) It should return true if str begins with prefix, false if not. It should return false if prefix is longer than str. See the table below for some examples of what your function should return for various cases: str prefix returns airplanes air true airplanes abc false airplanes plane false airplanes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT