template <class T>
struct BTnode
{
T d;
BTnode *LTree;
BTnode *RTree;
};
template <class T>
class BinaryTree
{
private:
BTnode<T> *BT;
public:
BinaryTree(){ BT = NULL; }
~BinaryTree();
void CreateBT(T end);
void PreTravBT();
};
template <class T>
void BinaryTree<T>::CreateBT(T end)
{
cout<<"please input any value end by -1"<<endl;
T x;
cin>>x;
if(x==end)return;
BTnode<T> *p;
p = new BTnode<T>;
p->d = x;
p->LTree = NULL;
p->RTree = NULL;
BT = p;
create_bt(p,1,end);
create_bt(p,2,end);
cout<<"creae is success"<<endl;
}
template <class T>
BinaryTree<T>::~BinaryTree()
{
BTnode<T> *p;
p = BT;
delete_bt(p);
cout<<"delete is success"<<endl;
}
template <class T>
static void delete_bt(BTnode<T> *p)
{
if(p==NULL)return;
delete_bt(p->LTree);
delete_bt(p->RTree);
delete [] p;
}
template <class T>
static void create_bt(BTnode<T> *p,int k,T end)
{
T x;
cin>>x;
if(x==end)return;
BTnode<T> *q;
q = new BTnode<T>;
q->d = x;
q->LTree = NULL;
q->RTree = NULL;
if(k==1)
{
p->LTree = q;
};
if(k==2)
{
p->RTree = q;
};
create_bt(q,1,end);
create_bt(q,2,end);
return;
}
template <class T>
void BinaryTree<T>::PreTravBT()
{
BTnode<T> *p;
p = BT;
pretrav_bt(p);
cout<<"pretrav_bt is success"<<endl;
}
template <class T>
static void pretrav_bt(BTnode<T> *p)
{
if(p==NULL)return;
cout<<p->d<<'\t';
pretrav_bt(p->LTree);
pretrav_bt(p->RTree);
cout<<endl;
return;
}
不知道几年前写的