感谢!下面是我所有的代码,每个开头都标有文件名字,现在是测试BTnode模板实例化就出现了问题。
main.cpp#include <iostream>
#include "BinaryTree.h"
#include "BTnode.h"
#include <string.h>
using namespace std;
template <typename elemType>
ostream& operator << (ostream &os, BinaryTree<elemType> &bt)
{
return bt.print(os);
}
int main()
{
BTnode<int> bt(3);
return 0;
}
BTnode.h#ifndef BTNODE_H_INCLUDED
#define BTNODE_H_INCLUDED
template <typename Type>
class BinaryTree;
template <typename valType>
class BTnode
{
public:
friend class BinaryTree<valType>;
BTnode(const valType &val);
void insert_value( const valType &val);
void lchild_leaf(BTnode<valType> *leaf,BTnode<valType> *subtree );
void remove_value(valType &val , BTnode<valType> *prev);
private:
valType _val;
int _cnt;
BTnode* _lchild;
BTnode* _rchild;
};
#endif // BTNODE_H_INCLUDED
BTnode.cpp#include "BTnode.h"
template <typename valType>
inline BTnode<valType>::
BTnode(const valType &val):_val(val)
{
_cnt=1;
_lchild=_rchild=0;
}
template <typename valType>
inline void BTnode<valType>::
insert_value ( const valType &val )
{
if(_val == val )
_cnt++;
if(_val < val)
{
if(! _lchild)
_lchild = new BTnode<valType>(val);
else
_lchild->insert_value(val);
}
if(_val > val)
{
if(! _rchild)
_rchild = new BTnode<valType>(val);
else
_rchild->insert_value(val);
}
}
template <typename valType>
inline void BTnode<valType>::
lchild_leaf(BTnode<valType> *leaf,BTnode<valType> *subtree )
{
//找到没有左孩子的结点
while (! subtree->_lchild )
subtree = subtree->_lchild;
//找到了 subtree所指结点没有左子树
subtree->_lchild = leaf;
}
template <typename valType>
inline void BTnode<valType>::
remove_value(valType &val , BTnode<valType> *prev)
{
if (val < _val)
{
if( !_lchild )
return ;
else
{
_lchild->remove_value(val,_lchild);
}
}
else
if (val > _val)
{
if( !_rchild )
return ;
else
{
_rchild->remove_value(val,_rchild);
}
}
else
{
if(_rchild)
{
prev = _rchild;
if(_lchild)
{
if(!prev -> _lchild )
prev->_lchild =_lchild;
else
[quote] BTnode<valType>::lchild_leaf(_lchild,prev->_lchild);
}
}
else
prev = _lchild;
delete this;
}
}
BinaryTree.h#ifndef BINARYTREE_H_INCLUDED
#define BINARYTREE_H_INCLUDED
#include "BTnode.h"
#include <iostream>
using namespace std;
template <typename elemType>
class BinaryTree
{
public:
BinaryTree() ;
BinaryTree(const BinaryTree& );
~BinaryTree() ;
BinaryTree& operator = (const BinaryTree& );
ostream& print(ostream& os) const ; //中序遍历
void insert(const elemType &elem);
void remove( const elemType &elem);
void remove_root( const elemType &elem);
bool empty() {return _root;}
void clear();
private:
void clear(BTnode<elemType> *pt);
BTnode<elemType>* _root;
void copy(BTnode<elemType> *tar,BTnode<elemType> *src);
};
#endif // BINARYTREE_H_INCLUDED
BinaryTree.cpp#include "BinaryTree.h"
template <typename elemType>
inline BinaryTree<elemType>::BinaryTree() :_root(0) {}
template <typename elemType>
inline BinaryTree<elemType>::BinaryTree(const BinaryTree &rhs)
{
copy(_root,rhs._root);
}
template <typename elemType>
inline BinaryTree<elemType>::~BinaryTree()
{
clear();
}
template <typename elemType>
inline BinaryTree<elemType>&
BinaryTree<elemType>::
operator = (const BinaryTree &rhs)
{
if(this != rhs)
clear();
copy(_root,rhs._root);
return *this;
}
template <typename elemType>
inline void BinaryTree<elemType>::
insert(const elemType &elem)
{
if( ! _root )
_root = new BTnode<elemType>(elem);
else
_root->inset_value(elem);
}
template <typename elemType>
inline void BinaryTree<elemType>::
remove(const elemType &elem)
{
if (_root)
{
if(_root->_val == elem)
remove_root(); //删除根节点
else
_root->remove_value(elem , _root); // 删除_root二叉树中的值为elem的结点
}
}
template <typename elemType>
inline void BinaryTree<elemType>::
remove_root (const elemType &elem)
{
BTnode<elemType> *p = _root; //指向准备删除的根节点
if (! _root)
return ;
if (_root->_rchild)
{
_root = _root->_rchild;
if( _root->_lchild )
{
BTnode<elemType> *lc = p->_lchild;//指向待处理的根节点的左子树
BTnode<elemType> *newlc = _root->_lchild;//新根节点的左子树
if(! newlc)
_root->_rchild = lc;
else
BTnode<elemType>::lchild_leaf(lc ,newlc);
}
}
else
_root = p->_lchild;
delete p;
}
template <typename elemType>
inline void
BinaryTree<elemType>::
clear()
{
if(_root)
{
clear(_root);
_root = 0;
}
}
template<typename elemType>
inline void
BinaryTree<elemType>::
clear(BTnode<elemType> *pt)
{
if(pt)
{
clear(pt->_lchild);
clear(pt->_rchild);
delete(pt);
}
}
template <typename elemType>
inline ostream&
BinaryTree<elemType>::print(ostream& os) const
{
if( _root )
{
os << print( _root->_lchild );
os << _root->_val << ' ';
os << print( _root->_rchild );
}
return os;
}
|