In: Computer Science
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
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.