数据结构——二叉树的遍历

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 #define OVERFLOW 0
 5 typedef char Elemtype;
 6 typedef struct TreeNode
 7 {
 8     Elemtype data;
 9     struct TreeNode *Lchild;
10     struct TreeNode *Rchild;
11 } TreeNode,*BiTree;
12 
13 void CreateBiTree(BiTree *T)
14 {
15     char ch;
16     scanf("%c",&ch);
17     if(ch == '#')
18         *T = NULL;
19     else
20     {
21         *T = (BiTree)malloc( sizeof(TreeNode) );
22         if(!*T)
23             exit(OVERFLOW);
24         (*T)->data = ch;
25         CreateBiTree(&(*T)->Lchild);
26         CreateBiTree(&(*T)->Rchild);
27 
28     }
29 }
30 void PreOrderTraverse(BiTree T)
31 {
32     if(T == NULL)
33         return ;
34     printf("%c",T->data);
35     PreOrderTraverse(T->Lchild);
36     PreOrderTraverse(T->Rchild);
37 }
38 
39 void InOrderTraverse(BiTree T)
40 {
41     if(T == NULL)
42         return ;
43     InOrderTraverse(T->Lchild);
44     printf("%c",T->data);
45     InOrderTraverse(T->Rchild);
46 }
47 void PostOrderTraverse(BiTree T)
48 {
49     if(T == NULL)
50         return ;
51     PostOrderTraverse(T->Lchild);
52     PostOrderTraverse(T->Rchild);
53     printf("%c",T->data);
54 }
55 int main()
56 {
57     BiTree T;
58     CreateBiTree(&T);
59     PreOrderTraverse(T);
60     printf("\n");
61     InOrderTraverse(T);
62     printf("\n");
63     PostOrderTraverse(T);
64     printf("\n");
65     return 0;
66 }
View Code

 

posted @ 2016-11-14 20:49  biu~biu~biu~  阅读(221)  评论(0编辑  收藏  举报