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

Need to write the following functions in table.cpp, and add function prototypes

ID: 2246512 • Letter: N

Question

Need to write the following functions in table.cpp, and add function prototypes for them to table.h and invoke the functions in main.cpp.

* int sumOfNodes(node * root)

  Recursively calculate the sum of all the nodes in the tree.

* void copyLeaf(node * src, node *& dest)

  Recursively copy all the leaves from the source tree to the destination tree. The destination tree must be a binary search
tree.

--------------------------------------------------------------------------------------------

main.cpp

#include "table.h"

#include <iostream>

using namespace std;

int main()
{
node * root = NULL;
build(root);
display(root);

/* PLACE YOUR FUNCTION CALL HERE */

display(root);
destroy(root);
return 0;
}

-----------------------------------------------------------------

table.h

#ifndef TABLE_H
#define TABLE_H
//table.h
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

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

void build(node * & root); //supplied
void display(node * root); //supplied
void destroy(node * & root); //supplied

/* ************** PLACE YOUR PROTOTYPE HERE ***************** */

#endif

------------------------------------------------------------

table.cpp

#include "table.h"

//Please put the impelementation of the required functions here

----------------------------------------------------------------------------------------

Please make sure the functions are done recursively, and are done in C++

Explanation / Answer

table.h

#ifndef TABLE_H
#define TABLE_H
//table.h
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

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

void build(node * & root); //supplied
void display(node * root); //supplied
void destroy(node * & root); //supplied
* int sumOfNodes(node * root);
* void copyLeaf(node * src, node *& dest);

#endif

main.cpp

#include "table.h"

#include <iostream>

using namespace std;

int main()
{
    node * root = NULL;
    build(root);
    display(root);
    sumOfNodes(root);
    copyLeaf(src,dest);
  
    display(root);
    destroy(root);
    return 0;
}

table.cpp

#include "table.h"
int IsBst(node *);
* int sumOfNodes(node * root)
{
   if(root==NULL)
   return 0;
   int sum;
   sum=root->data + sumOfNodes(root->left) + sumOfNodes(root->right);   //we have to add (root data + left sub tree data + right sub tree data)
   return sum;
}
* void copyLeaf(node * src, node *& dest)
{
   int x=IsBst(dest);       //to check whether Destination tree is a BST or Not
   if(x)
   {
       if(src==NULL)
       {
           dest=NULL;
           return dest;
       }
       if(src->left==NULL && src->right==NULL)
       {
           dest=new node;
           dest->data=src->data;
           dest->left=dest->right=NULL;
       }
       return copyLeaf(src->left,dest->left)+copyLeaf(src->right,dest->right) ;
   }
   else
   cout<<"Destination tree is not a Binary Search tree";
  
}
int IsBst(node *dest)
{
   if(dest==NULL)
   return 1;
   if (dest->left != NULL && dest->left->data > dest->data)
    return 0;
   if (dest->right != NULL && dest->right->data < dest->data)
    return 0;
   if (!isBst(dest->left) || !isBst(dest->right))
    return 0;
   return 1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote