c++ - Binary Search Tree -
i'm working on school project. assignment write implementation of templated binary search tree given corresponding header file.
my issue since it's templated, i'm not sure how comparisons between data items when inserting or finding.
here header file:
template < typename datatype, class keytype > // datatype : tree data item class bstree // keytype : key field { public: // constructor bstree (); // default constructor bstree ( const bstree<datatype,keytype>& other ); // copy constructor bstree& operator= ( const bstree<datatype,keytype>& other ); // overloaded assignment operator // destructor ~bstree (); // binary search tree manipulation operations void insert ( const datatype& newdataitem ); // insert data item bool retrieve ( const keytype& searchkey, datatype& searchdataitem ) const; // retrieve data item bool remove ( const keytype& deletekey ); // remove data item void writekeys () const; // output keys void clear (); // clear tree // binary search tree status operations bool isempty () const; // tree empty // !! isfull() has been retired. not useful in linked structure. // output tree structure -- used in testing/debugging void showstructure () const; // in-lab operations int getheight () const; // height of tree int getcount () const; // number of nodes in tree void writelessthan ( const keytype& searchkey ) const; // output keys < searchkey protected: class bstreenode // inner class: facilitator bstree class { public: // constructor bstreenode ( const datatype &nodedataitem, bstreenode *leftptr, bstreenode *rightptr ); // data members datatype dataitem; // binary search tree data item bstreenode *left, // pointer left child *right; // pointer right child }; // recursive helpers public member functions -- insert // prototypes of these functions here. void showhelper ( bstreenode *p, int level ) const; // data member bstreenode *root; // pointer root node };
the function in question insert().
i have following code:
template <typename datatype, class keytype> void bstree< datatype, keytype>::insert(const datatype& newdataitem) { if(isempty()) { root = new bstreenode(newdataitem, null, null); } else { bstreenode *ptr = root; while(ptr != null) { if((*ptr)>dataitem > newdataitem) { ptr = ptr->left; } else if((*ptr).dataitem < newdataitem) { ptr = ptr->right; } } ptr = new bstreenode(newdataitem, null, null); } }
and receive following error:
44 e:\school\302\labs\7\bstree.cpp no match 'operator>' in 'ptr->bstree<testdata, int>::bstreenode::dataitem > newdataitem' 49 e:\school\302\labs\7\bstree.cpp no match 'operator<' in 'ptr->bstree<testdata, int>::bstreenode::dataitem < newdataitem'
how deal this? need write overloaded operator, , if so, how begin?
this insert called:
class testdata { public: void setkey ( int newkey ) { keyfield = newkey; } // set key int getkey () const { return keyfield; } // returns key private: int keyfield; // key data item }; int main() { bstree<testdata,int> testtree; // test binary search tree testdata testdata; // binary search tree data item int inputkey; // user input key char cmd; // input command print_help(); { testtree.showstructure(); // output tree cout << endl << "command: "; // read command cin >> cmd; if ( cmd == '+' || cmd == '?' || cmd == '-' || cmd == '<' ) cin >> inputkey; switch ( cmd ) { case 'p' : case 'p' : print_help(); break; case '+' : // insert testdata.setkey(inputkey); cout << "insert : key = " << testdata.getkey() << endl; testtree.insert(testdata); break; }
i didn't include rest of switch because wasn't needed example.
i'm assuming issue because i'm comparing 2 different datatypes or attempting too. how around this?
i solved problem following code:
template <typename datatype, class keytype> void bstree< datatype, keytype>::insert(const datatype& newdataitem) { bstreenode* temp = new bstreenode(newdataitem, null, null); if(isempty()) { root = temp; } else { bstreenode* ptr = root; while(ptr != null) { if(ptr > temp) { ptr = ptr->left; } else { ptr = ptr->right; } } ptr = temp; } }
and
template <typename datatype, class keytype> bool bstree<datatype, keytype>::bstreenode::operator>(const bstreenode*& other) { if((*other).dataitem < (*this).dataitem) { return true; } return false; }
i assuming meant add -> after *ptr , not > after
if((*ptr)>dataitem > newdataitem)
though still incorrect. couple of suggestions:
- you not handling condition when data items equal. easy fix replace else-if else.
- (*ptr).dataitem should replaced ptr->dataitem
still, error looks need overload < , > operators data type. please update post actual calling code?
Comments
Post a Comment