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

This is a Java program that I am having trouble making. 1- Search for the max...
This is a Java program that I am having trouble making. 1- Search for the max value in the linked list. 2- Search for the min value in the linked list. 3- Swap the node that has the min data value with the max one. (Note: Move the nodes to the new positions). 4- Add the min value and the max value and insert the new node with the calculated value before the last node. I already made a generic...
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,...
Hello I'm currently having an error that says "main is not abstract and does not override...
Hello I'm currently having an error that says "main is not abstract and does not override abstract method" is there a way to fix the error? import java.io.IOException; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; import static javafx.application.Application.launch; import javafx.scene.layout.StackPane; public class Main extends Application implements EventHandler<ActionEvent>{     public static void main(String[] args){         launch(args);     }     @Override     public void Start(Stage stage){         stage.setTitle("Test");         Button calcButton = new Button("Steam");                StackPane...
I am currently working on this problem. It says it is wrong, and I was wondering...
I am currently working on this problem. It says it is wrong, and I was wondering if someone could correct my code to where its ouput is as followed. Expected Output: 6↵ 5↵ 4↵ 3↵ 2↵ 1 Actual Output: 9↵ 8↵ 7↵ 6↵ 5↵ 4↵ 3↵ 2↵ 1↵ 0↵ 9.10: Reverse Array Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT