Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ Write a function, singleParent, that returns the number of nodes in a binary tree that have only one child. Add this function the class
C++
Write a function, singleParent, that returns the number of nodes in a binary tree that have only one child. Add this function the class binaryTreeType and create a program to test this function. (NOTE: First create a binary search tree.)
***Here is the class binaryTreeType that the function needs to be added to.***
#ifndef H_binaryTree #define H_binaryTree #includetemplate struct nodeType { elemType info; nodeType *llink; nodeType *rlink; }; template class binaryTreeType { public: const binaryTreeType & operator=(const binaryTreeType &); bool isEmpty(); void inorderTraversal(); void preorderTraversal(); void postorderTraversal(); int treeHeight(); int treeNodeCount(); int treeLeavesCount(); void destroyTree(); binaryTreeType(const binaryTreeType & otherTree); binaryTreeType(); ~binaryTreeType(); protected: nodeType *root; private: void copyTree(nodeType * &copiedTreeRoot, nodeType * otherTreeRoot); void destroy(nodeType * &p); void inorder(nodeType *p); void preorder(nodeType *p); void postorder(nodeType *p); int height(nodeType *p); int max(int x, int y); int nodeCount(nodeType *p); int leavesCount(nodeType *p); }; //constructor template binaryTreeType ::binaryTreeType() { root = NULL; } template bool binaryTreeType ::isEmpty() { return (root == NULL); } template void binaryTreeType ::inorderTraversal() { inorder(root); } template void binaryTreeType ::preorderTraversal() { preorder(root); } template void binaryTreeType ::postorderTraversal() { postorder(root); } template int binaryTreeType ::treeHeight() { return height(root); } template int binaryTreeType ::treeNodeCount() { return nodeCount(root); } template int binaryTreeType ::treeLeavesCount() { return leavesCount(root); } 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); } } template void binaryTreeType ::inorder(nodeType *p) { if(p != NULL) { inorder(p->llink); cout< info<<" "; inorder(p->rlink); } } template void binaryTreeType ::preorder(nodeType *p) { if(p != NULL) { cout< info<<" "; preorder(p->llink); preorder(p->rlink); } } template void binaryTreeType ::postorder(nodeType *p) { if(p != NULL) { postorder(p->llink); postorder(p->rlink); cout< info<<" "; } } template const binaryTreeType & binaryTreeType :: operator=(const binaryTreeType & otherTree) { if(this != &otherTree) //avoid self-copy { if(root != NULL) //if the binary tree is not empty, destroy the binary tree destroy(root); if(otherTree.root == NULL) //otherTree is empty root = NULL; else copyTree(root, otherTree.root); }//end else return *this; } template void binaryTreeType ::destroy(nodeType * &p) { if(p != NULL) { destroy(p->llink); destroy(p->rlink); delete p; p = NULL; } } template void binaryTreeType ::destroyTree() { destroy(root); } //copy constructor template binaryTreeType ::binaryTreeType(const binaryTreeType & otherTree) { if(otherTree.root == NULL) //otherTree is empty root = NULL; else copyTree(root, otherTree.root); } //destructor template binaryTreeType ::~binaryTreeType() { destroy(root); } template int binaryTreeType ::height(nodeType *p) { if(p == NULL) return 0; else return 1 + max(height(p->llink), height(p->rlink)); } template int binaryTreeType ::max(int x, int y) { if(x >= y) return x; else return y; } template int binaryTreeType ::nodeCount(nodeType *p) { if (p==NULL) return 0; else return 1 + nodeCount(p->llink) + nodeCount(p->rlink); } template int binaryTreeType ::leavesCount(nodeType *p) { if (p==NULL) return 0; else if ((p->llink==NULL)&&(p->rlink==NULL)) return 1; else return (leavesCount(p->llink) + leavesCount(p->rlink)); } #endif
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