Please create a test program for each question , you can use your imagination to show the functions are working. All the functions and the class should be in same program
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
code:
#include <iostream>
#include <cstdlib>
using namespace std;
//using template clss
template <class Type>
struct binary_Tree_nd
{
Type info;
binary_Tree_nd<Type>* left_lnk;
binary_Tree_nd<Type>* rght_lnk;
};
template <class Type>
class binary_Tree_Type
{
public:
binary_Tree_Type();
bool isEmpty() const;
void trav() const;
int node_Count() const;
int leaves_Count() const;
void swap_sub_trees() const;
void elements_insertion(const Type& I_T);
protected:
binary_Tree_nd<Type>* root;
private:
void inorder(binary_Tree_nd<Type>* p) const;
int height(binary_Tree_nd<Type>* p) const;
int max(int x, int y) const;
int node_Count(binary_Tree_nd<Type>* p) const;
int leaves_Count(binary_Tree_nd<Type>* p) const;
void swap_sub_trees(binary_Tree_nd<Type>* p) const;
};
template <class Type>
binary_Tree_Type<Type>::binary_Tree_Type ()
{
root = NULL;
}
template <class Type>
bool binary_Tree_Type<Type>::isEmpty () const
{
return(root == NULL);
}
template <class Type>
void binary_Tree_Type<Type>::trav() const
{
inorder(root);
cout << endl;
}
template <class Type>
int binary_Tree_Type<Type>::node_Count() const
{
return node_Count(root);
}
template <class Type>
int binary_Tree_Type<Type>::leaves_Count() const
{
return leaves_Count(root);
}
template <class Type>
void binary_Tree_Type<Type>::swap_sub_trees() const
{
return swap_sub_trees(root);
}
template <class Type>
void binary_Tree_Type<Type>::elements_insertion(const Type& I_T)
{
binary_Tree_nd<Type> *elem;
binary_Tree_nd<Type> *last;
binary_Tree_nd<Type> *newNode;
newNode = new binary_Tree_nd<Type>;
newNode->info = I_T;
newNode->left_lnk = NULL;
newNode->rght_lnk = NULL;
if(root == NULL) {
root = newNode;
} else {
elem = root;
while(elem != NULL)
{
last = elem;
if(elem->info == I_T)
{
cout<<“no duplicate elements”;
return;
} else {
if(elem->info > I_T)
elem = elem->left_lnk;
else
elem = elem->rght_lnk;
}
}
if(last->info > I_T)
last->left_lnk = newNode;
else
last->rght_lnk = newNode;
}
}
template <class Type>
void binary_Tree_Type<Type>::inorder(binary_Tree_nd<Type>* p) const
{
if (p != NULL)
{
inorder (p->left_lnk);
cout<<p->info<<” “;
inorder (p->rght_lnk);
}
}
template <class Type>
int binary_Tree_Type<Type>::max(int x, int y) const
{
if (x >= y)
return x;
else
return y;
}
template <class Type>
int binary_Tree_Type<Type>::node_Count(binary_Tree_nd<Type>* p) const
{
if (p == NULL)
return 0;
else
return 1 + node_Count(p->left_lnk) + node_Count(p->rght_lnk);
}
template <class Type>
int binary_Tree_Type<Type>::leaves_Count(binary_Tree_nd<Type>* p) const
{
if (p == NULL)
return 0;
else if (p->left_lnk == NULL && p->rght_lnk == NULL)
return 1;
else
return leaves_Count (p->left_lnk) + leaves_Count (p->rght_lnk);
}
template <class Type>
void binary_Tree_Type<Type>::swap_sub_trees(binary_Tree_nd<Type>* p) const
{
if (p == NULL)
return;
binary_Tree_nd <Type>* temp = p->left_lnk;
p->left_lnk = p->rght_lnk;
p->rght_lnk = temp;
swap_sub_trees(p->left_lnk);
swap_sub_trees(p->rght_lnk);
}
int main()
{
binary_Tree_Type<int> T;
T.elements_insertion(12);
T.elements_insertion(23);
T.elements_insertion(19);
T.elements_insertion(7);
T.elements_insertion(4);
T.elements_insertion(13);
cout << “n after insertion: ” ;
T.trav();
cout << “n Number of nodes: ” << T.node_Count() << endl;
cout << “n Number of leaves: ” << T.leaves_Count() << endl;
cout << “n left and right subtrees swapping: ” ;
T.swap_sub_trees();
T.trav();
return 0;
}