- 论坛徽章:
- 0
|
这是一个模板类,实现最简单的二叉查找树,报错如下:
bst.cpp: In member function ‘void BinarySearchTree<T>::makeEmpty() const [with T = char]’:
bst.cpp:19: instantiated from ‘BinarySearchTree<T>::~BinarySearchTree() [with T = char]’
bst.cpp:154: instantiated from here
bst.cpp:29: error: no matching function for call to ‘BinarySearchTree<char>::makeEmpty(BinarySearchTree<char>::BinaryNode* const&) const’
bst.cpp:29: note: candidates are: void BinarySearchTree<T>::makeEmpty() const [with T = char]
bst.cpp:129: note: void BinarySearchTree<T>::makeEmpty(BinarySearchTree<T>::BinaryNode*&) const [with T = char]
代码- #include <iostream>
- #include <string>
- template <class T>
- class BinarySearchTree
- {
- public:
- BinarySearchTree () : root (NULL) { }
- BinarySearchTree (const BinarySearchTree &rhs) { root = clone (rhs.root); }
- const BinarySearchTree &operator= (const BinarySearchTree &rhs)
- {
- if (this != &rhs)
- {
- makeEmpty ();
- root = clone (rhs.root);
- }
- return *this;
- }
- ~BinarySearchTree () { makeEmpty (); }
- void insert (const T &x) { insert (x, root); }
- void remove (const T &x) { remove (x, root); }
- const T &findMax () const { findMax (root); }
- const T &findMin () const { findMin (root); }
- bool contains (const T &x) const { return container (x, root); }
- bool isEmpty () const { return (root != NULL); }
- void makeEmpty () const { makeEmpty (root); }
- void printTree () const { printTree (root); }
-
- private:
- struct BinaryNode
- {
- T data;
- BinaryNode *left;
- BinaryNode *right;
- BinaryNode (const T &elem, BinaryNode *lt, BinaryNode *rt) : data (elem), left (0), right (0) { }
- };
- BinaryNode *root;
- void insert (const T &x, BinaryNode *&t) const;
- void remove (const T &x, BinaryNode *&t) const;
- BinaryNode *findMax (BinaryNode *t) const;
- /*
- {
- if (t)
- while (t->right)
- t = t->right;
- return t;
- }*/
- BinaryNode *findMin (BinaryNode *t) const
- {
- if (t == NULL)
- return NULL;
- if (t->left == NULL)
- return NULL;
- return findMin (t->left);
- }
- BinaryNode *clone (BinaryNode *t) const
- {
- if (t == NULL)
- return NULL;
- return new BinaryNode (t->data, clone(t->left), clone(t->right));
- }
- bool contains (const T &x, BinaryNode *t) const;
- void makeEmpty (BinaryNode *&t) const;
- void printTree (BinaryNode *t) const;
- };
- template <class T>
- void BinarySearchTree<T>::insert (const T &x, BinaryNode *&t) const
- {
- if (t == NULL)
- t = new BinaryNode (x, NULL, NULL);
- else if (x < t->data)
- insert (x, t->left);
- else if (x > t->data)
- insert (x, t->right);
- else
- ;//same value , do nothing
- }
- template <class T>
- void BinarySearchTree<T>::remove (const T &x, BinaryNode *&t) const
- {
- if (t == NULL)
- return;
- if (x < t->data)
- remove (x, t->left);
- else if (x > t->data)
- remove (x, t->right);
- else if (t->left && t->right) //Two children
- {
- t->data = findMin (t->right)->data;
- remove(t->data, t->right);
- }
- else
- {
- BinaryNode *oldNode = t;
- t = (t->left) ? t->left : t->right;
- delete t;
- }
- }
- template <class T>
- struct BinarySearchTree<T>::BinaryNode *BinarySearchTree<T>::findMax (BinaryNode *t) const
- {
- if (t)
- while (t->right)
- t=t->right;
- return t;
- }
- template <class T>
- bool BinarySearchTree<T>::contains (const T &x, BinaryNode *t) const
- {
- if (t == NULL)
- return false;
- else if (x < t->data)
- return contains (x, t->left);
- else if (x > t->data)
- return contains (x, t->right);
- return true;
- }
- template <class T>
- void BinarySearchTree<T>::makeEmpty (BinaryNode *&t) const
- {
- if (t != NULL)
- {
- makeEmpty (t->left);
- makeEmpty (t->right);
- delete t;
- }
- t = NULL; //
- }
- template <class T>
- void BinarySearchTree<T>::printTree (BinaryNode *t) const
- {
- std::cout << "LNR" << std::endl;
- if (t != NULL)
- {
- printTree (t->left);
- std::cout << " " << t->data;
- printTree (t->right);
- }
- }
- int main ()
- {
- BinarySearchTree<char> bst;
- bst.insert ('F');
- bst.insert ('B');
- bst.insert ('G');
- bst.insert ('A');
- bst.insert ('D');
- bst.insert ('I');
- bst.insert ('C');
- bst.insert ('E');
- bst.insert ('H');
- //bst.isEmpty ();
- bst.printTree ();
- return 0;
- }
复制代码 |
|