# 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);
}
# 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);
}