| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 425 人关注过本帖
标题:求助求助,这个程序运行一下就跳出来了
只看楼主 加入收藏
z616284115
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-7
结帖率:0
收藏
已结贴  问题点数:20 回复次数:9 
求助求助,这个程序运行一下就跳出来了
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<malloc.h>
#define SIZE 100
#define STACKINCREMENT   10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

typedef char TElemType;
typedef struct BiTNode
{TElemType data;
 struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

typedef BiTree SElemType;
typedef struct
      { SElemType *base;
        SElemType *top;
        int stacksize;
        }Stack;

char e;
int InitStack (Stack *S)
       {S->base=(SElemType *)malloc(SIZE *sizeof(SElemType));
        if(!S->base) return 0;
        S->top=S->base;
        S->stacksize=SIZE;
        return 1;
       }

int GetTop(Stack *S,SElemType e)
       {if(S->top==S->base) return 0;
           e=*(S->top-1);
           return 1;
       }
      
int Push(Stack *S,SElemType e)
       {if(S->top-S->base>=S->stacksize)
          {S->base=(SElemType *) realloc (S->base,(S->stacksize+STACKINCREMENT) *  sizeof(SElemType));
           if(!S->base) return 0;
           S->top=S->base+ S->stacksize;
           S->stacksize +=STACKINCREMENT;
          }
        *S->top++=e;
        return 1;
       }

int Pop(Stack *S,SElemType e)
       {if(S->top==S->base) return 0;
        e=*--S->top;
        return 1;
       }
  
int StackEmpty(Stack *S)
       {if(S->top==S->base) return 1;
       else return 0;
       }





//1.建空二叉树
void InitBiTree(BiTree T)
     {T=NULL;}
     
//2.删除二叉树
void DestroyBiTree(BiTree T)
     {if (T==0) return ;
     else
         {DestroyBiTree(T->lchild);
          DestroyBiTree(T->rchild);
          free(T);
          T=NULL;
         }
     }


//03. 构建二叉树      
//利用先序序列跟中序序列建立二叉树
void BuildBiTree(BiTree T, int ps, char pre[], int is, char ino[], int n)
{  if( !n )  return;
   
   BiTNode *p = T;
   p -> data = pre[ps];
   p -> lchild = p -> rchild = NULL;
   T = p;  int i;
   for( i = 0; i < n; i++ )    //i表示ino中root的位置(第i个元素)
     if( ino[is+i] == T -> data )    { i++;  break; }
   BuildBiTree( T -> lchild, ps+1, pre,  is  , ino, i-1 );
   BuildBiTree( T -> rchild, ps+i, pre,  is+i, ino, n-i );
}

void CreateBiTree( BiTree T)
{  char pre[10],ino[10];
   int n;
   printf( "请输入所要构造的二叉树的先序序列:");  //输入先序序列
   scanf("%s",pre);
   
   printf( "请输入所要构造的二叉树的中序序列:") ;  //输入中序序列
   scanf("%s",ino);
   
   printf( "请输入所要构建的二叉树的元素总数:");
   scanf("%d",&n);
   BuildBiTree( T, 0, pre, 0, ino, n );//递归构建二叉树
}

     
//4.清空二叉树
void ClearBiTree(BiTree T)
     {if(T==0) return;
      else
          {DestroyBiTree(T->lchild);
           DestroyBiTree(T->rchild);
           free(T);
           T=NULL;
          }
      }
      
//5.判断是否为空
int BiTreeEmpty(BiTree T)
     {if(T==0) return 1;
      else return 0;
     }
     
//6.求二叉树深度
int BiTreeDepth(BiTree T)
    {int depthval,depthleft,depthright;
     if(!T) return depthval=0;
     else
         {depthleft=BiTreeDepth(T->lchild);
          depthright=BiTreeDepth(T->rchild);
          depthval=1+(depthleft>depthright?depthleft:depthright);
          }
    }
   
//7.求二叉树跟
int Root(BiTree T)
    {if(T)
     return(T->data);
    }
   
//8.求二叉树值
int Value(BiTree T,TElemType e)
    {Stack S;
     InitStack(&S);
     BiTNode *p=T;
     
     if(!p) return 0;
     else
        {while(p!=0 || StackEmpty(&S)!=1)
              {while(p!=0)
                     {if(p->data==e)
                        {printf("the value of e is %d\n",p->data);
                         return 1;
                        }
                      Push(&S,p);
                      p=p->lchild;
                     }
              Pop(&S,p);
              p=p->rchild;
              }
        }
     printf("e is not in the BiTree\n");
    }
   

//9.改变二叉树e值
int Assign(BiTree T,TElemType e,TElemType value)
    {Stack S;
     InitStack(&S);
     BiTNode *p=T;
     
     if(!p) return 0;
     else
        {while(p!=0 || StackEmpty(&S)!=1)
              {while(p!=0)
                     {if(p->data==e)  
                        {p->data=value;
                         printf("the value of e is change to %d\n",p->data);
                         return 1;}
                      Push(&S,p);
                      p=p->lchild;
                     }
              Pop(&S,p);
              p=p->rchild;
              }
        }
     printf("e is not in the BiTree\n");
    }

//10.求e的双亲
int Parent(BiTree T,TElemType e)
    {Stack S;
     InitStack(&S);
     BiTNode *p=T;
     
     if(!p) return 0;
     else
         {while(p!=0||StackEmpty(&S)==0)
                {while(p!=0)
                     {if(p->data==e)
                         {Pop(&S,p);
                          return p->data;}
                     Push(&S,p);
                     p=p->lchild;
                     }
                Pop(&S,p);
                p=p->rchild;
                }
         }
     printf("e is not in the BiTree\n");
    }
   
//11.求e的左孩子
int LeftChild(BiTree T,TElemType e)
    {Stack S;
     InitStack(&S);
     BiTNode *p=T;
     
     if(!p) return 0;
     else
         {while(p!=0||StackEmpty(&S)==0)
                {while(p!=0)
                      {if(p->data==e)
                         {if(p->lchild==0) return 0;
                          else return p->lchild->data;
                         }
                       Push(&S,p);
                       p=p->lchild;
                      }
                Pop(&S,p);
                p=p->rchild;
                }
         }
     printf("e is not in the BiTree\n");
    }
   
//12.求e的右孩子
int RightChild(BiTree T,TElemType e)
    {Stack S;
     InitStack(&S);
     BiTNode *p=T;
     
     if(!p) return 0;
     else
         {while(p!=0||StackEmpty(&S)==0)
                {while(p!=0)
                      {if(p->data==e)
                         {if(p->rchild==0) return 0;
                          else return p->rchild->data;
                         }
                       Push(&S,p);
                       p=p->lchild;
                      }
                Pop(&S,p);
                p=p->rchild;
                }
         }
     printf("e is not in the BiTree\n");
    }
   
//13.求e的左兄弟   
int LeftSibling(BiTree T,TElemType e)
    {Stack S;
     InitStack(&S);
     BiTNode *p=T;                        
                       
     while( p || !StackEmpty(&S) )                              
          {  while( p )                                             
                  {  if( p->lchild && p->lchild->data == e )            
                       {  printf("e是其双亲的左孩子\n" );  return 1;  }
                     if( p->rchild && p->rchild->data == e )         
                       {  if( p->lchild )  return p->lchild->data;
                          else  printf("e没有左兄弟\n");            
                          return 1;
                       }
                     Push(& S, p );                                      
                     p = p -> lchild;                                   
                  }
              Pop( &S, p );                                         
              p = p -> rchild;                                    
           }
        printf("e is not in the BiTree\n");                       
    }   
   
//14.求e的右兄弟   
int RightSibling(BiTree T,TElemType e)
     {Stack S;
     InitStack(&S);
     BiTNode *p=T;                        
                       
       while( p || !StackEmpty(&S) )                              
        {   while( p )                                             
            {  if( p->lchild && p->lchild->data == e )            
                 {  if( p->rchild )  return  p->rchild->data;
                    else  printf( "e没有右兄弟\n" );            
                    return 1;
                 }
               if( p->rchild && p->rchild->data == e )            
                 {  printf("e是其双亲的右孩子\n");  return 1;  }
               Push(& S, p );                                       
               p = p -> lchild;                                    
            }
            Pop(& S, p );                                          
            p = p -> rchild;                                    
        }
        printf("e is not in the BiTree\n");                       
    }   
 
// 15.插入e
int InsertChild(BiTree T,TElemType e,int LR,BiTree c)
    {Stack S;
     InitStack(&S);
     BiTNode *p=T;
     
     if(c->rchild)
       {printf("the righttree of c is not empty\n");
        return 1;
       }
     while(p!=0||StackEmpty(&S)==0)
          {while(p!=0)
                {if(p->data==e)
                    {if(LR==0)
                       {c->rchild=p->lchild;  p->lchild=c;}
                     else {c->rchild=p->rchild; p->rchild=c;}
                     printf("insert succed");
                     return 1;
                    }
                else {Push(&S,p); p=p->lchild;}
                }
          Pop(&S,p);
          p=p->rchild;
          }
     printf("e is not in the BiTree\n");
    }
   
//16.删除以e为根的树
int DeleteChild(BiTree T,TElemType e,int LR)
    {Stack S;
     InitStack(&S);
     BiTNode *p=T;
     
     while(p!=0||StackEmpty(&S)==0)
           {while(p!=0)
                 {if(p->data==e)
                     {if(LR==0)
                        {DestroyBiTree(p->lchild); return 1;}
                      else {DestroyBiTree(p->rchild); return 1;}
                     }
                 else {Push(&S,p); p=p->lchild;}
                 }
           Pop(&S,p);
           p=p->rchild;
           }
      printf("e is not in the tree\n");
    }
         

//17.前序遍历二叉树
int PreOrderTraverse(BiTree T)
    {if( T )            
       {printf("%c",T->data);
        PreOrderTraverse( T -> lchild );
        PreOrderTraverse( T -> rchild );
         
       }
    }

   
//18.中序遍历二叉树
int InOrderTraverse(BiTree T)
    {if( T )            
       {InOrderTraverse( T -> lchild );
        printf("%c",T->data);  
        InOrderTraverse( T -> rchild );
       }
    }


//19.后序遍历二叉树
int PostOrderTraverse(BiTree T)
    {if( T )            
       {PostOrderTraverse( T -> lchild );  
        PostOrderTraverse( T -> rchild );
          printf("%c",T->data);
       }
    }


//20.层序遍历二叉树
int LevelOrderTraverse(BiTree T)
    {BiTree Q[100];
     int front=0,rear=0;
     BiTNode *p=T;
     if(!p) return 0;
     Q[rear++]=p;
     while(front!=rear)
     
          {p=Q[front++];
           printf("%d\n",p->data);
           if(p->lchild)  Q[rear++]=p->lchild;
           if(p->rchild)  Q[rear++]=p->rchild;
          }
     }




int menu()
{  int time = 0, se_num, flag;
   int k;
   char str[2];
   do
   {  system("cls");   //运行前清屏
      //显示菜单
      system("cls");
    printf(" * * * * * * * * * * * * * * * * 菜 单 * * * * * * * * * * * * * * * * * * * \n");
    printf(" *   01.InitBiTree           02.DestoryBiTree        03.CreateBiTree       * \n");
    printf(" *   04.ClearBiTree          05.BiTreeEmpty          06.BiTreeDepth        * \n");
    printf(" *   07.Root                 08.Value                09.Assign             * \n");
    printf(" *   10.Parent               11.LeftChild            12.RightChild         * \n");
    printf(" *   13.LeftSibling          14.RightSibling         15.InsertChild        * \n");
    printf(" *   16.DeleteChild          17.PreOrderTraverse     18.InOrderTraverse    * \n");
    printf(" *   19.PostOrderTraverse    20.LevelOrderTraverse                         * \n");
    printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n");
      //根据time的值(0表示第一次输入,否则非第一次)输出不同提示
      if( !time ) printf( "请输入函数前的序号(只取输入前两位):");
      else        printf( "输入错误,请重新输入(只取输入前两位):");
      
      for(k=0;k<=1;k++)
         scanf("%c",&str[k]);     //读入使用者的选择序号
      time++;
      
      if( !(str[0] >= '0' && str[0] <= '2') ||
             !(str[1] >= '0' && str[1] <= '9')  )      flag = 1;
         else
         {  se_num = (str[0]-'0') * 10 + (str[1]-'0');
        
            flag = 0;
         }
         //如果第一个数不在0-2范围内或第二个数不在0-9范围内,标记为1,准备进入循环
         //否则,将该数转成整型存入se_num,标记为0
   
   }  while( flag || !( se_num >= 1 && se_num <= 20 ) );  //判断标记值及se_sum的值
   return se_num;  //返回se_sum的值
}




//主函数
int main()
{  TElemType e, value, temp;
   char x[20];
   BiTree T = NULL, C = NULL;
   int LR;
      
   for( ; ; )
   {  switch( menu() )                                     //选择想要调用的函数
      {  case  1: InitBiTree(T)   ;               break;  //构造空树T
         case  2: if(T) printf("树T已被销毁\n" );
                  DestroyBiTree( T );               break;  //销毁树T
         case  3:  CreateBiTree(T);                          //构造树T(使用先序中序建树)
                 printf("二叉树已经建好\n"); break;
         case  4: if(T)  printf( "树T已被清空\n");
                  ClearBiTree( T );                 break;  //清空树T
         case  5: if( BiTreeEmpty(T) ) printf("树T是空树\n"); //判断树T是否为空树
                  else printf( "树T不是空树\n");        break;
         case  6: printf("树T的深度是:\n");                  //求树T的深度
                  printf("BiTreeDepth(T) ");    break;
         case  7: Root(T);                          break;  //求树T的根
         
         case  8: printf("请输入e值:\n"); scanf("%d",&e);    Value(T,e);           break;  //求e的值
         case  9: printf("请输入e值:\n"); scanf("%d",&e);    printf("请输入value值:\n"); scanf("%d",&e);
                             Assign(T,e,value);    break;  //将e的值改为value
         case 10: printf("请输入e值:\n"); scanf("%d",&e);   Parent(T,e);          break;  //求e的双亲
         case 11: printf("请输入e值:\n"); scanf("%d",&e);   LeftChild(T,e);       break;  //求e的左孩子
         case 12: printf("请输入e值:\n"); scanf("%d",&e);   RightChild(T,e);      break;  //求e的右孩子
         case 13: printf("请输入e值:\n"); scanf("%d",&e);   LeftSibling(T,e);     break;  //求e的左兄弟
         case 14: printf("请输入e值:\n"); scanf("%d",&e);   RightSibling(T,e);    break;  //求e的右兄弟
         case 15: printf("请输入e值:\n"); scanf("%d",&e);   printf("请输入LR值:\n"); scanf("%d",&e);    CreateBiTree(C);
                  InsertChild( T, e, LR, C );       break;  //在树T中插入树C
         case 16: printf("请输入e值:\n"); scanf("%d",&e);   printf("请输入LR值:\n"); scanf("%d",&e);
                  DeleteChild( T, e, LR );          break;  //删除e的左/右子树
         case 17: printf("先序遍历:");  PreOrderTraverse  ( T );  break;  //先序遍历
         case 18: printf("中序遍历:");  InOrderTraverse   ( T );  break;  //中序遍历
         case 19: printf("后序遍历:");  PostOrderTraverse ( T );  break;  //后序遍历
         case 20: printf("层次遍历:");  LevelOrderTraverse( T );  break;  //层次遍历
      }
      system("Pause");
    }
   return 0;
}
搜索更多相关主题的帖子: include 
2011-06-07 15:12
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:4 
gcc 试了下,不会出现楼主所说的问题啊
2011-06-07 15:29
z616284115
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-7
收藏
得分:0 
为什么用VC会呢?
2011-06-07 16:16
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:4 
这个二叉树写的够全的啊  可惜我这里的6.0编译都有错误

                                         
===========深入<----------------->浅出============
2011-06-07 16:20
bccn_2012
Rank: 6Rank: 6
等 级:侠之大者
帖 子:158
专家分:447
注 册:2011-5-14
收藏
得分:4 
我用vc编译的:

 --69 error(s), 44 warning(s)

表示有点头大。。。
2011-06-07 16:26
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:0 
这个,换个版本新一点的 vc 试试
2011-06-07 17:24
z616284115
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2011-6-7
收藏
得分:0 
有没有高手告诉我哪里错拉?
2011-06-08 18:10
voidx
Rank: 12Rank: 12Rank: 12
来 自:邯郸
等 级:火箭侠
帖 子:1250
专家分:3538
注 册:2011-4-7
收藏
得分:0 
回复 7楼 z616284115
谁也不可能告诉你,因为你的程序没有错。
换个编译器吧
2011-06-08 18:11
官人
Rank: 3Rank: 3
来 自:瓮安
等 级:论坛游侠
帖 子:88
专家分:152
注 册:2011-6-2
收藏
得分:4 
完全不懂!!!!

   ---------------天道刚健,你不自强谁来帮你---------------
2011-06-08 19:11
qldxsun
Rank: 4
等 级:业余侠客
帖 子:125
专家分:240
注 册:2011-6-4
收藏
得分:4 
回复 楼主 z616284115
老师留得作业还是自己主动做的啊!二叉树的操作基本都写了……
我表示vc6.0的确经常出现错误……tc比较给力……vc2005没用过……
c99也不懂……
2011-06-09 14:17
快速回复:求助求助,这个程序运行一下就跳出来了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034269 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved