Question: #include #include bintree.h #include / / input and output / / Constructors BinTree::BinTree ( ) : root ( nullptr ) { } / /
#include
#include "bintree.h
#include input and output
Constructors
BinTree::BinTree : rootnullptr
Copy constructor
BinTree::BinTreeconst BinTree &other : rootcopyTreeotherroot
Assignment operator
BinTree &BinTree::operatorconst BinTree &other
if this &other
clearTreeroot;
root copyTreeotherroot;
return this;
Destructor
BinTree::~BinTree
clearTreeroot;
isEmpty
bool BinTree::isEmpty const
return root nullptr;
bool BinTree::retrieveconst string &target, Node &retrieved const
return retrieveHelperroot target, retrieved;
bool BinTree::retrieveHelperNode root const string &target, Node &retrieved const
if root nullptr
retrieved nullptr;
return false;
if rootdata target
retrieved root;
return true;
else if rootdata target
return retrieveHelperrootright, target, retrieved;
else if rootdata target
return retrieveHelperrootleft, target, retrieved;
retrieved nullptr;
return false;
bool BinTree::insertconst string &inserting
return insertHelperroot inserting;
bool BinTree::insertHelperNode &root, const string &inserting
if root nullptr
root new Node;
rootdata inserting;
rootleft nullptr;
rootright nullptr;
return true;
else if inserting rootdata
return insertHelperrootleft, inserting;
else if inserting rootdata
return insertHelperrootright, inserting;
else
Duplicate values are not allowed
return false;
displayTree
void BinTree::displayTree const
displayTreeHelperroot cout, ;
displayTreeHelper
void BinTree::displayTreeHelperconst Node root ostream &os const string &prefix const
if root nullptr
os prefix rootdata endl;
Explicitly cast prefix to a nonconst string before modifying
string mutablePrefix constcastprefix;
displayTreeHelperrootleft, os mutablePrefix L;
displayTreeHelperrootright, os mutablePrefix R;
displaySideways
void BinTree::displaySideways const
displaySidewaysHelperroot;
void BinTree::displaySidewaysHelperconst Node node int level const
if node nullptr
displaySidewaysHelpernoderight, level ;
for int i ; i level; i
cout ;
cout nodedata endl;
displaySidewaysHelpernodeleft, level ;
getHeight
int BinTree::getHeightconst string &target const
return getHeightHelperroot target;
Overloaded operators
bool BinTree::operatorconst BinTree &other const
return compareTreesroot other.root;
bool BinTree::operator!const BinTree &other const
return this other;
void BinTree::bstreeToArraystring arr
int index ;
bstreeToArrayHelperroot arr, index;
makeEmpty; make the tree empty after converting to array
void BinTree::bstreeToArrayHelperNode root string arr int &index
if root nullptr
bstreeToArrayHelperrootleft, arr, index;
arrindex rootdata;
bstreeToArrayHelperrootright, arr, index;
delete root; deallocate the node after storing data in the array
void BinTree::arrayToBSTreestring arr
makeEmpty;
int high ;
int index ;
while index ARRAYSIZE
if arrindexempty
high;
else
arrindex; Set empty string
index;
arrayToBSTreeHelperroot arr, high ;
void BinTree::arrayToBSTreeHelperNode &root, string arr int low, int high
if low high
int mid low high;
string temp arrmid;
arrmid;
if root nullptr
root new Node;
rootdata temp;
rootleft nullptr;
rootright nullptr;
Corrected recursive calls
arrayToBSTreeHelperrootleft, arr, low, mid ;
arrayToBSTreeHelperroot
right, arr, mid high;
void BinTree::clearTreeNode &node
if node nullptr
clearTreenodeleft;
clearTreenoderight;
delete node;
node nullptr;
Node BinTree::copyTreeconst Node sourceNode
if sourceNode nullptr
return nullptr;
else
Node newNode new NodesourceNode; Copy data
newNodeleft copyTreesourceNodeleft; Recursively copy left subtree
newNoderight copyTreesourceNoderight; Recursively
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
