当前位置:首页
开发技术指南» 文章正文
    引言:

 ·    »显示摘要«
    摘要: 我要使用glib库 但是无论我在编译时怎么做都不行 gcc gtk-config --cflags --libs glib until.c -o until.c gcc pkg-config --cflags --libs glib until.c -o until.c gcc until.c -o until.c -lglib 使用rpm -wa|grep glib可以看到glib-1.......
    摘要: set @sqlstr=update sbtmp set + @xs + = + @str exec (@sqlstr) 此语句不正确,因为@str 是字符型变量,应该引起来,但不知道sql中的转义符是什么,请教,谢谢! 如有其他方法实现我的目的的也非常感谢! ......


谁能给个建立二叉树,三种递归和非递归遍历C语言的源程序

谁能给个建立二叉树,三种递归和非递归遍历C语言的源程序??

NO.1   作者: xfgang

二叉树的三种递归遍历的算法:  
  typedef     struct   btnode   *bitreptr  
  struct   btnode    
  {datatype   data;  
    bitreptr   lchild,rchild;  
  }  
  bitreptr   root;  
  void   preorder(bitrept     r)  
  /*先根遍历根指针为r的算法*/  
      {  
          if(r!=null)  
          {  
              visit(r);         /*访问根结点*/  
              preorder(r->lchild);  
              preorder(r->rchild);  
          }  
  }  
  void   preorder(bitrept     r)  
  /*中根遍历根指针为r的算法*/  
  {  
          if(r!=null)  
          {  
               
              inorder(r->lchild);  
              visit(r);         /*访问根结点*/  
              inorder(r->rchild);  
          }  
  }  
  void   preorder(bitrept     r)  
  /*后根遍历根指针为r的算法*/  
  {  
          if(r!=null)  
          {  
                postorder(r->lchild);  
                  postorder(r->rchild);  
                visit(r);         /*访问根结点*/  
   
          }  
  }  
 

NO.2   作者: foochow

//看看这个别人的,呵呵  
   
  1.先序遍历非递归算法  
  #define   maxsize   100  
  typedef   struct  
  {  
          Bitree   Elem[maxsize];  
          int   top;  
  }SqStack;  
   
  void   PreOrderUnrec(Bitree   t)  
  {  
          SqStack   s;  
          StackInit(s);  
          p=t;  
           
          while   (p!=null   ||   !StackEmpty(s))  
          {  
                  while   (p!=null)                           //遍历左子树  
                  {  
                          visite(p->data);  
                          push(s,p);  
                          p=p->lchild;                
                  }//endwhile  
                   
                  if   (!StackEmpty(s))                   //通过下一次循环中的内嵌while实现右子树遍历  
                  {  
                          p=pop(s);  
                          p=p->rchild;                  
                  }//endif  
                                   
          }//endwhile    
           
  }//PreOrderUnrec  
   
  2.中序遍历非递归算法  
  #define   maxsize   100  
  typedef   struct  
  {  
          Bitree   Elem[maxsize];  
          int   top;  
  }SqStack;  
   
  void   InOrderUnrec(Bitree   t)  
  {  
          SqStack   s;  
          StackInit(s);  
          p=t;  
          while   (p!=null   ||   !StackEmpty(s))  
          {  
                  while   (p!=null)                           //遍历左子树  
                  {  
                          push(s,p);  
                          p=p->lchild;  
                  }//endwhile  
                   
                  if   (!StackEmpty(s))  
                  {  
                          p=pop(s);  
                          visite(p->data);                 //访问根结点  
                          p=p->rchild;                         //通过下一次循环实现右子树遍历  
                  }//endif        
           
          }//endwhile  
   
  }//InOrderUnrec  
   
   
  3.后序遍历非递归算法  
  #define   maxsize   100  
  typedef   enum{L,R}   tagtype;  
  typedef   struct    
  {  
          Bitree   ptr;  
          tagtype   tag;  
  }stacknode;  
   
  typedef   struct  
  {  
          stacknode   Elem[maxsize];  
          int   top;  
  }SqStack;  
   
  void   PostOrderUnrec(Bitree   t)  
  {  
          SqStack   s;  
          stacknode   x;  
          StackInit(s);  
          p=t;  
           
          do    
          {  
                  while   (p!=null)                 //遍历左子树  
                  {  
                          x.ptr   =   p;    
                          x.tag   =   L;                   //标记为左子树  
                          push(s,x);  
                          p=p->lchild;  
                  }  
           
                  while   (!StackEmpty(s)   &&   s.Elem[s.top].tag==R)      
                  {  
                          x   =   pop(s);  
                          p   =   x.ptr;  
                          visite(p->data);       //tag为R,表示右子树访问完毕,故访问根结点                
                  }  
                   
                  if   (!StackEmpty(s))  
                  {  
                          s.Elem[s.top].tag   =R;           //遍历右子树  
                          p=s.Elem[s.top].ptr->rchild;                  
                  }          
          }while   (!StackEmpty(s));  
  }

NO.3   作者: superccwang

二叉树实现源代码如下:  
   
  #include   <conio.h>  
  #include   <stdio.h>  
  #include   <stdlib.h>  
   
  #define   OK   1  
  #define   ERROR   0  
  #define   TRUE   1  
  #define   FALSE   0  
  #define   OVERFLOW   -2  
  typedef   int   status;  
   
  typedef   struct   BiNode  
  {  
          char   Data;  
          struct   BiNode*   lChild;  
          struct   BiNode*   rChild;  
  }BiNode,*pBiNode;  
   
  status   CreateTree(BiNode**   pTree);  
  status   PreOrderTraval(BiNode*   pTree);  
  status   Visit(char   Data);  
  status   Display(BiNode*   pTree,int   Level);  
  status   Clear(BiNode*   pTree);  
   
  BiNode   *pRoot=NULL;  
   
  main()  
  {  
          clrscr();  
          CreateTree(&pRoot);  
   
          printf("   PreOrder:");  
          PreOrderTraval(pRoot);  
          printf("   ");  
   
          printf("   InOrder:");  
          InOrderTraval(pRoot);  
          printf("   ");  
   
          printf("   PostOrder:");  
          PostOrderTraval(pRoot);  
          printf("   ");  
   
          printf("   ShowLeaves:");  
          ShowLeaves(pRoot);  
          printf("   -----------------------   ");  
          printf("   ");  
   
          Display(pRoot,0);  
   
          printf("   ");  
          printf("   Deleting   Tree:   ");  
          DelTree(pRoot);  
          printf("BiTree   Deleted.");  
   
          getch();  
  }  
  status   CreateTree(BiNode**   pTree)   /*Input   Example:   abd##e##cf##g##*/  
  {  
          char   ch;  
          scanf("%c",&ch);  
          if(ch==‘#‘)  
          {  
                  (*pTree)=NULL;  
          }  
          else  
          {  
                  if(!((*pTree)=(BiNode*)malloc(sizeof(BiNode))))  
                  {  
                          exit(OVERFLOW);  
                  }  
                  (*pTree)->Data=ch;  
                  CreateTree(&((*pTree)->lChild));  
                  CreateTree(&((*pTree)->rChild));  
          }  
  return   OK;  
  }  
  status   PreOrderTraval(BiNode*   pTree)  
  {  
          if(pTree)  
          {  
                  if(Visit(pTree->Data))  
                  {  
                          if(PreOrderTraval(pTree->lChild))  
                          {  
                                  if(PreOrderTraval(pTree->rChild))  
                                  {  
                                          return   OK;  
                                  }  
                          }  
                  }  
                  return   ERROR;  
          }  
          else  
          {  
                  return   OK;  
          }  
  }  
  status   InOrderTraval(BiNode*   pTree)  
  {  
          if(pTree)  
          {  
                  if(InOrderTraval(pTree->lChild))  
                  {  
                          if(Visit(pTree->Data))  
                          {  
                                  if(InOrderTraval(pTree->rChild))  
                                  {  
                                          return   OK;  
                                  }  
                          }  
                          return   ERROR;  
                  }  
                  return   ERROR;  
          }  
          else  
          {  
                  return   OK;  
          }  
  }  
  status   PostOrderTraval(BiNode*   pTree)  
  {  
          if(pTree)  
          {  
                  if(PostOrderTraval(pTree->lChild))  
                  {  
                          if(PostOrderTraval(pTree->rChild))  
                          {  
                                  if(Visit(pTree->Data))  
                                  {  
                                          return   OK;  
                                  }  
                                  return   ERROR;  
                          }  
                  }  
                  return   ERROR;  
          }  
          else  
          {  
                  return   OK;  
          }  
  }  
  status   Visit(char   Data)  
  {  
          printf("%c",Data);  
          return   OK;  
  }  
  status   Display(BiNode*   pTree,int   Level)  
  {  
          int   i;  
          if(pTree==NULL)   return;  
          Display(pTree->lChild,Level+1);  
          for(i=0;i<Level-1;i++)  
          {  
                  printf("   ");  
          }  
          if(Level>=1)  
          {  
                  printf("--");  
          }  
          printf("%c   ",pTree->Data);  
          Display(pTree->rChild,Level+1);  
  }  
  status   ShowLeaves(BiNode*   pTree)  
  {  
          if(pTree)  
          {  
                  if(ShowLeaves(pTree->lChild))  
                  {  
                          if(ShowLeaves(pTree->rChild))  
                          {  
                                  if((pTree->lChild==NULL)&&(pTree->rChild==NULL))  
                                  {  
                                          if(!Visit(pTree->Data))  
                                          {  
                                                  return   ERROR;  
                                          }  
                                  }    
                                  return   OK;  
                          }  
                  }  
                  return   ERROR;  
          }  
          else  
          {  
                  return   OK;  
          }  
  }  
  status   DelTree(BiNode*   pTree)  
  {  
          if(pTree)  
          {  
                  if(DelTree(pTree->lChild))  
                  {  
                          if(DelTree(pTree->rChild))  
                          {  
                                  printf("Deleting   %c   ",pTree->Data);  
                                  free((void*)pTree);  
                                  return   OK;  
                          }  
                  }  
                  return   ERROR;  
          }  
          else  
          {  
                  return   OK;  
          }  
  }


    摘要: 如何得到? ......
» 本期热门文章:

©2000-2007 All Rights Reserved. 最佳浏览:1024X768 MSIE