Answered step by step
Verified Expert Solution
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 Qh 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 ; if the sum of the nodes in the right
subtree is greater, return ; if the sums are equal, return If the binary tree is empty,
return
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 HbinaryTree
#define HbinaryTree
#include
# IMPORTANT NOTICE
# This is the header file binaryTreeType class, which is handled in your lab session.
# For the Q of labexam, 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& operatorconst 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 insertconst elemType& insertItem;
binaryTreeTypeconst binaryTreeType& otherTree;
binaryTreeType;
protected:
nodeType root;
private:
void copyTreenodeType &copiedTreeRoot, nodeType otherTreeRoot;
void inordernodeType p const;
void preordernodeType p const;
void postordernodeType p const;
int heightnodeType p const;
int maxint x int y const;
int nodeCountnodeType p const;
For Questions:
int leavesCountnodeType p const;
void swapSubtrees;
void swapSubtreesOfNodenodeType p;
int singleParent const;
int singlePnodeType p const;
int sumnodeType 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
inorderroot;
template
void binaryTreeType::preorderTraversal const
preorderroot;
template
void binaryTreeType::postorderTraversal const
postorderroot;
template
int binaryTreeType::treeHeight const
return heightroot;
template
int binaryTreeType::treeNodeCount const
return nodeCountroot;
For Questions:
template
int binaryTreeType::treeLeavesCount const
return leavesCountroot;
template
void binaryTreeType::treeSwapSubtrees
return swapSubtreesroot;
template
void binaryTreeType::treeSwapSubtreesOfNode
return swapSubtreesOfNoderoot;
template
int binaryTreeType::treeSingleParent const
return singleParentroot;
template
int binaryTreeType::treeSingleP const
return singleProot;
end
template
void binaryTreeType::copyTree
nodeType &copiedTreeRoot,
nodeType otherTreeRoot
if otherTreeRoot NULL
copiedTreeRoot NULL;
else
copiedTreeRoot new nodeType;
copiedTreeRootinfo otherTreeRootinfo;
copyTreecopiedTreeRootlLink, otherTreeRootlLink;
copyTreecopiedTreeRootrLink, otherTreeRootrLink;
end copyTree
template
void binaryTreeType::inorder
nodeType p const
if p NULL
inorderplLink;
cout pinfo ;
inorderprLink;
template
void binaryTreeType::preorder
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started