Question

Given the declaration: struct TreeNode {     int data;     TreeNode...

Given the declaration:

struct TreeNode {
    int data;
    TreeNode* left;
    TreeNode* right;
};

Write a function to test and see if a given complete binary tree is a (max) heap:

bool isAHeap(TreeNode *root)

On this, and on any subsequent questions where you are asked to give code, please use the Formatted paragraph style rather than Normal.

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

C program:

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

// node
struct TreeNode
{
    int key;
    struct TreeNode *left;
    struct TreeNode *right;
};

/* Helper function that allocates a new TreeNode */
struct TreeNode *newTreeNode(int k)
{
    struct TreeNode *TreeNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    TreeNode->key = k;
    TreeNode->right = TreeNode->left = NULL;
    return TreeNode;
}

/* This function counts the number of TreeNodes in a binary tree */
unsigned int countTreeNodes(struct TreeNode* root)
{
    if (root == NULL)
        return (0);
    return (1 + countTreeNodes(root->left) + countTreeNodes(root->right));
}

/* This function checks if the binary tree is complete or not */
bool isCompleteUtil (struct TreeNode* root, unsigned int index,
                     unsigned int number_TreeNodes)
{
    // An empty tree is complete
    if (root == NULL)
        return (true);

    // If index assigned to current TreeNode is more than
    // number of TreeNodes in tree, then tree is not complete
    if (index >= number_TreeNodes)
        return (false);

    // Recur for left and right subtrees
    return (isCompleteUtil(root->left, 2*index + 1, number_TreeNodes) &&
            isCompleteUtil(root->right, 2*index + 2, number_TreeNodes));
}

// This Function checks the heap property in the tree.
bool isHeapUtil(struct TreeNode* root)
{
    //  Base case : single TreeNode satisfies property
    if (root->left == NULL && root->right == NULL)
        return (true);

    //  TreeNode will be in second last level
    if (root->right == NULL)
    {
        //  check heap property at TreeNode
        //  No recursive call , because no need to check last level
        return (root->key >= root->left->key);
    }
    else
    {
        //  Check heap property at TreeNode and
        //  Recursive check heap property at left and right subtree
        if (root->key >= root->left->key &&
            root->key >= root->right->key)
            return ((isHeapUtil(root->left)) &&
                    (isHeapUtil(root->right)));
        else
            return (false);
    }
}

//  Function to check binary tree is a Heap or Not.
bool isAHeap(struct TreeNode* root)
{
    // These two are used in isCompleteUtil()
    unsigned int TreeNode_count = countTreeNodes(root);
    unsigned int index = 0;

    if (isCompleteUtil(root, index, TreeNode_count) && isHeapUtil(root))
        return true;
    return false;
}

// Driver program
int main()
{
    struct TreeNode* root = NULL;
    root = newTreeNode(10);
    root->left = newTreeNode(9);
    root->right = newTreeNode(8);
    root->left->left = newTreeNode(7);
    root->left->right = newTreeNode(6);
    root->right->left = newTreeNode(5);
    root->right->right = newTreeNode(4);
    root->left->left->left = newTreeNode(3);
    root->left->left->right = newTreeNode(2);
    root->left->right->left = newTreeNode(1);

    if (isAHeap(root))
        printf("Given binary tree is a Max Heap\n");
    else
        printf("Given binary tree is not a Max Heap\n");

    return 0;
}

Screen shot of the program and sample output:

Execute >Share main.c STDIN .li Result #include<stdio.h> #include<stdlib.h> #includecstdbool . h> $gcc -o main *.c $main Give

Add a comment
Know the answer?
Add Answer to:
Given the declaration: struct TreeNode {     int data;     TreeNode...
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