Question
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,
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 a 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
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 STL, where N1 = 100 and N2 = 50 are the corresponding 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
Here is my code, just getting wrong output, please help
Node.h
#ifndef NODE_H #define NODE_H
template
template
private: T nodeContent; Node
// default constructor template
#endif
binTree.h
#ifndef BINTREE_H #define BINTREE_H
#include "Node.h"
template
binTree ( ); int height ( ) const; virtual void insert ( const T& ); void inOrder ( void ( * ) ( T& )); protected: Node
//function definitions template
// returns height of tree template
//to insert the node in the binary tree //recursive function template
//To perform inorder traversal of the tree template
// private version of height template
template
//inorder traversa template
#endif
prog6.cpp
#include
using namespace std; class RND1{ int x; public: static int RND(){ srand(time(NULL)); return (-999+ (rand() % (static_cast
};
class RND2{ float randm; public: static float RND() { srand(time(NULL)); return -999.99 + static_cast
};
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