Question
C++ Problem For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of
C++ Problem
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template.
The definition of the class for a binary tree (as a template) is given as follows:
template < class T > class binTree {
public:
binTree ( );
// default constructor
unsigned height ( ) const;
// returns height of tree
virtual void insert ( const T& );
// inserts node in tree
void inorder ( void ( * ) ( const T& ) );
// inorder traversal of tree
protected:
Node < T >* root;
// root of tree
private:
unsigned height ( Node < T >* ) const;
// private version of height ( )
void insert ( Node < T >*&, const T& );
// private version of insert ( )
void inorder ( Node < T >*, void ( * ) ( const T& ) );
// private version of inorder ( )
};
Most of the public member functions of the binTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations in code. However, they require more memory and usually slower than their non-recursive versions in execution, especially for a large amount of input data.
Because of information hiding, a client is not permitted to access the binary tree directly, so the root of the tree is kept protected (not private because of future implementations of derived classes from the base class of the binTree), so it cannot be passed as an argument to any of the public functions of the tree. It is essential to have private utility functions, which act as interface between a client and the tree. The insert ( ) function of the binTree class is described as follows:
-insert ( const T& x ): This virtual function can be used to insert a node with the data value x in a binary tree, applying the following technique: if the tree is empty, then the new node will be the root of the tree with the value x; otherwise, the left or the right subtree is randomly selected and the value x is inserted in that side. To implement the random selection, you can use the following RNG.
typedef enum { left_side, right_side } SIDE;
SIDE rnd ( ) { return rand ( ) % 2 ? right_side : left_side; }
Put the implementation of your binTree class in the following header file:
binTree.h: Definition of the class Node, which represents the nodes in a binary tree, can be found in the header file Node.h. To use the class Node in your program, include the header file Node.h.
The source file prog6.cc contains the driver program. In addition to the main ( ) routine, it has the implementations of the following routines (as templates) and the definitions of the two RNGs used in the main ( ) routine.
o template < class T > void print ( const T& x ):
o template < class T > void print_vals ( binTree < T >& tree, const string& name );
The unary function print ( ) can be used as an argument to the member functions inorder ( ) to print the value of its argument x. The function print_vals ( ) does the followings: first, it prints name, which is the name of the tree, and it also prints the height of the tree. Then, it calls the member function inorder ( ) to print the data values in the tree in inorder. The class RND1 can be used to generate random integers in the range [ LOW1 = -999, HIGH1 = 999 ] and the class RND2 can be used to generate random floating-point numbers in the range [ LOW2 = -999.99, HIGH2 = 999.99 ]. The function objects RND1 ( ) and RND2 ( ), generated from these classes, are used to fill in the random values in vector containers vector < int > A ( N1 ) and vector < float > B ( N2 ) by using the generate ( ) function in the STL, where N1 = 100 and N2 = 50 are the sizes of these two vectors. The main ( ) routine copies the random values from vectors A and B and inserts them in the binary trees first and second, respectively. At the end, the data values in the binary trees first and second are printed out on stdout with LSIZE = 12 numbers in a single line.
The source file of the driver program prog6.cc and Node.h are provided.
//Node.h
#ifndef H_NODE #define H_NODE
// definition for class of nodes in bin tree
template < class T > class binTree; // forward declaration
template < class T > class Node { friend class binTree < T >; // binTree is friend public: // default constructor Node ( const T& x = T ( ), Node < T >* l = 0, Node < T >* r = 0 ) : data ( x ), left ( l ), right ( r ) { } private: T data; // data component Node < T > *left, *right; // left and right links };
#endif
//prog6.cc
#include
#include
#include
#include
#include
using namespace std;
#include "binTree.h"
#define SEED 1 // seed for RNGs
#define N1 100 // size of 1st vector container
#define LOW1 -999 // low val for rand integer
#define HIGH1 999 // high val for rand integer
#define N2 50 // size of 2nd vector container
#define LOW2 -99999 // low val for rand float
#define HIGH2 99999 // high val for rand float
#define PREC 2 // no of digits after dec pt
#define LSIZE 12 // no of vals printed on line
#define ITEM_W 7 // no of spaces for each item
// prints single val
template < class T > void print(const T&);
// prints data vals in tree
template < class T > void print_vals(binTree < T >&, const string&);
// class to generate rand ints
class RND1 {
private:
int low, high;
public:
RND1(const int& l = 0, const int& h = 1) : low(l), high(h) { }
int operator ( ) () const {
return rand() % (high - low + 1) + low;
}
};
// class to generate rand floats
class RND2 {
private:
int low, high, prec;
public:
RND2(const int& l = 0, const int& h = 1, const int& p = 0) :
low(l), high(h), prec(p) { }
float operator ( ) () const {
return (static_cast < float >
(rand() % (high - low + 1) + low)) / pow(10, prec);
}
};
// prints out val passed as argument
template < class T >
void print(const T& x) {
static unsigned cnt = 0;
cout << setw(ITEM_W) << x << ' '; cnt++;
if (cnt % LSIZE == 0) cout << endl;
}
// prints out height of bin tree and data val in each node in inorder
template < class T >
void print_vals(binTree < T >& tree, const string& name)
{
cout << name << ": "; // print name of tree
// print height of tree
cout << "height of tree = " << tree.height() << endl << endl;
// print data values of tree in inorder
cout << "Data values in '" << name << "' inorder: ";
tree.inorder(print); cout << endl;
}
// driver program: to test several member functions
// of bin tree class
int main()
{
srand(SEED); // set seed for RNGs
// create 1st vector and fill it with rand ints
vector < int > A(N1);
generate(A.begin(), A.end(), RND1(LOW1, HIGH1));
// create bin tree with int vals in vector A,
// and print all vals of tree
binTree < int > first;
for (unsigned i = 0; i < A.size(); i++)
first.insert(A[i]);
print_vals(first, "first"); cout << endl;
// create 2nd vector and fill it with rand floats
vector < float > B(N2);
generate(B.begin(), B.end(), RND2(LOW2, HIGH2, PREC));
// create bin tree with float-pt vals in vector B,
// and print all vals of tree
binTree < float > second;
for (unsigned i = 0; i < B.size(); i++)
second.insert(B[i]);
print_vals(second, "second"); cout << endl;
return 0;
}
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