Question
C++ Programming: Modify integer_tree.h and integer_tree.cpp to define a templated class for any numeric type. // filename: integer_tree. /* In this file we give a
C++ Programming: Modify integer_tree.h and integer_tree.cpp to define a templated class for any numeric type.
// filename: integer_tree.
/* In this file we give a definition of a binary tree of integer numbers */
#ifndef INTEGER_TREE_H #define INTEGER_TREE_H #include "integer_tree_node.h"
#include
namespace integer_tree { class Tree { private:
Node *root; // data member
// construct tree from a node Tree(Node *r) : root(r) {}
public:
Tree() : root(NULL) {}
Tree(const int& item, const Tree& left = Tree(), const Tree& right = Tree() ) : root(new Node(item,left.root,right.root)) {}
Tree get_left() const; // returns left child // precondition: not is_left_null() Tree get_right() const; // returns right child // precondition: not is_right_null();
bool is_left_null() const; // true if left child is null bool is_right_null() const; // true if right child is null
int get_data() const; // returns data at node
void insert(int item); // inserts item in sorted tree std::string to_string(); // returns inorder printing }; } #endif
AND THE OTHER FILE:
// filename: integer_tree.cpp
#include "integer_tree.h"
namespace integer_tree { Tree Tree::get_left() const { return Tree(root->left); }
Tree Tree::get_right() const { return Tree(root->right); }
bool Tree::is_left_null() const { return (root->left == NULL); }
bool Tree::is_right_null() const { return (root->right == NULL); }
int Tree::get_data() const { return root->data; }
void Tree::insert(int item) { if(root == NULL) root = new Node(item); else if(item < root->data) { Tree L = this->get_left(); L.insert(item); root->left = L.root; } else { Tree R = this->get_right(); R.insert(item); root->right = R.root; } }
std::string Tree::to_string() { using std::string; if(root == NULL) return ""; else { string L = this->get_left().to_string(); string d = root->to_string(); string R = this->get_right().to_string(); return L + " " + d + " " + R; } } }
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