Question: Write a member function for the binaryTreeType class ( given in Q 3 . h ) that calculates and compares the sum of the nodes

Write a member function for the binaryTreeType class (given in Q3.h) that
calculates and compares the sum of the nodes in the left and right subtrees. If the sum of
the nodes in the left subtree is greater, return 1; if the sum of the nodes in the right
subtree is greater, return 2; if the sums are equal, return 0. If the binary tree is empty,
return -1.
int binaryTreeType::compareSubtreesSum() const
(Hint : First, write a helper (private) member function that takes a binary tree node as a
parameter and calculates the sum of that binary tree (rooted at the given parameter);
then use this helper function in the function which youre asked to code).
//Header File Binary Search Tree
#ifndef H_binaryTree
#define H_binaryTree
#include
# IMPORTANT NOTICE
# This is the header file binaryTreeType class, which is handled in your lab session.
# For the Q3 of lab-exam, just scroll down all the way to the bottom
# and fill in the given function blocks.
# !!!!!!!!!!!!!!!!
using namespace std;
//Definition of the Node
template
struct nodeType
{
elemType info;
nodeType *lLink;
nodeType *rLink;
};
//Definition of the class
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;
//For Questions:
int treeLeavesCount() const;
void treeSwapSubtrees();
void treeSwapSubtreesOfNode();
int treeSingleParent() const;
int treeSingleP() const;
int treeLargest() const;
int compareSubtreesSum() const;
//end
virtual void insert(const elemType& insertItem)=0;
binaryTreeType(const binaryTreeType& otherTree);
binaryTreeType();
protected:
nodeType *root;
private:
void copyTree(nodeType* &copiedTreeRoot, nodeType* otherTreeRoot);
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;
//For Questions:
int leavesCount(nodeType *p) const;
void swapSubtrees();
void swapSubtreesOfNode(nodeType* p);
int singleParent() const;
int singleP(nodeType* p) const;
int sum(nodeType *p) const;
//end
};
//Definition of member functions
template
binaryTreeType::binaryTreeType()
{
root = NULL;
}
template
bool binaryTreeType::isEmpty() const
{
return (root == NULL);
}
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);
}
//For Questions:
template
int binaryTreeType::treeLeavesCount() const
{
return leavesCount(root);
}
template
void binaryTreeType::treeSwapSubtrees()
{
return swapSubtrees(root);
}
template
void binaryTreeType::treeSwapSubtreesOfNode()
{
return swapSubtreesOfNode(root);
}
template
int binaryTreeType::treeSingleParent() const
{
return singleParent(root);
}
template
int binaryTreeType::treeSingleP() const
{
return singleP(root);
}
//end
template
void binaryTreeType::copyTree
(nodeType* &copiedTreeRoot,
nodeType* otherTreeRoot)
{
if (otherTreeRoot == NULL)
copiedTreeRoot = NULL;
else
{
copiedTreeRoot = new nodeType;
copiedTreeRoot->info = otherTreeRoot->info;
copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink);
copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink);
}
}//end copyTree
template
void binaryTreeType::inorder
(nodeType *p) const
{
if (p != NULL)
{
inorder(p->lLink);
cout << p->info <<"";
inorder(p->rLink);
}
}
template
void binaryTreeType::preorder

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 Databases Questions!