#include <stdio.h>
#include <stdlib.h>
typedef struct Tree{
char data;
struct tree*lchild;
struct tree*rchild;
int k;
}tree;
tree*creat(tree*root){//创建二叉树
char value;
scanf("%c",&value);
if(value=='#'){
root=NULL;
}
else{
root=(tree*)malloc(sizeof(tree));
root->data=value;
root->k=0;
root->lchild=creat(root->lchild);
root->rchild=creat(root->rchild);
}
return root;
}
void preorder(tree*root){//递归先根遍历
if(root!=NULL){
printf("%c\t",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void inorder(tree*root){//递归中根遍历
if(root!=NULL){
inorder(root->lchild);
printf("%c\t",root->data);
inorder(root->rchild);
}
}
void postorder(tree*root){//递归后跟遍历
if(root!=NULL){
postorder(root->lchild);
postorder(root->rchild);
printf("%c\t",root->data);
}
}
void pre_order(tree*root){//非递归先根遍历(辅助堆栈)
tree*s[20];
int i=0;
tree*p=root;
while(i!=0||p){
while(p!=NULL){
printf("%c\t",p->data);
s[++i]=p;
p=p->lchild;
}
p=s[i];
p=p->rchild;
i--;
}
}
void in_order(tree*root){//非递归中根遍历(辅助堆栈)
tree*s[20];
int i=-1,j=0;
tree*p;
p=root;
while(i!=-1||p){
j=1;
while(p!=NULL){
s[++i]=p;
p=p->lchild;
}
p=s[i];
printf("%c\t",p->data);
p=p->rchild;
i--;
}
}
void post_oeder(tree*root){//非递归后根遍历(辅助堆栈)
tree*s[20];
int i=-1;
tree*p=root;
s[++i]=p;
while(i!=-1){
p=s[i];
i--;
if(p->k==0){
p->k=1;
s[++i]=p;
if(p->lchild!=NULL)
s[++i]=p->lchild;
}
else if(p->k==1){
p->k=2;
s[++i]=p;
if(p->rchild!=NULL)
s[++i]=p->rchild;
}
else if(p->k==2)
printf("%c\t",p->data);
}
}
void levelorder(tree*root){//层次遍历(辅助环状队列)
tree*s[5];
int top=0,tail=0,count=0;
tree*p;
p=root;
s[tail]=p;
tail=(tail+1)%5;
count++;
while(count!=0){
p=s[top];
top=(top+1)%5;
count--;
printf("%c\t",p->data);
if(p->lchild!=NULL)
{s[tail]=p->lchild;
tail=(tail+1)%5;
count++;
}
if(p->rchild!=NULL)
{s[tail]=p->rchild;
tail=(tail+1)%5;
count++;
}
}
}
tree*find(tree*root,char item){//找值为特定值的一节点(递归)
tree*q=NULL;
if(root==NULL)
return NULL;
if(root->data==item)
{q=root;
return q;
}
q=find(root->lchild,item);
if(q!=NULL)
return q;
else return find(root->rchild,item);
}
tree*findfather(tree*root,tree*p,tree*q){//找某节点父亲节点(递归)
if(p==NULL||root==NULL||p==root)
return NULL;
if(root->lchild==p||root->rchild==p)
{ q=root;
return q;
}
q=findfather(root->lchild,p,q);
if(q!=NULL)
return q;
else return findfather(root->rchild,p,q);
}
void del(tree*p){//释放二叉树
if(p==NULL)
return;
del(p->lchild);
del(p->rchild);
free(p);
}
void insertll(tree*p,tree*s){//在特定节点后插入某节点
if(s==NULL||p==NULL)
return;
p->lchild=s->lchild;
s->lchild=p;
}
void dst(tree*t,tree*root){//删除节点
if(t==NULL)
return;
if(t==root)
{
del(t);
root=NULL;
return;
}
tree*p,*q;
p=t;
q=findfather(root,p,q);
if(q->lchild==p)
q->lchild=NULL;
if(q->rchild==p)
q->rchild=NULL;
del(p);
}
int main()
{
tree*root;
root=creat(root);
//preorder(root);
//inorder(root);
//postorder(root);
//printf("\n");
//pre_order(root);
//in_order(root);
//post_oeder(root);
//levelorder(root);
tree*p,*q=NULL,*t;char item;
getchar();
scanf("%c",&item);
q=find(root,item);
/*t=findfather(root,q,t);
if(p!=NULL)
printf("%c\t%c\t\n",t->data,q->data);
else
printf("p的father为空");
p=(tree*)malloc(sizeof(tree));
getchar();
scanf("%c",&(p->data));
insertll(p,q);*/
//levelorder(root);
dst(q,root);
levelorder(root);
return 0;
}