Answered step by step
Verified Expert Solution
Question
1 Approved Answer
c + + Write two functions for our Binary Search Tree class that calculate the minimum and maximum value found in the tree. #ifndef TREE
c Write two functions for our Binary Search Tree class that calculate the minimum and maximum value found in the tree.
#ifndef TREEH
#define TREEH
#include
#include
using namespace std;
template
struct node
TYPE value;
node left;
node right;
node
left nullptr;
right nullptr;
nodeTYPE value
thisvalue value;
left nullptr;
right nullptr;
;
template
class Tree
public:
node root;
Tree
root nullptr;
Treenode root
thisroot root;
node searchTYPE value, node current
ifcurrent nullptr
return nullptr;
else ifvalue currentvalue
return current;
else ifvalue currentvalue
return searchvalue currentleft;
else ifvalue currentvalue
return searchvalue currentright;
node searchTYPE value
return searchvalue thisroot;
void insertTYPE value, node current
ifthisroot nullptr
root new nodevalue;
else ifvalue currentvalue
ifcurrentleft nullptr
currentleft new nodevalue;
else
insertvalue currentleft;
else ifvalue currentvalue
ifcurrentright nullptr
currentright new nodevalue;
else
insertvalue currentright;
void insertTYPE value
insertvalue thisroot;
void displaynode current
ifcurrent nullptr
displaycurrentleft;
cout currentvalue current
;
displaycurrentright;
void display
displaythisroot;
node successornode current
current currentright;
while current && currentleft nullptr
current currentleft;
return current;
node removeTYPE target, node current
if current nullptr
return current;
else if target currentvalue
currentleft removetarget currentleft;
else if target currentvalue
currentright removetarget currentright;
else
if currentleft nullptr and currentright nullptr
delete current;
return nullptr;
else if currentleft nullptr
node temp currentright;
delete current;
return temp;
else if currentright nullptr
node temp currentleft;
delete current;
return temp;
node temp successorcurrent;
currentvalue tempvalue;
currentright removetempvalue, currentright;
return current;
node removeTYPE value
return removevalue thisroot;
int sizenode current
ifcurrent nullptr
return ;
else
return sizecurrentleft sizecurrentright;
int size
return sizethisroot;
void makeArraynode current, vector &list
ifcurrent nullptr
thismakeArraycurrentleft, list;
list.pushbackcurrent;
thismakeArraycurrentright, list;
node balanceTreevector &list,
int begin, int end
ifbegin end
return nullptr;
int mid begin end;
listmidleft balanceTreelist begin, mid ;
listmidright balanceTreelist mid end;
return listmid;
void balance
vector list;
thismakeArraythisroot, list;
thisroot thisbalanceTreelist list.size;
int totalNodesnode current returns the size of the tree
if current nullptr
return ;
int l totalNodescurrentleft;
int r totalNodescurrentright;
return l r;
int totalNodes
return totalNodesthisroot;
;
#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