ch Byron Au if he Lush my should be in a siS PI P Write the dad o the function, nodolounds, that returns the number aneda in the by Add this function to the clas BermuTuoitar Croato a program to tract this function. ) write the dea of ho wochin leaves Court, had talkees apan tierkorporate as a parameter a ports, to Jo’ rootnode of a hioy tree and returns the num be a leaves in a d Binoy ro. Add this Urciu | The He clocs hisery Trote ond create a program to the Jews (Ohion Writo a , swope ultras , JNod Suare a unction the K1 and stress of a now draw AN No uction to the harpoolipe or clus to feed ODI ate a pogrom the fun this
Expert Answer
#include<iostream>
using namespace std;
class tree
{
struct TreeNode{
int data;
struct TreeNode *left;
struct TreeNode *right;
};
//new_node and its data and left and right pointers
struct TreeNode *new_node(int item)
{
struct TreeNode *temp=new TreeNode;
// struct TreeNode *temp=(struct TreeNode*)malloc(sizeof(struct TreeNode));
temp->data=item;
temp->left=temp->right=NULL;
return temp;
}
//inserting elements into a tree
struct TreeNode* insert(struct TreeNode *temp,int key)
{
if(temp==NULL)
return new_node(key);
if(key<temp->data)
temp->left=insert(temp->left,key);
else if(key>temp->data)
temp->right=insert(temp->right,key);
return temp;
}
//leaf count
int leavescount(struct TreeNode *root)
{
if(root==NULL)
return 0;
if(root->left==NULL&&root->right==NULL)
return 1;
else
return leavescount(root->left)+leavescount(root->right);
}
//nodes count
int nodecount(struct TreeNode *root)
{
int c=1;
if(root==NULL)
return 0;
else
{
c=c+nodecount(root->left);
c=c+nodecount(root->right);
return c;
}
}
//swap sub tree function
void swapsubtree(struct TreeNode *root)
{
struct TreeNode *temp=NULL;
if(root==NULL)
return;
else
{
swapsubtree(root->left);
swapsubtree(root->right);
}
temp=root->left;
root->left=root->right;
root->right=temp;
}
//inorder function
void inorder(struct TreeNode *root)
{
if(root==NULL)
return;
else
{
inorder(root->left);
cout<<root->data<<” “;
inorder(root->right);
}
}
//main function
int main()
{
int count;
struct TreeNode *root=NULL;
//inserting into binary tree
root=insert(root,3);
insert(root,2);
insert(root,5);
insert(root,6);
insert(root,7);
//calling inorder of the tree
cout<<“nInorder, Before Swaping:n”;
inorder(root);
//after inserting nodes as tree, the tree looks like this
// 3
// 2 5
// 6
// 7
//calling the required function
//calling leavescount to count the no of leaf nodes
count=leavescount(root);
cout<<“nNumber of Leaf nodes:”<<count;
//calling nodecount function to count number of nodes
count=nodecount(root);
cout<<“nNumber of nodes:”<<count;
//calling function to swap subtree nodes
swapsubtree(root);
//calling inorder function after swapping
cout<<“nAfter swaping sub tree:n”;
inorder(root);
}
};