Comparing binary search trees
Nov 24, 2016 at 1:14am UTC
I am trying to compare binary search trees.
one is given but the other i have to make so that it can be compared. what am I missing that isn't allowing the program to run?
I am running into issues. errors posted below.
Error C2678 binary '==': no operator found which takes a left-hand operand of type 'mm::BST<int>' (or there is no acceptable conversion)
why am i getting this comparison error? It's how i'm using right?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
template <class Object>
bool BST<Object>::isMatchingTree(BST<Object> otherTree) const throw (InvalidTreeArgument)
{
BST<Object> ot = otherTree;
bool ist = false ;
if (root == nullptr && ot == nullptr ) // == error
throw InvalidTreeArgument();
//structure and element
if (root != nullptr && ot != nullptr )// =! error
{
//root->getLeftSide() == otherTree->getLeftSide()
if (root->getLeftSide() == ot)// == error
ist = true ;
if (root->getRightSide() == ot)
ist = true ;
else
ist = isMatchingTree(otherTree);
}
return ist;
}
Last edited on Nov 24, 2016 at 1:46am UTC
Nov 24, 2016 at 7:25am UTC
Could you post enough code we can run/compile to reproduce this error? It sounds like you need to overload your '==' operator, but I am not sure.
Last edited on Nov 24, 2016 at 7:26am UTC
Nov 29, 2016 at 4:39am UTC
header file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
template <class Object>
class BST {
public :
BST();
BST( const Object& rootElement );
BST( const BST& rhs );
~BST();
int total() const throw (InvalidTreeArgument);
int total(BSTNode<Object> * node) const ;
bool BST< Object >::isMatchingTree(BST< Object > otherTree) const throw (InvalidTreeArgument);
bool isEmpty() const ;
void makeEmpty();
int size() const ;
int height() const ;
const BST& operator =( const BST& rhs );
const BST<Object>& operator ==(const BST<Object>& rhs);
std::ostream& printBST( std::ostream& outs ) const ;
BSTNode<Object>* getRoot() const ;
private :
typedef BSTNode<Object>* NodePtr;
NodePtr root;
void makeEmpty( NodePtr &t );
friend class BSTIterator<Object>;
friend class BSTNode<Object>;
};
cpp file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
template <class Object>
BST<Object>::BST() {
root = nullptr ;
}
template <class Object>
BST<Object>::BST( const BST<Object>& rhs ) {
root = nullptr ;
*this = rhs;
}
template <class Object>
BST<Object>::BST( const Object& rootElement ) {
root = new BSTNode<Object>( rootElement );
}
template <class Object>
BST<Object>::~BST() {
makeEmpty();
}
template <class Object>
bool BST<Object>::isMatchingTree(BST<Object> otherTree) const throw (InvalidTreeArgument)
{
bool ist = false ;
if (root == nullptr && otherTree == nullptr )
throw InvalidTreeArgument();
//structure and element
if (root != nullptr && ot != nullptr )
{
//root->getLeftSide() == otherTree->getLeftSide()
if (root->getLeftSide()->getElement() == otherTree)
ist = true ;
if (root->getRightSide() == otherTree)
ist = true ;
else
ist = isMatchingTree(otherTree);
}
return ist;
}
template <class Object>
void BST<Object>::makeEmpty() {
makeEmpty( root );
}
template <class Object>
void BST<Object>::makeEmpty( NodePtr & node ) {
if (node != nullptr ) {
NodePtr l = node->getLeftSide();
NodePtr r = node->getRightSide();
makeEmpty( l );
makeEmpty( r );
delete node;
node = nullptr ;
}
}
template <class Object>
int BST<Object>::size() const {
return ( BSTNode<Object>::size( root ) );
}
template <class Object>
int BST<Object>::height() const {
return ( BSTNode<Object>::height( root ) );
}
template <class Object>
std::ostream& BST<Object>::printBST( std::ostream& outs ) const {
if (isEmpty())
outs << "Empty BST" ;
else
root->printBSTNode( outs );
outs << std::endl;
return ( outs );
}
Last edited on Nov 29, 2016 at 4:40am UTC
Topic archived. No new replies allowed.