二叉树的使用方法及遍历
#include "iostream.h"
template<class T>
class CTree
{
public:
CTree(const T &item, CTree<T>*lp = NULL, CTree<T>*rp = NULL);
CTree<T> *GetLp(void) const;
CTree<T> *GetRp(void) const;
CTree<T> *GetNode(const &item,CTree<T>*lp = NULL, CTree<T>*rp = NULL);
void FreeNode(CTree<T>* p);
public:
T data;
private:
CTree<T> *Left;
CTree<T> *Right;
};
template<class T>
CTree<T>::CTree(const T &item, CTree<T>*lp, CTree<T>*rp) : data(item), Left(lp), Right(rp)
{
}
template<class T>
CTree<T>* CTree<T>::GetLp(void) const
{
return Left;
}
template<class T>
CTree<T>* CTree<T>::GetRp(void) const
{
return Right;
}
template<class T>
CTree<T> *CTree<T>::GetNode(const &item,CTree<T>*lp, CTree<T>*rp)
{
CTree<T>* p = new CTree<T>(item, lp, rp);
ASSERT(p);
return p;
}
template<class T>
void CTree<T>::FreeNode(CTree<T>* p)
{
delete p;
}
void main()
{
CTree<int> *root, *lp, *rp;
lp = new CTree<int>(10);
rp = new CTree<int>(20);
root =new CTree<int>(50,lp,rp);
}