1. #include<stdio.h>
    2. #include<stdlib.h>
    3. typedef struct BiTNode{
    4. char data;
    5. struct BiTNode *L;
    6. struct BiTNode *R;
    7. }BiTNode,*BiTree;
    8. //前序创建二叉树
    9. void PreCreatBiTree(BiTree*t);
    10. //前序遍历二叉树
    11. void PreShowBiTree(BiTree t);
    12. //中序遍历二叉树
    13. void MidShowBiTree(BiTree t);
    14. //后序遍历二叉树
    15. void RearShowBiTree(BiTree t);
    16. int main(){
    17. //定义二叉树
    18. BiTree t;
    19. printf("请输入任意字符,#代表空\n");
    20. PreCreatBiTree(&t);
    21. printf("前序遍历结果为:\n");
    22. PreShowBiTree(t);
    23. printf("\n");
    24. printf("中序遍历结果为:\n");
    25. MidShowBiTree(t);
    26. printf("\n");
    27. printf("后序遍历结果为:\n");
    28. RearShowBiTree(t);
    29. printf("\n");
    30. }
    31. void PreCreatBiTree(BiTree*t){
    32. char ch;
    33. scanf("%c",&ch);
    34. if(ch == '#')
    35. {
    36. *t = NULL;
    37. }
    38. else
    39. {
    40. *t = (BiTree)malloc(sizeof(BiTNode));
    41. if(NULL == (*t))return;
    42. (*t) -> data = ch;
    43. PreCreatBiTree(&(*t)->L);
    44. PreCreatBiTree(&(*t)->R);
    45. }
    46. }
    47. void PreShowBiTree(BiTree t){
    48. if(t != NULL)
    49. {
    50. printf("%c",t->data);
    51. PreShowBiTree(t->L);
    52. PreShowBiTree(t->R);
    53. }
    54. }
    55. void MidShowBiTree(BiTree t){
    56. if(t != NULL)
    57. {
    58. MidShowBiTree(t->L);
    59. printf("%c",t ->data);
    60. MidShowBiTree(t->R);
    61. }
    62. }
    63. void RearShowBiTree(BiTree t){
    64. if(t != NULL)
    65. {
    66. RearShowBiTree(t->L);
    67. RearShowBiTree(t->R);
    68. printf("%c",t->data);
    69. }
    70. }