Write the definition of the function nodeCount that returns the number of codes in the binary tree. Add this function to the class binaryTreeType and createe a program to test this function. C++
`Hey,
Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries
#include<iostream>
using namespace std;
struct node
{
int key_value;
node *left;
node *right;
};
class binaryTreeType
{
public:
node *root;
binaryTreeType ();
~binaryTreeType ();
void insert(int key);
int nodeCount(node *rt);
int singleParent(node *rt);
int leafCount(node *rt);
private:
void insert(int key, node *leaf);
};
binaryTreeType ::binaryTreeType ()
{
root=NULL;
}
void binaryTreeType ::insert(int key, node *leaf)
{
if(key< leaf->key_value)
{
if(leaf->left!=NULL)
insert(key, leaf->left);
else
{
//node creation
leaf->left=new node;
leaf->left->key_value=key;
leaf->left->left=NULL;
leaf->left->right=NULL;
}
}
else if(key>=leaf->key_value)
{
if(leaf->right!=NULL)
insert(key, leaf->right);
else
{//node creation
leaf->right=new node;
leaf->right->key_value=key;
leaf->right->left=NULL;
leaf->right->right=NULL;
}
}
}
void binaryTreeType ::insert(int key)
{
if(root!=NULL)
insert(key, root);
else
{
root=new node;
root->key_value=key;
root->left=NULL;
root->right=NULL;
}
}
//below code is for our question to find the leaf count,node count
and single parent node count
int binaryTreeType ::leafCount(node *rt)//count the number of leaf
nodes in tree
{
if(rt == NULL)
return 0;
if(rt->left == NULL && rt->right==NULL)
return 1;
else
return leafCount(rt->left)+leafCount(rt->right);
}
binaryTreeType ::~binaryTreeType ()
{}
int binaryTreeType ::singleParent(node *rt)//count the number of
single parent nodes in tree
{
if(rt==NULL)
return 0;
else
{
if (rt->left == NULL && rt->right != NULL)
return 1 + singleParent(rt->right);
else if(rt->right == NULL && rt->left != NULL)
return 1 + singleParent(rt->left);
else
return singleParent(rt->left)+singleParent(rt->right);
}
}
int binaryTreeType ::nodeCount(node *rt)//count the total nodes in
tree
{
if(rt==NULL)
return 0;
else
{
return 1+nodeCount(rt->left)+nodeCount(rt->right);
}
}
int main()
{
binaryTreeType b;
//tree creation with inserting nodes
b.insert(23);
b.insert(13);
b.insert(223);
b.insert(2);
b.insert(3);
b.insert(123);
b.insert(43);
b.insert(6);
//calling the implemented functions
cout<<"number of leaf nodes in tree is\t"<<
b.leafCount(b.root)<<endl;
cout<<"number of nodes in tree is\t"<<
b.nodeCount(b.root)<<endl;
cout<<"number of single parent nodes in tree is\t"<<
b.singleParent(b.root)<<endl;
}

Kindly revert for any queries
Thanks.
Write the definition of the function nodeCount that returns the number of codes in the binary...
Data Structures Using C++: Using C++: Write the definition of the function, nodeCount, that returns the number of nodes in a binary tree. Add this function to the class binaryTreeType and create a program to test this function. Read the tree definitions from files.
C++ Write a function, singleParent, that returns the number of nodes in a binary tree that have only one child. Add this function to the class binaryTreeType and create a program to test this function. (Note: First create a binary search tree.)
(C++ only) Write a function that returns a decimal number from a binary string. The function header is as follows: int bin2Dec(const string& binaryString) For example, bin2Dec("10001") returns 17. Write a test program that prompts the user to enter a binary number as a string and displays its decimal equivalent value Sample Input: 1110100110101 Sample Output: Enter a bianry number: 1110100110101 7477
Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...
By definition, the height of a node in a binary tree is the number of edges along the longest path from the node to any leaf. Assume the following node structure struct TreeNode int data; node Type right; // points to right child node Type "Left; // points to left child ) Write a recursive function that takes a pointer to a node in a binary tree and returns its height. Note: the height of a leaf node is 0...
By definition, the height of a node in a binary tree is the number of edges along the longest path from the node to any leaf. Assume the following node structure struct TreeNode! int data; node Type right; // points to right child node Type "left; // points to left child }; Write a recursive function that takes a pointer to a node in a binary tree and returns its height. Note: the height of a leaf node is o...
Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType *right; }; //Definition of class Binary Tree template <class elemType> class Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...
Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...
in java ..write all complete program from a- e
1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...
please explain each line of code! ( in python
)
1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....