二叉樹的創建與遍歷

    最近在學習C語言的基礎知識。
    正好,今天爲一個同學講二叉樹的創建與排序。將最近複習的知識又重新梳理了一下,順便記錄下二叉樹實現的具體代碼。
    1、二叉樹節點的存儲結構

  1. typedef struct BiTNode 
  2.     ElemType data; 
  3.     struct BiTNode*lchild,*rchild;//左右孩子指針 
  4. } BiTNode,*BiTree; 

    2 利用前驅遍歷的方式,創建二叉樹,

    3 利用遞歸的方式,前序、中序、後序遍歷二叉樹
 

  1. #include <stdlib.h> 
  2. #include <stdio.h> 
  3. typedef char ElemType;//每個元素的數據類型爲ElemType。假設爲char 
  4. typedef struct BiTNode 
  5.     ElemType data; 
  6.     struct BiTNode*lchild,*rchild;//左右孩子指針 
  7. } BiTNode,*BiTree; 
  8. //打印主界面 
  9. void printMain() 
  10.     printf("**********************************************************\n"); 
  11.     printf("\t 1、建立二叉樹\n"); 
  12.     printf("\t 2、前序遍歷二叉樹\n"); 
  13.     printf("\t 3、中序遍歷二叉樹\n"); 
  14.     printf("\t 4、後序遍歷二叉樹\n"); 
  15.     printf("\t 0、結束\n"); 
  16.     printf("**********************************************************\n"); 
  17. //創建二叉樹 
  18. BiTree CreateBiTree(BiTree T) 
  19.     char ch; 
  20.     ch=getchar(); 
  21.     if(ch=='#'
  22.         T=NULL; 
  23.     else
  24.         if(!(T=(BiTNode *)malloc(sizeof(BiTNode)))) 
  25.         printf("Error!"); 
  26.         T->data=ch; 
  27.         T->lchild=CreateBiTree(T->lchild); 
  28.         T->rchild=CreateBiTree(T->rchild); 
  29.     } 
  30.     return T; 
  31. //前序遍歷 
  32. void PreOrder(BiTNode *root) 
  33.     if(root==NULL) return
  34.     printf("%c",root->data); 
  35.     PreOrder(root->lchild); 
  36.     PreOrder(root->rchild); 
  37. //中序遍歷 
  38. void InOrder  (BiTNode *root) 
  39.     if(root==NULL) 
  40.         return
  41.     InOrder(root->lchild); 
  42.     printf("%c",root->data); 
  43.     InOrder(root->rchild); 
  44.   
  45. //後序遍歷 
  46. void   PostOrder(BiTNode *root) 
  47.     if(root==NULL) 
  48.         return
  49.     PostOrder(root->lchild); 
  50.     PostOrder(root->rchild); 
  51.     printf("%c",root->data); 
  52. int main() 
  53.     BiTree A=NULL; 
  54.     int  i;        
  55.     do
  56. //        system("cls"); 
  57.         printMain(); 
  58.         printf("請選擇操作數(1,2,3,4,0):"); 
  59.         scanf("%d",&i); 
  60.         switch(i) 
  61.         { 
  62.             case 1: 
  63.                 { 
  64.                     printf("創建二叉樹:"); 
  65.                     A = CreateBiTree(A); 
  66.                     break
  67.                 }            
  68.             case 2: 
  69.                 { 
  70.                     printf("前序遍歷二叉樹:"); 
  71.                     PreOrder(A); 
  72.                     break
  73.                 } 
  74.             case 3: 
  75.                 { 
  76.                     printf("中序遍歷二叉樹:"); 
  77.                     InOrder(A); 
  78.                     break
  79.                 } 
  80.             case 4: 
  81.                 { 
  82.                     printf("後序遍歷二叉樹:"); 
  83.                     PostOrder(A); 
  84.                     break
  85.                 } 
  86.             case 0: 
  87.                 exit(0); 
  88.         } 
  89.         printf("\n"); 
  90.     }while( i==1 || i==2 || i==3 || i==4 || i==0 );    
  91.     return 0; 
  92.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章