Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions

Question

5 What does it mean to think of an organisation as an open system?

Answered: 1 week ago