二叉树操作(补充)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值