数据结构_求二叉树的宽度_C语言源代码

来源:互联网 发布:qq看不见对方网络状态 编辑:程序博客网 时间:2024/05/22 00:49


typedef struct 
{
        BTNode *p;
        int line;
}PL;
const int M=50;
int  BTreeWidth(BTNode *T) 
{
    BTNode *s;
    int LNO;
    PL Q[M];
    int front=0,rear=0;
    int i,j;
    int max=0,num;
    if(NULL == T)  return 0;
    else
    {
        Q[rear].p=T;
        Q[rear].line=1;
        rear=(rear+1)%M;
        while(rear !=front)
        {
               s = Q[front].p; 
               LNO= Q[front].line;
               front=(front+1)%M;  
               if(s->lchild!=NULL)  
               {
                 Q[rear].p = s->lchild;
                 Q[rear].line =LNO + 1;
                 rear =(rear +1)%M;
               }
               
               if(s->rchild!=NULL)  
               {
                 Q[rear].p = s->rchild;
                 Q[rear].line =LNO + 1;
                 rear =(rear +1)%M;
               }
               
        }
        
        for(i=1;i<=LNO;++i)
        {
          num=0;                          
          for(j=1;j<=rear;++j)
          {
            if(Q[j].line==i)  ++num;
          }
          
          if(max<num)
            max=num;
           
        }
        
      return max;
        
        
        
        
        
        
        
        
        
    }
}
0 0
原创粉丝点击