Question
For this computer assignment, implement a derived class to represent a binary search tree. Since a binary search tree is also a binary tree, implement
For this computer assignment, implement a derived class to represent a binary search tree. Since a binary search tree is also a binary tree, implement your binary search tree class from the base class of binary trees, which you implemented in your previous assignment.
assignment6.h
#ifndef ASSIGNMENT6 #define ASSIGNMENT6
class BST : public binTree { public: BST() : binTree() {} void insert( int ); bool search( int ); bool remove( int ); int sumLeftLeaves(); private: void insert( Node*&, int ); bool search( Node*&, int ); bool remove( Node*&, int ); int sumLeftLeaves(Node*&); };
#endif
assignment6main.cc
#include
static const int MAX_COUNT = 20; static int mcount = 0;
void display2(int d) { if ( mcount < MAX_COUNT ) { cout << setw(4) << d; mcount++; } }
// produce a random number within range [1 1000] int rand_1000() { return rand() % 1000 + 1; }
void show_tree_information( BST& bst ) { cout << "Size of the tree: " << bst.size() << endl; cout << "Height of the tree: " << bst.height() << endl; cout << "In order traverse (displaying first " << MAX_COUNT << " numbers): " << endl; mcount = 0; bst.inorder( display2 ); cout << " Pre order traverse (displaying first " << MAX_COUNT << " numbers): " << endl; mcount = 0; bst.preorder( display2 ); cout << " Post order traverse (displaying first " << MAX_COUNT << " numbers): " << endl; mcount = 0; bst.postorder( display2 ); }
// For each value in searchVec, search it in the binary search tree // and report the number of successful searches void run_search( BST& bst, vector
int main() { //-------------- Create a B.S.T. using unique random numbers ----------- vector
BST bst; for (it=inputVec.begin(); it!=inputVec.end(); it++) bst.insert( *it ); cout << "A binary search tree is generated with random numbers: " << endl; show_tree_information( bst );
//-------------- Create a vector of random integers to be searched ------ vector
cout << endl << endl << "Sum of left leaves: " << bst.sumLeftLeaves() << endl;
//------------ test search operations ---------------- run_search( bst, searchVec );
//------------ remove half of nodes from the tree --------- int counter = 0; random_shuffle( inputVec.begin(), inputVec.end() ); for ( it = inputVec.begin(); it < inputVec.end(); it += 2 ) { if ( bst.remove( *it ) ) counter++; } cout << endl << counter << " nodes are removed." << endl << endl; show_tree_information( bst );
cout << endl << endl << "Sum of left leaves: " << bst.sumLeftLeaves() << endl;
//-------------- test search operations again --------------- run_search( bst, searchVec );
return 0;
}
The definition of the derived binary search tree class is given here to facilitate the following description:
class BST : public binTree {
public:
BST() : binTree() { } // constructor
void insert( int ); // insert an item in the tree
bool search( int ); // search an item in the tree
bool remove( int ); // remove an item in the tree // returns true when successful
int sumLeftLeaves(); // return the sum of values of left leaves
private: void insert( Node*&, int ); // private version of insert(int)
bool search( Node*&, int ); // private version of search(int)
bool remove( Node*&, int ); // private version of remove(int)
int sumLeftLeaves( Node*& n); // private version of sumLeftLeaves
};
The above public functions simply call their private versions. The private versions of insert (), remove (), search() and sumLeftLeaves () functions can be implemented as recursive functions. You can assume there are no identical numbers to be inserted into the tree
When you implement the remove() function, consider three possible cases: the node to be removed (1) is a leaf; (2) has only one child; and (3) has two children. In the first case, simply delete the node. In the second case, bypass the node making a new link from its parent to its child, and delete the node. In the third case, find the immediate predecessor of the node first move to the left child of the node, and then move to rightmost node in the sub-tree, replace the value of the node with its immediate predecessor, and delete the predecessor.
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