Question

In: Computer Science

Consider the following struct that represents a node within a binary tree: struct Node { int...

Consider the following struct that represents a node within a binary tree:

struct Node {
int data; // Data of interest

Node *left // Link to left subtree (nullptr if none)
Node *right ; // Link to right subtree (nullptr if none)
};

Complete the following function that computes the number of elements in a binary tree:

// Counts the number of elements in the binary tree to which t points.
// Returns the number of elements.

int size(Node *t) {





}

GIven root, a pointer to the root of a binary tree, size(root) evaluates to the number of nodes in the tree.

Solutions

Expert Solution

The required code and corresponding output are as follows:

#include <stdio.h>
#include<math.h>
 
struct Node {
    int data;
    struct Node *left;
    struct Node *right;
};
 
struct Node* getNewNode(int data) {
  /* dynamically allocate memory for a new node */
  struct Node* newNode = 
        (struct Node*)malloc(sizeof(struct Node));
  
  /* include data in new Node */
  newNode->data = data;
  newNode->left = NULL;
  newNode->right = NULL;
   
  return newNode;
}
// Creating a binary tree to check the function size()
struct Node* generateBTree(){
    // Root Node
    struct Node* root =  getNewNode(1);
    // Level 2 nodes 
    root->left = getNewNode(2);
    root->right = getNewNode(3);
    // Level 3 nodes
    root->left->left = getNewNode(4);
    root->left->right = getNewNode(5);
    root->right->left = getNewNode(6);
    root->right->right = getNewNode(7);
     
    return root;
 
}
/*
Returns total number of nodes(size) in a bianry tree
size(root) = size(left-subTree) + 1 
                     + size(right-subTree);
*/
//Required Function
int size(struct Node *t){
    if(t == NULL)
        return 0;
    return size(t->left) + 1 + size(t->right);
}
 
int main() {
    struct Node *root = generateBTree();    
     
    printf("Size of Tree = %d", size(root));
     
    getchar();
    return 0; 
}

Output:


Related Solutions

Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree...
Consider the following type of binary trees: data Tree a = Leaf a | Node (Tree a) (Tree a) A tree is balanced if the number of leaves in the left and right subtree of every node differ by at most one. Write a Haskell function balanced that returns whether a tree is balanced or not. balanced :: Tree a -> Bool
C++ language. struct Node {    int data;    Node *next; } Write a function to...
C++ language. struct Node {    int data;    Node *next; } Write a function to concatenate two linked lists. Given lists A* = (4, 6) and B* = (3, 7, 12), after return from Concatenate_Lists(Node A*, Node B*) the list A should be changed to be A = (4, 6, 3, 7, 12). Your function should not change B and should not directly link nodes from A to B (i.e. the nodes inserted into A should be copies of...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template (include screenshots please of output)
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary...
C++ tree program (please do NOT use struct Node, use classes) Program 1 Implement a Binary tree using an array Program 2 Implement a tree using linked list - pointer Binary Tree Program 3 - Convert program 1 to a template
Please do this code with python. Thank you! struct Node {     int data;     struct...
Please do this code with python. Thank you! struct Node {     int data;     struct Node* left;     struct Node* right; }; // Node creation struct Node* newNode(int data) {     struct Node* nn         = new Node;     nn->data = data;     nn->left = NULL;     nn->right = NULL;     return nn; } // Function to insert data in BST struct Node* insert(struct Node* root, int data) {   if (root == NULL)         return newNode(data);     else {...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function...
Assume that struct Node { int item; Node* link; }; typedef Node* NodePtr; 1. Write function void list_head_insert(NodePtr& head, int entry); The function should insert a new Node, in which entry is the value of attribute item, in front of the linked list that is pointed by head. 2. Write function void list_head_remove(NodePtr& head); The function will remove the first node from the linked list that is pointed by head. 3. Write function NodePtr list_search(NodePtr head, int target); The function...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT