二叉樹創建以及遍歷(j簡單創建、遍歷、葉子計數、深度計數、銷燬)之1

//binary.h

  6 #include <stdlib.h>
  7
  8 #define OK 1
  9 #define ERROR 0
 10 #define OVERFLOW -2
 11
 12 typedef int Status;
 13 typedef struct BiTNode
 14 {
 15     int data;
 16     struct BiTNode *lchild,*rchild;
 17 }BiTNode,*BiTree;
 18
 19 Status CreateBiTree(BiTree *T);
 20 Status pre_serche(BiTree T);
 21 Status in_serche(BiTree T);
 22 Status po_serche(BiTree T);
 23 Status destory_tree(BiTree *T);
 24 void countleaf(BiTree T,int *count);
 25 int Depth(BiTree T);
 26 int preorder(BiTree T, int x, BiTree *p);
 27 #endif

//binary.c

1 #include "binary.h"
  2
  3 /*creat tree
  4  *
  5  * */
  6 Status CreateBiTree(BiTree *T)
  7 {
  8     int e;
  9     scanf("%d",&e);
 10
 11     if (0 == e)
 12         *T = NULL;
 13     else {
 14         *T = (BiTree)malloc(sizeof(BiTNode));
 15
 16         if (NULL == *T)
 17         {
 18             perror("creat error\n");
 19             exit(-1);
 20         }
 21
 22         (*T)->data = e;
 23         printf("successful\n");
 24         CreateBiTree(&((*T)->lchild));
 25         CreateBiTree(&((*T)->rchild));
 26     }
 27     return OK;
 28 }
 29
 30 /*preorder traversal...
 31  * */
 32 Status pre_serche(BiTree T)
 33 {
 34     if (NULL == T) return OK;
 35     printf("%d  ",T->data);
 36     pre_serche(T->lchild);
 37     pre_serche(T->rchild);
 38 }
 39
 40 /*infix order
 41  *
 42  * */
 43 Status in_serche(BiTree T)
44 {
 45     if (NULL == T) return OK;
 46     in_serche(T->lchild);
 47     printf("%d  ",T->data);
 48     in_serche(T->rchild);
 49 }
 50
 51 /*
 52  *postorder
 53  * */
 54 Status po_serche(BiTree T)
 55 {
 56     if (NULL == T) return OK;
 57     po_serche(T->lchild);
 58     po_serche(T->rchild);
 59     printf("%d  ",T->data);
 60 }
 61
 62 /*count tree is leaf
 63  *  para1:tree address
 64  *  para2:we need number of tree's leaf
 65  * */
 66 void countleaf(BiTree T,int *count)
 67 {
 68
 69     if (NULL != T)
 70     {
 71         if ((NULL == T->lchild) && (NULL == T->rchild))
 72         {
 73             (*count)++;
 74         }
 75         countleaf(T->lchild,count);
 76         countleaf(T->rchild,count);
 77     }//if
 78 }//countleaf
 79
 80 /*count depth of tree
 81  *para1:tree address
 82  * */
 83 int Depth(BiTree T)
 84 {
 85     int depthleft,depthright,depthval=0;
86
 87     if (NULL == T) depthval = 0;
 88     else {
 89         depthleft = Depth(T->lchild);
 90         depthright = Depth(T->rchild);
 91         depthval = 1 + (depthleft > depthright?depthleft:depthright);
 92     }
 93     return depthval;
 94 }
 95
 96 /*find x from tree  and  p point that x's NODE
 97  *
 98  * */
 99 int preorder(BiTree T, int x, BiTree *p)
100 {
101     if (T)
102     {
103         if (T->data == x)
104         {
105             *p = T;
106             return OK;
107         } else {
108             if (preorder(T->lchild,x,p))
109             {
110                 return OK;
111             } else return(preorder(T->rchild,x,p));
112         }
113     } else {
114         *p = NULL;
115         return ERROR;
116     }
117 }
118
119 /*destory tree
120  * */
121 Status destory_tree(BiTree *T)
122 {
123     if (NULL == *T)
124     {
125         return OK;
126     }
127
128     destory_tree(&((*T)->lchild));
129     destory_tree(&((*T)->rchild));
130     free(*T);
131     *T = NULL;
132 }

//test.c

1 #include "binary.h"
  2
  3 int main(int argc, const char *argv[])
  4 {
  5     int count = 0;
  6     BiTree T;
  7     CreateBiTree(&T);
  8     printf("----------------------\n");
  9
 10     pre_serche(T);
 11     putchar('\n');
 12
 13     in_serche(T);
 14     putchar('\n');
 15
 16     po_serche(T);
 17     putchar('\n');
 18
 19 /* count leaf  
 20     countleaf(T, &count);
 21     printf("num is %d\n",count);
 22 */
 23
 24 /*
 25 printf("depth is %d \n",Depth(T));
 26 */
 27
 28 /*fiding data x*/
 29     BiTree p;
 30     if (preorder(T,3,&p))
 31     {
 32         printf("find it \n");
 33         printf("find it is %d\n",p->data);
 34     } else {
 35         printf("not find \n");
 36     }
 37     destory_tree(&T) ;
 38     po_serche(T);
 39     putchar('\n');
 40
 41     return 0;
 42 }

 //Makefile

  2 CC  :=gcc
  3
  4 main:test.c binary.c
  5     $(CC) $^  -o main
  6
  7 clear:
  8     rm main
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章