C++ Binary Search Tree 5 Find the lowest number of Krabby Patties eaten by any c
ID: 3708503 • Letter: C
Question
C++
Binary Search Tree
5 Find the lowest number of Krabby Patties eaten by any customer (requires two functions) The public function (getLeastNumPatties) accepts two arguments- a string that will eventually hold the customer's name that has the least number of eaten Krabby Patties and an integer that will eventually hold the customer's # of eaten Krabby Patties. This function sets the integer parameter (leastPatties) to the root node's # of patties and then calls the private function (getLeast) The getleast function is a recursive function that traverses the nodes like the displaylnOrder function except instead of printing out the customer's name, it will check to see if the current node has a smaller number then the one saved in the leastPatties variable. If so, then save this node's customer name & number of patties as the new least. a. b. 6. Find the highest number of Krabby Patties eaten by any customer (requires two functions) a. Follow the instructions for finding the lowest except find the highest instead 7. Find the total number of Krabby Patties eaten by AL customers (requires two functions) The public function (getTotalNumPatties) accepts no arguments. It reates a variable to hold the total and then calls the private getTotal function, sending the root node & the total variable (send total by reference) to the function. Then, this function will return the total as an integer. The getTotal function is a recursive function that traverses the tree like the displaylnOrder function except instead of printing out the customer's name, it will get a running total of Krabby Patties eaten from each node. a. b.Explanation / Answer
void getLeast(node *root, string &name, int &num){
if (root == NULL)
return;
if (root->getValue() < num){
num = root->getValue();
name = root->getName();
}
getLeast(root->leftChild,name, num);
getLeast(root->rightChild,name, num);
}
void getHighest(node *root, string &name, int &num){
if (root == NULL)
return;
if (root->getValue() > num){
num = root->getValue();
name = root->getName();
}
getLeast(root->leftChild,name, num);
getLeast(root->rightChild,name, num);
}
void getTotal(node *root, int &num){
if (root == NULL)
return;
num = num + root->getValue();
getLeast(root->leftChild,name, num);
getLeast(root->rightChild,name, num);
}
void getLeastNumPatties(string &name, int &num){
num = 1000;
getLeast(root,name,num);
}
void getHighestNumPatties(string &name, int &num){
num = 0;
getHighest(root,name,num);
}
void int getTotalNumPatties(){
int num
getTotal(root,num);
return num;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.