Question
Debug the following code. Refer to the main to see what it needs to output: main.cpp: ------------------------------------------------------------------ #include p4.h using namespace std; void doWork(iBST &myTree)
Debug the following code. Refer to the main to see what it needs to output:
main.cpp:
------------------------------------------------------------------
#include "p4.h"
using namespace std;
void doWork(iBST &myTree) {
int i, j; int val; int deleteCount; bool rc;
cout << "Do Work - START ******************** ";
while (cin >> val) { if (!myTree.insertV(val)) { cout << "Val not inserted: " << val << endl; } } cout << "Tree count is " << myTree.count() << endl; cout << "Tree contents are: "; myTree.printIt();
deleteCount = 0; for (i = 25; i >=0; i--) { if (myTree.deleteV(i)) { deleteCount++; } }
cout << " Delete Count is " << deleteCount << endl; cout << "Tree count is " << myTree.count() << endl; cout << "Tree contents are: "; myTree.printIt();
cout << endl;
for (i = 0; i < 100; i += 2) { if (myTree.isIn(i)) { cout << "isIn reports this is in the list: " << i << endl; } }
myTree.clear();
cout << " The cleared tree status: count = " << myTree.count() < cout << "Do Work - END ******************** "; } int main(int argc, char *argv[]) { iBST theTree; doWork(theTree); } ----------------------------------------------------------------------- p4.h ----------------------------------------------------------------------- #ifndef P4_H #define P4_H class iBST { private: class iNode { public: int key; iNode *left; iNode *right; }; iNode *root; int tCount; public: findMin(iNode *ptr); //Finds the minimm value in the tree to which ptr points. iBST(); // Constructor ~iBST(); // Deconstructor bool insertV(int v); // Inserts v into the tree bool deleteV(int v); // Removes the node with value v. bool isIn(int v); // returns true if the node with value v is in the tree. void printIt(); // Returns BST values being printed in ascending order. int count(); // Returns the number of nodes in the tree void clear(); // Removes all nodes from the BST, making BST empty void clear(iNode * temp); }; #endif ----------------------------------------------------------------------- p4.cpp ----------------------------------------------------------------------- #include iBST::iBST() { //COMPILES. root = NULL; } iBST::~iBST() { //COMPILES. delete(root); } bool iBST::insertV(int v){ // COMPILES AS LONG AS isIn is done. bool rc; if (root==NULL) { iNode * temp; temp -> key = v; temp -> left = temp -> right = NULL; rc = true; } if (isIn(v)) { // Complains when isIn is commented. rc = false; } while (v < root -> key) { iNode *temp; temp -> left; while (v > root -> key) { temp -> right; } } iNode * temp; temp -> key = v; temp -> left = temp -> right = NULL; return rc; } bool iBST::deleteV(int v) { //COMPILES. bool rc = false; if(root == NULL || root -> key == v) { delete root; rc = true; } while (root -> key < v) { root = root -> right; if (root -> key == v) { delete root; rc = true; } } return rc; } bool iBST::isIn(int v) { // NOT TESTED bool rc; if(root == NULL || root -> key == v) { rc = true; } while (root -> key < key) { // DOESNT LIKE SECOND KEY root = root -> right; } while(root -> key > key) { // DOESNT LIKE SECOND KEY root = root -> left; if(root -> key == v){ rc = true; } rc = false; } return rc; } void iBST::printIt() { // COMPILES MINUS ONE LINE. TWO ERRORS. findMin(iNode *ptr) { // Can't find ptr int rc = 0; if(ptr == NULL) { ; }else if (ptr -> left == NULL) { rc = ptr; }else { rc = findMin(ptr -> left); // This is messed up. Cant find *ptr } return rc; } } int iBST::count() { // COMPILES. return tCount; } void iBST::clear(iNode *temp) { // COMPILES. if (temp != NULL) { clear(temp -> left); clear(temp -> right); delete temp; } } void iBST::clear() { // COMPILES. clear(root); tCount = 0; } ------------------------------------------------------------------------ ----------------------------------------------------------------------- Output: ----------------------------------------------------------------------- The issues are either in the .h or the .cpp.Input: 10 50 20 52 30 5 15 22 60 45 48 33 17 22 20 15 44
Do Work - START ******************** Val not inserted: 22 Val not inserted: 20 Val not inserted: 15 Tree count is 14 Tree contents are: 5 10 15 17 20 22 30 33 44 45 48 50 52 60 Delete Count is 6 Tree count is 8 Tree contents are: 30 33 44 45 48 50 52 60 isIn reports this is in the list: 30 isIn reports this is in the list: 44 isIn reports this is in the list: 48 isIn reports this is in the list: 50 isIn reports this is in the list: 52 isIn reports this is in the list: 60 The cleared tree status: count = 0 The cleared tree contents follow: Do Work - END ********************
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