#include<stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *Leftchild;
struct Node *rightchild;
}BiTreeNode;
void Destroy(BiTreeNode **root)
{
if(*root!=NULL&&(*root)->Leftchild!=NULL)
Destroy(&(*root)->Leftchild);
if((*root)!=NULL&&(*root)->rightchild!=NULL)
Destroy(&(*root)->rightchild);
free(*root);
}
void Initiate(BiTreeNode **root)
{
*root=(BiTreeNode *)malloc(sizeof(BiTreeNode));
(*root)->Leftchild=NULL;
(*root)->rightchild=NULL;
}
BiTreeNode *InsertLeftNode(BiTreeNode *curr,int x)
{
BiTreeNode *s,*t;
if(curr==NULL)return NULL;
t=curr->Leftchild;
s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data=x;
s->Leftchild=t;
s->rightchild=NULL;
curr->Leftchild=s;
return curr->Leftchild;
}
BiTreeNode *InsertRightNode(BiTreeNode *curr,int x)
{
BiTreeNode *s,*t;
if(curr==NULL)return NULL;
t=curr->rightchild;
s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data=x;
s->rightchild=t;
s->Leftchild=NULL;
curr->rightchild=s;
return curr->rightchild;
}
BiTreeNode *DeleteRightTree(BiTreeNode *curr)
{
if(curr==NULL||curr->rightchild==NULL)return NULL;
Destroy(&curr->rightchild);
return curr;
}
BiTreeNode *DeleteLeftTree(BiTreeNode *curr)
{
if(curr==NULL||curr->Leftchild==NULL)
return NULL;
Destroy(&curr->Leftchild);
return curr;
}
BiTreeNode *CreateBitree()
{
BiTreeNode *s;
int a;
scanf("%d",&a);
if(a==0)
s=NULL;
else{
s=(BiTreeNode *)malloc(sizeof(BiTreeNode));
s->data=a;
s->Leftchild=CreateBitree();
s->rightchild=CreateBitree();}
return s;
}
二叉树的创建
于 2022-03-23 08:04:04 首次发布