Question: Instructions Write the function singleParent that returns the number of nodes in a binary tree that have only one child. Add this function to the
Instructions
Write the function singleParent that returns the number of nodes in a binary tree that have only one child.
Add this function to the class binaryTreeType and create a program to test this function. (Note: First create a binary search tree.)
binarySearchTree.h
//HeaderFileBinarySearchTree
#ifndefH_binarySearchTree
#defineH_binarySearchTree
#include
#include"binaryTree.h"
usingnamespacestd;
template
classbSearchTreeType:publicbinaryTreeType
{
public:
boolsearch(constelemType&searchItem)const;
//FunctiontodetermineifsearchItemisinthebinary
//searchtree.
//Postcondition:ReturnstrueifsearchItemisfoundin
//thebinarysearchtree;otherwise,
//returnsfalse.
voidinsert(constelemType&insertItem);
//FunctiontoinsertinsertIteminthebinarysearchtree.
//Postcondition:Ifthereisnonodeinthebinarysearch
//treethathasthesameinfoas
//insertItem,anodewiththeinfo
//insertItemiscreatedandinsertedinthe
//binarysearchtree.
voiddeleteNode(constelemType&deleteItem);
//FunctiontodeletedeleteItemfromthebinarysearchtree
//Postcondition:IfanodewiththesameinfoasdeleteItem
//isfound,itisdeletedfromthebinary
//searchtree.
//IfthebinarytreeisemptyordeleteItem
//isnotinthebinarytree,anappropriate
//messageisprinted.
private:
voiddeleteFromTree(nodeType
//Functiontodeletethenodetowhichppointsis
//deletedfromthebinarysearchtree.
//Postcondition:Thenodetowhichppointsisdeleted
//fromthebinarysearchtree.
};
template
boolbSearchTreeType
(constelemType&searchItem)const
{
nodeType
boolfound=false;
if(this->root==nullptr)
cout<<"Cannotsearchanemptytree."< else { current=this->root; while(current!=nullptr&&!found) { if(current->info==searchItem) found=true; elseif(current->info>searchItem) current=current->lLink; else current=current->rLink; }//endwhile }//endelse returnfound; }//endsearch template voidbSearchTreeType (constelemType&insertItem) { nodeType nodeType //behindcurrent nodeType newNode=newnodeType newNode->info=insertItem; newNode->lLink=nullptr; newNode->rLink=nullptr; if(this->root==nullptr) this->root=newNode; else { current=this->root; while(current!=nullptr) { trailCurrent=current; if(current->info==insertItem) { cout<<"Theitemtobeinsertedisalready"; cout<<"inthetree--duplicatesarenot" <<"allowed."< return; } elseif(current->info>insertItem) current=current->lLink; else current=current->rLink; }//endwhile if(trailCurrent->info>insertItem) trailCurrent->lLink=newNode; else trailCurrent->rLink=newNode; } }//endinsert template voidbSearchTreeType (constelemType&deleteItem) { nodeType nodeType boolfound=false; if(this->root==nullptr) cout<<"Cannotdeletefromanemptytree." < else { current=this->root; trailCurrent=this->root; while(current!=nullptr&&!found) { if(current->info==deleteItem) found=true; else { trailCurrent=current; if(current->info>deleteItem) current=current->lLink; else current=current->rLink; } }//endwhile if(current==nullptr) cout<<"Theitemtobedeletedisnotinthetree." < elseif(found) { if(current==this->root) deleteFromTree(this->root); elseif(trailCurrent->info>deleteItem) deleteFromTree(trailCurrent->lLink); else deleteFromTree(trailCurrent->rLink); } else cout<<"Theitemtobedeletedisnotinthetree." < } }//enddeleteNode template voidbSearchTreeType (nodeType { nodeType nodeType nodeType if(p==nullptr) cout<<"Error:Thenodetobedeleteddoesnotexist." < elseif(p->lLink==nullptr&&p->rLink==nullptr) { temp=p; p=nullptr; deletetemp; } elseif(p->lLink==nullptr) { temp=p; p=temp->rLink; deletetemp; } elseif(p->rLink==nullptr) { temp=p; p=temp->lLink; deletetemp; } else { current=p->lLink; trailCurrent=nullptr; while(current->rLink!=nullptr) { trailCurrent=current; current=current->rLink; }//endwhile p->info=current->info; if(trailCurrent==nullptr)//currentdidnotmove; //current==p->lLink;adjustp p->lLink=current->lLink; else trailCurrent->rLink=current->lLink; deletecurrent; }//endelse }//enddeleteFromTree #endif binaryTree.h //HeaderFileBinaryTree #ifndefH_binaryTree #defineH_binaryTree #include usingnamespacestd; //Definitionofthenode template structnodeType { elemTypeinfo; nodeType nodeType }; //Definitionoftheclass template classbinaryTreeType { public: constbinaryTreeType (constbinaryTreeType //Overloadtheassignmentoperator. boolisEmpty()const; //Functiontodeterminewhetherthebinarytreeisempty. //Postcondition:Returnstrueifthebinarytreeisempty; //otherwise,returnsfalse. voidinorderTraversal()const; //Functiontodoaninordertraversalofthebinarytree. //Postcondition:Nodesareprintedininordersequence. voidpreorderTraversal()const; //Functiontodoapreordertraversalofthebinarytree. //Postcondition:Nodesareprintedinpreordersequence. voidpostorderTraversal()const; //Functiontodoapostordertraversalofthebinarytree. //Postcondition:Nodesareprintedinpostordersequence. inttreeHeight()const; //Functiontodeterminetheheightofabinarytree. //Postcondition:Returnstheheightofthebinarytree. inttreeNodeCount()const; //Functiontodeterminethenumberofnodesina //binarytree. //Postcondition:Returnsthenumberofnodesinthe //binarytree. inttreeLeavesCount()const; //Functiontodeterminethenumberofleavesina //binarytree. //Postcondition:Returnsthenumberofleavesinthe //binarytree. voiddestroyTree(); //Functiontodestroythebinarytree. //Postcondition:Memoryspaceoccupiedbyeachnode //isdeallocated. //root=nullptr; virtualboolsearch(constelemType&searchItem)const=0; //FunctiontodetermineifsearchItemisinthebinary //tree. //Postcondition:ReturnstrueifsearchItemisfoundin //thebinarytree;otherwise,returns //false. virtualvoidinsert(constelemType&insertItem)=0; //FunctiontoinsertinsertIteminthebinarytree. //Postcondition:Ifthereisnonodeinthebinarytree //thathasthesameinfoasinsertItem,a //nodewiththeinfoinsertItemiscreated //andinsertedinthebinarysearchtree. virtualvoiddeleteNode(constelemType&deleteItem)=0; //FunctiontodeletedeleteItemfromthebinarytree //Postcondition:Ifanodewiththesameinfoas //deleteItemisfound,itisdeletedfrom //thebinarytree. //Ifthebinarytreeisemptyor //deleteItemisnotinthebinarytree, //anappropriatemessageisprinted. binaryTreeType(constbinaryTreeType //Copyconstructor binaryTreeType(); //Defaultconstructor ~binaryTreeType(); //Destructor protected: nodeType private: voidcopyTree(nodeType nodeType //Makesacopyofthebinarytreetowhich //otherTreeRootpoints. //Postcondition:ThepointercopiedTreeRootpointsto //therootofthecopiedbinarytree. voiddestroy(nodeType //Functiontodestroythebinarytreetowhichppoints. //Postcondition:Memoryspaceoccupiedbyeachnode,in //thebinarytreetowhichppoints,is //deallocated. //p=nullptr; voidinorder(nodeType //Functiontodoaninordertraversalofthebinary //treetowhichppoints. //Postcondition:Nodesofthebinarytree,towhichp //points,areprintedininordersequence. voidpreorder(nodeType //Functiontodoapreordertraversalofthebinary //treetowhichppoints. //Postcondition:Nodesofthebinarytree,towhichp //points,areprintedinpreorder //sequence. voidpostorder(nodeType //Functiontodoapostordertraversalofthebinary //treetowhichppoints. //Postcondition:Nodesofthebinarytree,towhichp //points,areprintedinpostorder //sequence. intheight(nodeType //Functiontodeterminetheheightofthebinarytree //towhichppoints. //Postcondition:Heightofthebinarytreetowhich //ppointsisreturned. intmax(intx,inty)const; //Functiontodeterminethelargerofxandy. //Postcondition:Returnsthelargerofxandy. intnodeCount(nodeType //Functiontodeterminethenumberofnodesin //thebinarytreetowhichppoints. //Postcondition:Thenumberofnodesinthebinary //treetowhichppointsisreturned. intleavesCount(nodeType //Functiontodeterminethenumberofleavesin //thebinarytreetowhichppoints //Postcondition:Thenumberofleavesinthebinary //treetowhichppointsisreturned. }; template boolbinaryTreeType { return(root==nullptr); } template binaryTreeType { root=nullptr; } template voidbinaryTreeType { inorder(root); } template voidbinaryTreeType { preorder(root); } template voidbinaryTreeType { postorder(root); } template intbinaryTreeType { returnheight(root); } template intbinaryTreeType { returnnodeCount(root); } template intbinaryTreeType { returnleavesCount(root); } template voidbinaryTreeType (nodeType { if(p!=nullptr) { inorder(p->lLink); cout< inorder(p->rLink); } } template voidbinaryTreeType (nodeType { if(p!=nullptr) { cout< preorder(p->lLink); preorder(p->rLink); } } template voidbinaryTreeType (nodeType { if(p!=nullptr) { postorder(p->lLink); postorder(p->rLink); cout< } } template intbinaryTreeType (nodeType { if(p==nullptr) return0; else return1+max(height(p->lLink),height(p->rLink)); } template intbinaryTreeType { if(x>=y) returnx; else returny; } template voidbinaryTreeType (nodeType nodeType { if(otherTreeRoot==nullptr) copiedTreeRoot=nullptr; else { copiedTreeRoot=newnodeType copiedTreeRoot->info=otherTreeRoot->info; copyTree(copiedTreeRoot->lLink,otherTreeRoot->lLink); copyTree(copiedTreeRoot->rLink,otherTreeRoot->rLink); } }//endcopyTree template voidbinaryTreeType { if(p!=nullptr) { destroy(p->lLink); destroy(p->rLink); deletep; p=nullptr; } } template voidbinaryTreeType { destroy(root); } //copyconstructor template binaryTreeType (constbinaryTreeType { if(otherTree.root==nullptr)//otherTreeisempty root=nullptr; else copyTree(root,otherTree.root); } //Destructor template binaryTreeType { destroy(root); } //Overloadtheassignmentoperator template constbinaryTreeType operator=(constbinaryTreeType { if(this!=&otherTree)//avoidself-copy { if(root!=nullptr) //ifthebinarytreeisnotempty, //destroythebinarytree destroy(root); if(otherTree.root==nullptr)//otherTreeisempty root=nullptr; else copyTree(root,otherTree.root); }//endelse return*this; } #endif I need main.cpp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
