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 classes to represent a binary tree (of integers) using Microsoft visual .

For this computer assignment, you are to write a C++ program to implement classes to represent a binary tree (of integers) using Microsoft visual. The definition of the binary tree class is given here to facilitate the following description:All of 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.

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.

size(Node* r) const : This function retur ns the number of nodes in the tree rooted at r . If the tree is empty, the size is 0.

height(Node* r) const : This function returns the height of the tree rooted at r . If the tree is empty, the size is 1

Inorder ( Node* r, void( * p)(int) ) : This function traverse the tree rooted at r . p is the visit operation on each node . To visit r , simply invoke p(r- >data) .

#include #include #include #include

using namespace std;

class binTree; class BST;

class Node {

};

class binTree { public: binTree(); // default constructor virtual void insert(int); int height() const;// returns height of tree unsigned size() const; // return size of tree void inorder(void(*)(int));// inorder traversal of tree void preorder(void(*)(int));// preorder traversal void postorder(void(*)(int));// postorder traversal

protected: Node* root;// root of tree

private: void insert(Node*&, int);// private version of height() int height(Node*) const;// privvate version of size() unsigned size(Node*) const;// private version of insert() void inorder(Node*, void(*)(int));// private version of inorder void preorder(Node*, void(*)(int));// prvate version of preorder void postorder(Node*, void(*)(int));//private version o postorder };

const int MAX_SIZE = 40; const int MAX_COUNT = 40; const int WIDTH = 5; const int ROW_SIZE = 8;

int mcount = 0; int rcount = 0;

void display(int d) { if (mcount < MAX_COUNT) { cout << setw(WIDTH) << d; mcount++; rcount++; if (rcount == ROW_SIZE) { cout << endl; rcount = 0; } } }

#define BINTREE_MAIN #ifdef BINTREE_MAIN int main() { vector v(MAX_SIZE); for (int i = 1; i

binTree bt; vector::iterator it; for (it = v.begin(); it != v.end(); it++) bt.insert(*it);

cout << "Height: " << bt.height() << endl; cout << "Size: " << bt.size() << endl; cout << "In order traverse (displaying first " << MAX_COUNT << " numbers): " << endl; mcount = rcount = 0; bt.inorder(display); cout << " Pre order traverse (displaying first " << MAX_COUNT << " numbers): " << endl; mcount = rcount = 0; bt.preorder(display); cout << " Post order traverse (displaying first " << MAX_COUNT << " numbers): " << endl; mcount = rcount = 0; bt.postorder(display);

cout << endl; return 0; }

output - output- output

Height: 6 Size: 40 In order traverse (displaying first 40 numbers): 13 8 39 38 17 30 32 15 1 18 33 10 28 6 25 31 27 35 20 23 4 14 0 22 26 9 12 24 34 7 2 29 11 16 21 19 3 36 37 5

Pre order traverse (displaying first 40 numbers): 4 28 15 17 39 8 13 38 32 30 33 18 1 10 35 31 25 6 27 23 20 11 24 26 0 14 22 12 9 2 7 34 29 36 19 21 16 3 5 37

Post order traverse (displaying first 40 numbers): 13 8 38 39 30 32 17 1 18 10 33 15 6 25 27 31 20 23 35 28 14 22 0 9 12 26 34 7 29 2 24 16 21 3 19 37 5 36 11 4

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

Students also viewed these Databases questions

Question

Define marketing concepts.

Answered: 1 week ago