Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

E 5. A full binary tree is a binary tree in which each node is either a leaf (i.

ID: 3167565 • Letter: E

Question

E 5. A full binary tree is a binary tree in which each node is either a leaf (i.e., has no children) or has exactly two children, called its "left" and "right" children. Consider the full binary tree below: Root IR. Every path from the root to a leaf involves at most L = 2 left children, and at most R right child. Notici we added two children to auy of the leaves, we w iuoduce a root-to-leaf path involving more than two left children and/or more than one right child, so this example is maximal for L-2, R = 1 . Moreover, no other tree does better: the maximum number of leaves under these constrains is three. More generally, given some positive integers L, R, we ask: what is the maximum number of leaves a full binary tree can have, such that every root-to-leaf path contains at most L left children and at most R right children? Design a dynamic-programming algorithm that solves this problem in O(LR) time Bonus challenge: design an algorithm that solves the problem in only O(1) time!

Explanation / Answer

// C program to check whether a given Binary Tree is full or not

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

/* Tree node structure */

struct Node

{

    int key;

    struct Node *left, *right;

};

/* Helper function that allocates a new node with the

   given key and NULL left and right pointer. */

struct Node *newNode(char k)

{

    struct Node *node = (struct Node*)malloc(sizeof(struct Node));

    node->key = k;

    node->right = node->left = NULL;

    return node;

}

/* This function tests if a binary tree is a full binary tree. */

bool isFullTree (struct Node* root)

{

    // If empty tree

    if (root == NULL)

        return true;

    // If leaf node

    if (root->left == NULL && root->right == NULL)

        return true;

    // If both left and right are not NULL, and left & right subtrees

    // are full

    if ((root->left) && (root->right))

        return (isFullTree(root->left) && isFullTree(root->right));

    // We reach here when none of the above if conditions work

    return false;

}

// Driver Program

int main()

{

    struct Node* root = NULL;

    root = newNode(10);

    root->left = newNode(20);

    root->right = newNode(30);

    root->left->right = newNode(40);

    root->left->left = newNode(50);

    root->right->left = newNode(60);

    root->right->right = newNode(70);

    root->left->left->left = newNode(80);

    root->left->left->right = newNode(90);

    root->left->right->left = newNode(80);

    root->left->right->right = newNode(90);

    root->right->left->left = newNode(80);

    root->right->left->right = newNode(90);

    root->right->right->left = newNode(80);

    root->right->right->right = newNode(90);

    if (isFullTree(root))

        printf("The Binary Tree is full ");

    else

        printf("The Binary Tree is not full ");

    return(0);

}

Time complexity of the above code is O(n) where n is number of nodes in given binary tree.