二cha树

  1 void porder(BTree *b)
  2 {
  3     BTree *St[MaxSize],*p;
  4     int top=1;
  5     if(b!=NULL)
  6     {
  7         top++;
  8         St[top]=b;
  9         while(top>-1)
 10         {
 11             p=St[top];
 12             top--;
 13             printf("%c",p->data);
 14             if(p->rchild!=NULL)
 15             {
 16                 top++;
 17                 St[top]=p->rchild;
 18             }
 19             if(p->lchild!=NULL)
 20             {
 21                 top++;
 22                 St[top]=p->lchild;
 23             }
 24         }
 25     }
 26 }
 27 
 28 void psorder(BTree*t)
 29 {
 30     BTree *St[MaxSize];
 31     BTree*p;
 32     int flag,top=-1;
 33     do
 34     {
 35         while(t)
 36         {
 37             top++;
 38             St[top]=t;
 39             t=t->lchild;
 40         }
 41         p=NULL;
 42         flag=1;
 43         while(top!=-1&&flag)
 44         {
 45             t=St[top];
 46             if(t->rchild==p)
 47             {
 48                 printf("%c ",t->data);
 49                 top--;
 50                 p=t;
 51             }
 52             else
 53             {
 54                 t=t->rchild;
 55                 flag=0;
 56             }
 57         }
 58     }while(top!=-1)
 59 }
 60 
 61 int level(BTree *b,ElemType x,int h)
 62 {
 63     int h1;
 64     if(b==NULL)
 65         return(0);
 66     else
 67         if(b->data==x)
 68             return(h);
 69         else
 70         {
 71             h1=level(b->lchild,x,h+1);
 72             if(h1!=0)
 73                 return(h1);
 74             else
 75                 return level(b->rchild,x,h+1);
 76         }
 77 }
 78 
 79 void translevel(BTree *b)
 80 {
 81     struct node
 82     {
 83         BTree *vec[MaxSize];
 84         int f,r;
 85     }Qu;
 86     Qu.f=0;
 87     Qu.r=0;
 88     if(b!=NULL)
 89         printf("%c ",b->data);
 90     Qu.vec[Qu.r]=b;
 91     Qu.r=Qu.r+1;
 92     while(Qu.f<Qu.r)
 93     {
 94         b=Qu.vec[Qu.f];
 95         Qu.f=Qu.f+1;
 96         if(b->lchild!=NULL)
 97         {
 98             printf("%c",b->lchild->data);
 99             Qu.vec[Qu.r]=b->lchild;
100             Qu.r=Qu.r+1;
101         }
102         if(b->rchild!=NULL)
103         {
104             printf("%c ",b->rchild->data);
105             Qu.vec[Qu.r]=b->rchild;
106             Qu.r=Qu.r+1;
107         }
108     }
109     printf("
");
110 }
原文地址:https://www.cnblogs.com/Zblogs/p/3400009.html