Question: Implement the countLeaves function in the binaryTree.h file. Use the following files to create a program to test the new function. #ifndef H _ binaryTree

Implement the countLeaves function in the binaryTree.h file. Use the following files to create a program to test the new function.
#ifndef H_binaryTree
#define H_binaryTree
#include
using namespace std;
template
struct nodeType
{
elemType info;
nodeType *lLink;
nodeType *rLink;
};
template
class binaryTreeType
{
public:
const binaryTreeType& operator=
(const binaryTreeType&);
bool isEmpty() const;
void inorderTraversal() const;
void preorderTraversal() const;
void postorderTraversal() const;
int treeHeight() const;
int treeNodeCount() const;
int treeLeavesCount() const;
void destroyTree(); virtual bool search(const elemType& searchItem) const =0;
virtual void insert(const elemType& insertItem)=0;
virtual void deleteNode(const elemType& deleteItem)=0;
binaryTreeType(const binaryTreeType& otherTree);
binaryTreeType();
~binaryTreeType();
protected:
nodeType *root;
private:
void copyTree(nodeType* &copiedTreeRoot,
nodeType* otherTreeRoot);
void destroy(nodeType* &p);
void inorder(nodeType *p) const;
void preorder(nodeType *p) const;
void postorder(nodeType *p) const;
int height(nodeType *p) const;
int max(int x, int y) const;
int nodeCount(nodeType *p) const;
int leavesCount(nodeType *p) const;
};
template
bool binaryTreeType::isEmpty() const
{
return (root == nullptr);
}
template
binaryTreeType::binaryTreeType()
{
root = nullptr;
}
template
void binaryTreeType::inorderTraversal() const
{
inorder(root);
}
template
void binaryTreeType::preorderTraversal() const
{
preorder(root);
}
template
void binaryTreeType::postorderTraversal() const
{
postorder(root);
}
template
int binaryTreeType::treeHeight() const
{
return height(root);
}
template
int binaryTreeType::treeNodeCount() const
{
return nodeCount(root);
}
template
int binaryTreeType::treeLeavesCount() const
{
return leavesCount(root);
}
template
void binaryTreeType::inorder
(nodeType *p) const
{
if (p != nullptr)
{
inorder(p->lLink);
cout p->info "";
inorder(p->rLink);
}
}
template
void binaryTreeType::preorder
(nodeType *p) const
{
if (p != nullptr)
{
cout p->info "";
preorder(p->lLink);
preorder(p->rLink);
}
}
template
void binaryTreeType::postorder
(nodeType *p) const
{
if (p != nullptr)
{
postorder(p->lLink);
postorder(p->rLink);
cout p->info "";
}
}
template
int binaryTreeType::height
(nodeType *p) const
{
if (p == nullptr)
return 0;
else
return 1+ max(height(p->lLink), height(p->rLink));
}
template
int binaryTreeType::max(int x, int y) const
{
if (x >= y)
return x;
else
return y;
}
template
void binaryTreeType::copyTree
(nodeType* &copiedTreeRoot,
nodeType* otherTreeRoot)
{
if (otherTreeRoot == nullptr)
copiedTreeRoot = nullptr;
else
{
copiedTreeRoot = new nodeType;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink);
copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink);
}
}//end copyTree
template
void binaryTreeType::destroy(nodeType* &p)
{
if (p != nullptr)
{
destroy(p->lLink);
destroy(p->rLink);
delete p;
p = nullptr;
}
}
template
void binaryTreeType::destroyTree()
{
destroy(root);
}
template
binaryTreeType::binaryTreeType
(const binaryTreeType& otherTree)
{
if (otherTree.root == nullptr)//otherTree is empty
root = nullptr;
else
copyTree(root, otherTree.root);
}
template
binaryTreeType::~binaryTreeType()
{
destroy(root);
}
template
const binaryTreeType& binaryTreeType::
operator=(const binaryTreeType& otherTree)
{
if (this != &otherTree)
{
if (root != nullptr)
Implement the countLeaves function in the

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!