Question

Task: A ternary heap is like a binary heap, except instead of each node having a max of two children, each node has a max of

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Source Code in C++:

#include <iostream>
#include <iterator>
using namespace std;

bool isValidHeap(int arr[],int n) //function to check if an array is a valid terneary min heap
{
for(int i=0;i<n;i++) //iterating through all the nodes
{
for(int j=3*i+1;j<=3*i+3 && j<n;j++) //iterating through the child nodes for every node, if available
{
if(arr[i]>arr[j]) //if a parent node is higher than child node, it is not valid
return false;
}
}
return true; //if program reaches this point, heap was valid min heap
}

int main()
{
//testing the function
int arr1[]={4,7,10,6,12,9};
cout << "Array1 is " << (isValidHeap(arr1,6)?"":"not") << "a valid heap" << endl;
int arr2[]={5,10,9,4,12};
cout << "Array2 is " << (isValidHeap(arr2,5)?"":"not ") << "a valid heap" << endl;
return 0;
}

Output:

CAUsers Debjit G Documents heap.exe Array2 is not a valid heap Pro cess returned 0 <0x0> execution tine 0.014 s Press any ke

Add a comment
Know the answer?
Add Answer to:
Task: A ternary heap is like a binary heap, except instead of each node having a max of two children, each node has a max of three children. Given a ternary min heap, return true if it is a valid hea...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT