Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 class binTree;

template class Node { friend class binTree ; public: //The node defualt constructor Node ( const T& =T ( ), Node * = 0, Node * = 0 );

private: T nodeContent; Node *leftChild, *childRight; };

// default constructor template Node :: Node( const T& temp, Node * newLeft, Node * newRight ) { nodeContent = temp; leftChild = newLeft; childRight = newRight; }

#endif

binTree.h

#ifndef BINTREE_H #define BINTREE_H

#include "Node.h"

template class binTree { public:

binTree ( ); int height ( ) const; virtual void insert ( const T& ); void inOrder ( void ( * ) ( T& )); protected: Node * root; private: int height ( Node * ) const; void insert ( Node *&, const T& ); void inOrder ( Node *, void ( * ) ( T& )); };

//function definitions template binTree ::binTree( ) { //set the root root = 0; }

// returns height of tree template int binTree ::height( ) const { return height( root ); // call recursive }

//to insert the node in the binary tree //recursive function template void binTree ::insert( const T& temp ) { insert( root, temp ); }

//To perform inorder traversal of the tree template void binTree ::inOrder( void ( *print ) ( T& ) ) { inOrder( root, print ); }

// private version of height template int binTree ::height( Node * ptr ) const { if( ptr == 0 ) { return 0; } else { int heightleft = height( ptr->leftChild ); int heigRight = height( ptr->childRight ); if( heightleft > heigRight ) { return 1 + heightleft; } else { return 1 + heigRight; } } }

template void binTree ::insert( Node * & nod, const T& temp ) { if( nod == 0 ) { Node * newNode; newNode = new Node ( temp ); nod = newNode; } else { int heightleft = height( nod->leftChild ); int heigRight = height( nod->childRight ); if( heightleft <= heigRight ) { insert( nod->leftChild, temp ); } else { insert( nod->childRight, temp ); } } }

//inorder traversa template void binTree ::inOrder( Node * nod, void ( *print ) ( T& ) ) { if( nod != NULL ) { inOrder( nod->leftChild, print ); print( nod->nodeContent ); inOrder( nod->childRight, print ); } }

#endif

prog6.cpp

#include #include #include #include #include #include "binTree.h"

using namespace std; class RND1{ int x; public: static int RND(){ srand(time(NULL)); return (-999+ (rand() % (static_cast(999 +999 + 1)))); srand(time(NULL)); }

};

class RND2{ float randm; public: static float RND() { srand(time(NULL)); return -999.99 + static_cast (rand()) /( static_cast (RAND_MAX/(999.99+999.99))); }

};

template void print(T a) { cout<

int main() { srand(unsigned(time(0))); vector A(100); vector B(50); RND1 obj3=RND1();

std::cout << " In order traversal of int tree: "; binTree tr=binTree ( ); int count=1; generate(A.begin(), A.end(), &RND1::RND); for (auto iv: A) { tr.insert(iv); }

tr.inOrder(&print);

//float tree std::cout << " In order traversal of float tree: "; binTree tr1=binTree ( ); generate(B.begin(), B.end(), &RND2::RND); for (auto iv: B) { tr1.insert(iv); }

tr1.inOrder(&print); //print heights

cout<<" int tree height"<

cout<<" float tree height"<

here is what it should be ouputting

first: height of tree = 10

Data values in 'first' inorder:

826 479 871 -787 139 -319 243 379 919 -337 -329 -621 639 256 -692 -703 596 57 64 -906 -831 657 -955 -617 923 -304 -524 -928 604 218 -80 136 790 978 -925 -660 -541 -706 -190 785 -856 756 191 179 -899 488 176 979 992 -304 -996 -551 -236 -715 471 424 -417 509 -375 -547 79 398 949 -439 -276 293 220 -641 873 -902 804 472 -436 -824 252 111 -822 -460 800 974 559 595 -318 -743 -386 -685 157 446 -38 -155 -985 615 610 -900 727 -957 -277 -429 -741 -418

second: height of tree = 8

Data values in 'second' inorder:

-621.05 -570.48 237.14 975.37 51.42 777.25 -623.73 -965.47 -591.68 546.78 974.32 -691.54 -616.89 840.06 572.52 -313.42 984.63 -563.99 478.9 -909.31 -493.92 318.93 -184.09 -446.25 469.7 957.73 774.68 -974.78 135.85 286.91 -974.2 652.83 605.56 -256.83 -621.87 -892.59 -760.51 -565.89 -726.96 459.03 -41.61 -410.55 -6.97 -60.01 65.96 899.21 -816.94 -148.48 -310.26 236.28

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2021 Workshops Biokdd Iwcfs Mlkgraphs Al Cares Protime Alsys 2021 Virtual Event September 27 30 2021 Proceedings

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Atif Mashkoor ,Johannes Sametinger ,Anna Fensel ,Jorge Martinez-Gil ,Lukas Fischer

1st Edition

3030871002, 978-3030871000

More Books

Students also viewed these Databases questions

Question

7. Understand the challenges of multilingualism.

Answered: 1 week ago