二叉树1

# include <stdio.h>
# include <stdlib.h>
typedef char ElemType;
typedef struct BiTNode{
ElemType data;
    struct BiTNode *Lchild;
struct BiTNode *Rchild;
}BiTNode,*BiTree;
/*构造一棵空二叉树*/
int Init(BiTree &T){
if(T=(BiTNode*)malloc(sizeof(BiTNode)))
{
T->Lchild=NULL;
T->Rchild=NULL;
return 1;
}else
return 0;
}
/*销毁一棵二叉树*/
void Destroy(BiTree &T){
if(T){
Destroy(T->Lchild);//销毁左子树
Destroy(T->Rchild);//销毁右子树
free(T);//销毁根节点
T=NULL;
}
return;
}
/*删除二叉树某节点的子树(左子树)*/
void DeleteL(BiTree &T,BiTree pt){
if(pt==NULL||pt->Lchild==NULL)
return;
Destroy(pt->Lchild);//销毁pt左子树
pt->Lchild=NULL;//pt左孩子指针赋空
}
/*在二叉树中插入节点*/
void InsertL(BiTree &T,ElemType e,BiTree pt){
BiTree p;
if(pt){
if(p=(BiTNode*)malloc(sizeof(BiTNode))){
p->data=e;//赋值
//插入新节点
p->Lchild=pt->Lchild;
p->Rchild=pt->Rchild;
pt->Lchild=p;
}
}
}
/*在二叉树中插入节点*/
void InsertR(BiTree &T,ElemType e,BiTree pt){
BiTree p;
if(pt){
if(p=(BiTNode*)malloc(sizeof(BiTNode))){
p->data=e;//赋值
//插入新节点
p->Lchild=pt->Lchild;
p->Rchild=pt->Rchild;
pt->Rchild=p;
}
}
}
void main(){
BiTree T;
printf("create a null tree\n");
Init(T);
printf("success a null tree\n");


printf("输入一个left节点\n");
InsertL(T,'a',T);
printf("\r\n"+T->Lchild->data);


printf("输入一个right节点\n");
InsertR(T,'b',T);
printf("\r\n"+T->Rchild->data);


printf("删除根左子树");
DeleteL(T,T);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值