Question
I would like to get the following code running. How do I fix this error I am getting from my code: error: no matching function
I would like to get the following code running. How do I fix this error I am getting from my code:
error: no matching function for call to 'BST::inOrder(BST::BinNode*&)'
in file BST.cpp, line 101 (inOrder(myRoot->left)
Please supply aide in C++.
This is my code, it is divided into 3 files (BST.h, BST.cpp, BST_tester.cpp):
~~~~~~~~~~~~~~BST.h~~~~~~~~~~~~~~~~~~~~~ #include
#ifndef BINARY_SEARCH_TREE #define BINARY_SEARCH_TREE
class BST { public: /***** Function Members *****/ BST(); bool empty() const; bool search(const int & item) const; void insert(const int & item); void inOrder(); void preOrder(); int nodeCount();
private: /***** Node class *****/ class BinNode { public: int data; BinNode * left; BinNode * right;
// BinNode constructors // Default -- data part is default int value; both links are null. BinNode() : left(0), right(0) {}
// Explicit Value -- data part contains item; both links are null. BinNode(int item) : data(item), left(0), right(0) {}
};// end of class BinNode declaration
/***** Data Members *****/ BinNode * myRoot;
}; // end of class declaration
#endif ~~~~~~~~~~~~~~BST.cpp~~~~~~~~~~~~~~~~~~~~~ #include
using namespace std;
#include "BST.h"
//--- Definition of constructor BST::BST() : myRoot(0) {}
bool BST::empty() const { return myRoot == 0; }
bool BST::search(const int & item) const { BinNode * locptr = myRoot; bool found = false; while (!found && locptr != 0) { if (item < locptr->data) // descend left locptr = locptr->left; else if (locptr->data < item) // descend right locptr = locptr->right; else // item found found = true; } return found; }
void BST::insert(const int & item) { BinNode * locptr = myRoot; // search pointer BinNode * parent = 0; // pointer to parent of current node bool found = false; // indicates if item already in BST while (!found && locptr != 0) { parent = locptr; if (item < locptr->data) // descend left locptr = locptr->left; else if (locptr->data < item) // descend right locptr = locptr->right; else // item found found = true; } if (!found) { // construct node containing item locptr = new BinNode(item); if (parent == 0) // empty tree myRoot = locptr; else if (item < parent->data ) // insert to left of parent parent->left = locptr; else // insert to right of parent parent->right = locptr; } else cout << "Item already in the tree "; }
/* Implement the inorder traversal algorithm of a binary search tree. Your function should display each node data on the screen. LEFT RIGHT ROOT*/ void BST::inOrder() { BinNode * locptr = myRoot; if ( myRoot != NULL) { inOrder(myRoot->left); cout << myRoot->data; inOrder(myRoot->right); } }
/* Implement the preorder traversal algorithm of a binary search tree. Your function should display each node data on the screen.*/ void BST::preOrder() { BinNode * locptr = myRoot; if (myRoot != NULL) { cout << myRoot->data << " "; preOrder(myRoot->left); preOrder(myRoot->data); } } // count the number of nodes in a binary search tree int BST::nodeCount() { int count = 0; BinNode * locptr = myRoot; if ( locptr != NULL) { count = 1 + nodeCount(locptr->left) + nodeCount(locptr->right); } return count; } ~~~~~~~~~~~~~~BST_tester.cpp~~~~~~~~~~~~~~~~~~~~~ /*----- treetester.cpp ----------------------------------------------------- Program for testing BST. ------------------------------------------------------------------------*/ #include
#include "BST.h"
int main() { // Testing Constructor and empty() BST intBST; // test the class constructor cout << "Constructing empty BST "; cout << "BST " << (intBST.empty() ? "is" : "is not") << " empty ";
// Testing insert cout << " Now insert a bunch of integers into the BST." " Try items not in the BST and some that are in it: "; int number; for (;;) { cout << "Item to insert (-999 to stop): "; cin >> number; if (number == -999) break; intBST.insert(number); } cout << "BST " << (intBST.empty() ? "is" : "is not") << " empty "; // inOrder() cout << "BST inOrder(): "; intBST.inOrder(); cout << " ";
// preOrder() cout << "BST preOrder(): "; intBST.preOrder(); cout << " ";
// nodeCount() cout << "BST nodeCount(): "; intBST.nodeCount(); cout << " "; // Testing search() cout << " Now testing the search() operation." " Try both items in the BST and some not in it: "; for (;;) { cout << "Item to find (-999 to stop): "; cin >> number; if (number == -999) break; cout << (intBST.search(number) ? "Found" : "Not found") << endl; }
}
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