创建及删除二叉树

#include<iostream>
using namespace std;


class BiNode{
private:
BiNode *LChild, *RChild;
int Data;
public:
BiNode(): LChild(NULL),RChild(NULL),Data(-1){}
BiNode(BiNode* l,BiNode* r,int d): LChild(l),RChild(r),Data(d){}
BiNode(int d): LChild(NULL),RChild(NULL),Data(d) {}
BiNode* Get_L() {return LChild;}
BiNode* Get_R() {return RChild;}
int Get_D() {return Data;}
void Change (BiNode* l, BiNode* r,int d){LChild = l,RChild = r, Data = d;}
void Change_L(BiNode* l) {LChild = l;}
void Change_R(BiNode* r) {RChild = r;}
void Change_D(int d) {Data = d;}
};
BiNode* CreateTree(BiNode* T)
{
int D = -1;
cout << " please cin the number " << endl;
cin >> D;
if(-1 == D)
{T = NULL;  return T;}
else
{
if( !(T = new BiNode(D)))
return T;
else
{
T->Change_L(CreateTree(T->Get_L() ) );
T->Change_R(CreateTree(T->Get_R() ) );
return T;
}
}
}
void DeleteTree(BiNode* T)
{
if(NULL == T)
{return;}
else 
{
if(T->Get_L() != NULL)
DeleteTree(T->Get_L() );
if(T->Get_R() != NULL)
DeleteTree(T->Get_R());
cout << T->Get_D() << endl;
delete T;
return;
}
}
void PreTraverse(BiNode *T)
{
if(NULL == T)
return;
else
{
cout << T->Get_D() << endl;
PreTraverse(T->Get_L());
PreTraverse(T->Get_R());
return;
}
}


int main()
{
BiNode* BT = NULL;
cout << "begin Create the tree " << endl;
BT = CreateTree(BT);
PreTraverse(BT);
cout << " begin delete the tree " << endl;
DeleteTree(BT);
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值