当前位置:   article > 正文

数据结构——二叉树的遍历及应用_以二叉链表作为二叉树的存储结构,编写以下算法: (1)输出二叉树的先序遍历序列、中

以二叉链表作为二叉树的存储结构,编写以下算法: (1)输出二叉树的先序遍历序列、中

建立一棵二叉树,试编程实现二叉树的如下基本操作:

  1. 按先序序列构造一棵二叉链表表示的二叉树T;
  2. 对这棵二叉树进行遍历:先序、中序、后序遍历,分别输出结点的遍历序列;
  3. 求二叉树的叶结点数目;

设计要求:

首先设计一个含有多个菜单项的主控菜单程序,然后再为这些菜单项配上相应的功能。

主控菜单设计要求:

程序运行后,给出6个菜单项的内容和输入提示:

  1. 创建二叉树(先序)
  2. 先序遍历并输出遍历序列
  3. 中序遍历并输出遍历序列
  4. 后序遍历并输出遍历序列
  5. 统计二叉树的叶子结点数
  6. 退出

请选择菜单1--6:

功能要求:

完成各菜单的功能,并能正确显示遍历结果和叶子数目

从键盘接受输入(先序),以二叉链表作为存储结构,建立二叉树(以先序来建立)

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define OVERFLOW -2
  4. #define OK 1
  5. typedef char TElemType;
  6. //二叉链表结点定义
  7. typedef struct BiTNode{
  8. TElemType data;
  9. struct BiTNode *lchild,*rchild;
  10. }BiTNode,*BiTree;
  11. BiTree T=NULL;
  12. //先序创建二叉树
  13. void CreateBiTree(BiTree &T)
  14. {
  15. char ch;
  16. scanf("%c",&ch);
  17. if(ch==' ')
  18. T=NULL;
  19. else{
  20. if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))
  21. exit(OVERFLOW);
  22. T->data=ch;
  23. CreateBiTree(T->lchild);
  24. CreateBiTree(T->rchild);
  25. }
  26. }
  27. //先序遍历并输出遍历序列
  28. void PreOrder(BiTree T)
  29. {
  30. if(T){
  31. printf("%c",T->data);
  32. PreOrder(T->lchild);
  33. PreOrder(T->rchild);
  34. }
  35. }
  36. //中序遍历并输出遍历序列
  37. void InOrder(BiTree T)
  38. {
  39. if(T){
  40. InOrder(T->lchild);
  41. printf("%c",T->data);
  42. InOrder(T->rchild);
  43. }
  44. }
  45. //后序遍历并输出遍历序列
  46. void PostOrder(BiTree T)
  47. {
  48. if(T){
  49. PostOrder(T->lchild);
  50. PostOrder(T->rchild);
  51. printf("%c",T->data);
  52. }
  53. }
  54. //统计二叉树的叶子结点数
  55. void CountLeaf(BiTree T,int &count)
  56. {
  57. if(T){
  58. if((!T->lchild)&&(!T->rchild))
  59. count++;
  60. CountLeaf(T->lchild,count);
  61. CountLeaf(T->rchild,count);
  62. }
  63. }
  64. //主函数
  65. int main(){
  66. int choice;
  67. int count=0;
  68. printf("**********二叉树的遍历及应用**********\n");
  69. printf("1.创建二叉树(先序)\n");
  70. printf("2.先序遍历并输出遍历序列\n");
  71. printf("3.中序遍历并输出遍历序列\n");
  72. printf("4.后序遍历并输出遍历序列\n");
  73. printf("5.统计二叉树的叶子结点数\n");
  74. printf("6.退出\n");
  75. while(choice)
  76. {
  77. printf("请选择菜单1--6:\n");
  78. scanf("%d",&choice);
  79. if(choice>6||choice<=0)
  80. {
  81. printf("您输入的数据有误!\n");
  82. return 0;
  83. }
  84. while(choice<=6)
  85. {
  86. if(choice==1)
  87. {
  88. getchar();
  89. printf("先序创建二叉树:\n");
  90. printf("请输入字符串\n");
  91. CreateBiTree(T);
  92. break;
  93. }
  94. if(choice==2)
  95. {
  96. printf("先序遍历并输出遍历序列:\n");
  97. PreOrder(T);
  98. printf("\n");
  99. break;
  100. }
  101. if(choice==3)
  102. {
  103. printf("中序遍历并输出遍历序列:\n");
  104. InOrder(T);
  105. printf("\n");
  106. break;
  107. }
  108. if(choice==4)
  109. {
  110. printf("后序遍历并输出遍历序列:\n");
  111. PostOrder(T);
  112. printf("\n");
  113. break;
  114. }
  115. if(choice==5)
  116. {
  117. printf("统计二叉树的叶子结点数:\n");
  118. CountLeaf(T,count);
  119. printf("叶子结点数为:%d\n",count);
  120. break;
  121. }
  122. if(choice==6)
  123. {
  124. printf("退出\n");
  125. return 0;
  126. }
  127. }
  128. }
  129. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/718367
推荐阅读
相关标签
  

闽ICP备14008679号