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.
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:

Given the declaration:struct TreeNode { int data; TreeNode* left; TreeNode* right;};Write a function to test a tree and return if every node has a 0 or 1 non-null children, i.e., no node has 2 non-null children.bool isDegenerate(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.
When debugging a crashing program for manipulating Projects, where each project contains a linked list of Staff records, you discover that two different projects have pointers to the same linked list node. Likely causes for this problem might be: Failure to provide explicit forms of the Big 3 An undefined static variable Inappropriate use of shallow copy A memory leak due to an improper destructor Failure to provide a default constructor Given the declaration: struct TreeNode { int data;...
Coding Language: C++
Function Header: vector<vector<int>>
printFromButtom(TreeNode* root) {}
Definition for a binary tree node:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
};
The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...
Java class TreeNode is defined as below: public class TreeNode { public int val; public TreeNode left, right; public TreeNode(int x) { val = x; } public TreeNode(int x, TreeNode lChild, TreeNode rChild) { val = x; left = lChild; right = rChild; } } Suppose you only have the reference of the root, please write a method that returns a deep copy of the binary tree. Your method returns the root of the deep copy. Your algorithm should...
PROMPT:
Consider a binary tree (not necessarily a binary search tree)
with node structure.
QUESTION:
Prove that findMax works by mathematical induction.
struct Node int val; struct Node * left; struct Node* right; The following function findMax returns the largest value 'val in the tree; and returns -1 if the tree is empty. You may assume that all the values 'val' in the tree are nonnegative. struct Node * findMax(struct Node root) if (rootNULL) return -1; maxval = root->val; maxval...
Public class TreeNode { TreeNode left, right; Int val; } Given a binary tree, print val in level order. Input: 1 2 3 4 5 6 7 Out: 1234567
TreeNode.java
public class TreeNode {
public int key;
public TreeNode p;
public TreeNode left;
public TreeNode right;
public TreeNode () {
p = left = right = null;
}
public TreeNode (int k) {
key = k;
p = left = right = null;
}
}
BinarySearchTree.java
public class BinarySearchTree {
public TreeNode root;
public BinarySearchTree () {
root...
The code is in JAVA
public class CheckBST {
//method to implement
public static boolean isValidBST(TreeNode root) {
}
public static void main(String[] args) {
TreeNode a = new TreeNode(1);
TreeNode b = new TreeNode(2);
TreeNode c = new TreeNode(3);
a.left = b;
a.right = c;
System.out.println(isValidBST(a));
TreeNode d = new TreeNode(2);
TreeNode e = new TreeNode(1);
TreeNode f = new TreeNode(3);
d.left = e;
d.right = f;
System.out.println(isValidBST(d));
}
}
TreeNode.java
class TreeNode {
int val;
TreeNode left;
TreeNode...
/* * struct for a single node in a binary tree. data contains
the int
* stored in this node. left and right contain pointers to the
left and
* right subtrees respectively. *
* All of the ints stored in the left subtree is smaller than
data.
* All of the ints stored in the right subtree is larger than
data.
*/
struct node {
int data;
struct node *left;
struct node *right;
};
typedef struct node node;
Write...
answer in java
Given a Binary Search Tree containing integer values, write a method to print the values in descending order. public class Treenode { int val; Treenode left; Treenode right; Treenode (int x) { val = x; } The method signature is: public void reverseSorted(Treenode root){ // your code