Question

In: Computer Science

I am having an error in this function. It says Max was not found. int height()...

I am having an error in this function. It says Max was not found.

int height() const {

  
if (is_null())
return 0;
return 1 + max(get_left_subtree().height(), get_right_subtree().height());
}

The whole code is

#ifndef BINARY_TREE_H
#define BINARY_TREE_H

#include <cstddef>
#include <sstream>
#include <stdexcept>
#include <string>
#include <algorithm>
#include "BTNode.h"

template<typename Item_Type>
class Binary_Tree
{
public:

Binary_Tree() : root(NULL) {}

Binary_Tree(const Item_Type& the_data,
const Binary_Tree<Item_Type>& left_child
= Binary_Tree(),
const Binary_Tree<Item_Type>& right_child
= Binary_Tree()) :
root(new BTNode<Item_Type>(the_data, left_child.root,
right_child.root)) {}

virtual ~Binary_Tree() {}

Binary_Tree<Item_Type> get_left_subtree() const;

Binary_Tree<Item_Type> get_right_subtree() const;

const Item_Type& get_data() const;

bool is_null() const;

bool is_leaf() const;

bool is_full() const;

virtual std::string to_string() const;

static Binary_Tree<Item_Type> read_binary_tree(std::istream& in);

std::string root_to_string() const {
return root->to_string();
}

std::string pre_order() const {
return pre_order(root);
}

std::string post_order() const {
return post_order(root);
}
  
std::string in_order() const {
return in_order(root);
}
  
int height() const {
  
if (is_null())
return 0;
return 1 + max(get_left_subtree().height(), get_right_subtree().height());
}

int number_of_nodes() const {
if (is_null()) return 0;
return 1 + get_left_subtree().number_of_nodes() + get_right_subtree().number_of_nodes();
}


protected:

Binary_Tree(BTNode<Item_Type>* new_root) : root(new_root) {}

BTNode<Item_Type>* root;

private:

std::string pre_order(const BTNode<Item_Type>* local_root) const;
  
std::string post_order(const BTNode<Item_Type>* local_root) const;
  
std::string in_order(const BTNode<Item_Type>* local_root) const;

};
template<typename Item_Type>
std::ostream& operator<<(std::ostream& out,
const Binary_Tree<Item_Type>& tree)
{
return out << tree.to_string();
}

template<typename Item_Type>
std::istream& operator>>(std::istream& in,
Binary_Tree<Item_Type>& tree)
{
tree = Binary_Tree<Item_Type>::read_binary_tree(in);
return in;
}


template<typename Item_Type>
Binary_Tree<Item_Type>
Binary_Tree<Item_Type>::get_left_subtree() const {
if (root == NULL) {
throw std::invalid_argument("get_left_subtree on empty tree");
}
return Binary_Tree<Item_Type>(root->left);
}


template<typename Item_Type>
Binary_Tree<Item_Type>
Binary_Tree<Item_Type>::get_right_subtree() const {
if (root == NULL) {
throw std::invalid_argument("get_right_subtree on null tree");
}
return Binary_Tree<Item_Type>(root->right);
}


template<typename Item_Type>
const Item_Type& Binary_Tree<Item_Type>::get_data() const {
if (root == NULL) {
throw std::invalid_argument("get_data on null tree");
}
return root->data;
}

template<typename Item_Type>
bool Binary_Tree<Item_Type>::is_null() const {
return root == NULL;
}

template<typename Item_Type>
bool Binary_Tree<Item_Type>::is_leaf() const {
if (root != NULL) {
return root->left == NULL && root->right == NULL;
}
else
return true;
}

template<typename Item_Type>
std::string Binary_Tree<Item_Type>::to_string() const {
std::ostringstream os;
if (is_null())
os << "NULL\n";
else {
os << *root << '\n';
os << get_left_subtree().to_string();
os << get_right_subtree().to_string();
}
return os.str();
}

template<typename Item_Type>
Binary_Tree<Item_Type> Binary_Tree<Item_Type>::
read_binary_tree(std::istream& in) {
std::string next_line;
getline(in, next_line);
if (next_line == "NULL") {
return Binary_Tree<Item_Type>();
}
else {
Item_Type the_data;
std::istringstream ins(next_line);
ins >> the_data;
Binary_Tree<Item_Type> left = read_binary_tree(in);
Binary_Tree<Item_Type> right = read_binary_tree(in);
return Binary_Tree<Item_Type>(the_data, left, right);
}
}


template<typename Item_Type>
std::string Binary_Tree<Item_Type>::pre_order(const BTNode<Item_Type>* local_root) const {
std::string result;
if (local_root != NULL) {
result += local_root->to_string();
if (local_root->left != NULL) {
result += " ";
result += pre_order(local_root->left);
}
if (local_root->right != NULL) {
result += " ";
result += pre_order(local_root->right);
}
}
return result;
}

template<typename Item_Type>
std::string Binary_Tree<Item_Type>::post_order(const BTNode<Item_Type>* local_root) const {
std::string result;
if (local_root != NULL) {
if (local_root->left != NULL) {
result += post_order(local_root->left);
result += " ";
}
if (local_root->right != NULL) {
result += post_order(local_root->right);
result += " ";
}
result += local_root->to_string();
}
return result;
}

template<typename Item_Type>
std::string Binary_Tree<Item_Type>::in_order(const BTNode<Item_Type>* local_root) const {
std::string result;
if (local_root != NULL) {
result += "(";
if (local_root->left != NULL) {
result += in_order(local_root->left);
result += " ";
}
result += local_root->to_string();
if (local_root->right != NULL) {
result += " ";
result += in_order(local_root->right);
}
result += ")";
}
return result;
}

#endif

Solutions

Expert Solution

There is no max() function defined in the code, and also this function is not present in C library. C programming language, doesn't have in-built function min() and max(). Hence the programmer is required to implement his/her own functionality to find minimum and maximum.

To find maximum value,

#define max(a,b) (((a)>(b))?(a):(b))

To find minimum value,  

#define min(a,b) (((a)<(b))?(a):(b))

Now, we have defined the function max, which takes two parameters and return the maximum of two if max(5, 6) is called 6 is returned. If min(5,6) is called 5 is returned.

Equivalent of the above statement is a function which looks like this

int max(int a, int b){
    if(a>b)
        return a;
    else
        return b;
}
int min(int a, int b){
    if(a<b)
        return a;
    else
        return b;
}

Add any one of the above codes, to rectify that error. Using #define macro, would be more readable.


Related Solutions

I am having an issue with the code. The issue I am having removing the part...
I am having an issue with the code. The issue I am having removing the part when it asks the tuter if he would like to do teach more. I want the program to stop when the tuter reaches 40 hours. I believe the issue I am having is coming from the driver. Source Code: Person .java File: public abstract class Person { private String name; /** * Constructor * @param name */ public Person(String name) { super(); this.name =...
Why am I getting this error using 000webhost , but I do not get this error...
Why am I getting this error using 000webhost , but I do not get this error running XAMPP? Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/006/14881006/public_html/test/index.php:1) in /storage/ssd2/006/14881006/public_html/test/index.php on line 8 Code is below. ******************************************************************************************************************** <!DOCTYPE html> <?php    //Determine if the submit button has been clicked to set the cookie name and value    if (isset($_POST['name'])){            $cookie_name = $_POST['name'];            $cookie_value = $_POST['value'];            setcookie($cookie_name, $cookie_value,...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
I am having trouble understanding what value to use for each function. The question is: "Tom...
I am having trouble understanding what value to use for each function. The question is: "Tom plans to save $88 a month, starting today, for 22 years. Dean plans to save $88 a month for 22 years, starting one month from today. Both Tom and Dean expect to earn an average return o 5.27 percent APR on thier savings and both will make the same number of deposits. At the end of the 22 years, how much more (in $)...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed...
I'm having trouble implementing the merge function on this recursive merge sort. I am not allowed to change the parameter list in the functions. This is what I have so far, written in C. So far I've been getting segfaults, and I feel like it's because I'm off by 1, somewhere... void merge_the_array(int *list, int l1, int h1, int l2, int h2){ // create a temp array for l1-h1 int arr_a[(h1-l1)+1]; // create a temp array for l2-h2, adding 1...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
I am looking at the Transitional Gains Trap. and at the end, it says that It...
I am looking at the Transitional Gains Trap. and at the end, it says that It is very difficult to remove such a policy afterwards because it will reduce net returns to the protected group below normal levels. The benefits are only temporary but as a new group comes into using the policy then they will only earn normal profits. what does it mean by protected group ? and what it means by  The benefits are only temporary but as a...
I have an error on my code. the compiler says 'setLastName' was not declared in this...
I have an error on my code. the compiler says 'setLastName' was not declared in this scope. this is my code : #include <iostream> using std::cout; using std::cin; using std::endl; #include <string> using std::string; class Employee {    public :               Employee(string fname, string lname, int salary)        {            setFirstName(fname);            setLastName(lname);            setMonthlySalary(salary);        }               void setFirstName(string fname)        {       ...
Hi, I am doing an experiment that is called NaBH4 reduction of acetophenone. I am having...
Hi, I am doing an experiment that is called NaBH4 reduction of acetophenone. I am having trouble understand the techinque/wording when using a sep funnel. These are some of the procedures below: 1. Pour the mixture into a separatory funnel and add 20 mL of ice water. Rinse the beaker with additional ice water (5 mL) and add the rinsing to the separatory funnel. Rinise the beaker with ether (2 X 15 mL), adding the ether to the separatory funnel....
I am creating SAS code, but am receiving an errors " ERROR: Value is out of...
I am creating SAS code, but am receiving an errors " ERROR: Value is out of range or inappropriate. ERROR: No body file. HTML5(WEB) output will not be created." This is the code: option ls=65 ps=65; data one; input IQ; cards; 145 139 122 ; title 'Normal Quantile - Quantile Plot for IQ'; ods graphics on; proc univariate data=one; qqplot IQ / normal (mu=est sigma=est); run;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT