typedef struct Tree
{
char data;
struct tree* lchild;
struct tree* rchild;
}tree;
void inittree(tree* root)
{
root->data = "";
root->rchild = NULL;
root->lchild = NULL;
}
tree* create(tree* root)
{
char value;
scanf_s("%c", &value);
if (value == '#')
{
root = NULL;
}
else
{
root = (tree*)malloc(sizeof(tree));
root->data = value;
root->lchild = create(root->lchild);
root->rchild = create(root->rchild);
}
return root;
}
void createnode(tree** node)
{
char p_data;
scanf_s("%c", &p_data);
if (p_data == '#')
{
*node = NULL;
}
else
{
*node = (tree*)malloc(sizeof(tree));
(*node)->data = p_data;
createnode(&((*node)->lchild));
createnode(&((*node)->rchild));
}
}
int tempty(tree* root)
{
if (root->data == "")
{
printf("二叉树为空\n");
return 0;
}
else
{
printf("二叉树不为空\n");
return 1;
}
}
void porder(tree* root)
{
if (root != NULL)
{
printf("%c\t", root->data);
porder(root->lchild);
porder(root->rchild);
}
}
void morder(tree* root)
{
if (root != NULL)
{
morder(root->lchild);
printf("%c\t", root->data);
morder(root->rchild);
}
}
void torder(tree* root)
{
if (root != NULL)
{
torder(root->lchild);
torder(root->rchild);
printf("%c\t", root->data);
}
}
void insertBinarySortTreeNode(Tree *dataNode, Tree **root)
{
if (*root == NULL || dataNode == NULL)
{
*root = dataNode;
return;
}
if (dataNode->data > (*root)->data)
{
if ((*root)->right == NULL)
{
(*root)->right = dataNode;
return;
}
if ((*root)->right)
insertBinarySortTreeNode(dataNode, &(*root)->right);
}
if (dataNode->data <= (*root)->data)
{
if ((*root)->left == NULL)
{
(*root)->left = dataNode;
return;
}
if ((*root)->left)
insertBinarySortTreeNode(dataNode, &(*root)->left);
}
}
void Deleteer(tree* root,int key)
{
Tree *L,*LL;
Tree *p=root;
Tree *parent=root;
int child=0;
if(!root)
return ;
while(p)
{
if(p->data==key)
{
if(!p->lchild&&!p->rchild)
{
if(p==root)
free(p);
else if(child==0)
{
parent->lchild=NULL;
free(p);
}
else
{
parent->rchild=NULL;
free(p);
}
}
else if(!p->lchild)
{
if(child==0)
parent->lchild=p->rchild;
else
parent->rchild=p->rchild;
free(p);
}
else if(!p->rchild)
{
if(child==0)
parent->lchild=p->lchild;
else
parent->rchild=p->lchild;
free(p);
}
else
{
LL=p;
L=p->rchild;
if(L->lchild)
{
LL=L;
L=L->lchild;
p->data=L->data;
LL->lchild=L->lchild;
for(;L->lchild;L=L->lchild);
L->lchild=p->lchild;
p->lchild=NULL;
}
else
{
p->data=L->data;
LL->rchild=L->rchild;
}
}
p=NULL;
}
else if(key<p->data)
{
child=0;
parent=p;
p=p->lchild;
}
else
{
child=1;
parent=p;
p=p->rchild;
}
}
}
int main()
{
tree* root = (tree*)malloc(sizeof(tree));
inittree(root);
printf("请按先序遍历的次序输入二叉树:\n");
root = create(root);
printf("二叉树先序遍历\n");
porder(root);
printf("二叉树中序遍历\n");
morder(root);
printf("二叉树后序遍历\n");
torder(root);
int n;
printf("请输入待插入的数据\n");
scanf("%d",&n);
dataNode = (tree*)malloc(sizeof(tree));
inittree(dataNode);
dataNode->data=n;
insertBinarySortTreeNode(dataNode, &root);
printf("插入后的二叉树为:\n");
porder(root);
printf(" **将数据5从二叉树中删除**\n\n");
printf("删除后的二叉树为:\n");
Deleteer(root,5);
porder(root);
printf("\n");
return 0;