二叉树的创建+递归遍历+非递归遍历

落日映苍穹つ 2022-05-19 01:58 398阅读 0赞
  1. #include<iostream>
  2. #include<stdlib.h>
  3. #define MAXSIZE 100
  4. typedef char ElementType;
  5. using namespace std;
  6. typedef struct BTNode{
  7. ElementType data;
  8. struct BTNode *lChild;
  9. struct BTNode *rChild;
  10. }BTNode;
  11. void createBinaryTree(BTNode *&bt)
  12. {
  13. ElementType data;
  14. bt = (BTNode *)malloc(sizeof(BTNode));
  15. cin>>data;
  16. if(data == '#')
  17. {
  18. bt = NULL;
  19. }
  20. else
  21. {
  22. bt->data = data;
  23. createBinaryTree(bt->lChild);
  24. createBinaryTree(bt->rChild);
  25. }
  26. }
  27. void preOrderRecursionVisit(BTNode *bt)
  28. {
  29. if(bt == NULL)
  30. return;
  31. else
  32. {
  33. cout<<bt->data<<" ";
  34. preOrderRecursionVisit(bt->lChild);
  35. preOrderRecursionVisit(bt->rChild);
  36. }
  37. }
  38. void preOrderNonRecursionVisit(BTNode *bt)
  39. {
  40. if(bt == NULL)
  41. return;
  42. BTNode *stack[MAXSIZE], *tNode;
  43. int top = -1;
  44. stack[++top] = bt;
  45. while(top != -1)
  46. {
  47. tNode = stack[top--];
  48. cout<<tNode->data<<" ";
  49. if(tNode->rChild != NULL)
  50. stack[++top] = tNode->rChild;
  51. if(tNode->lChild != NULL)
  52. stack[++top] = tNode->lChild;
  53. }
  54. cout<<endl;
  55. }
  56. void inOrderRecursionVisit(BTNode *bt)
  57. {
  58. if(bt == NULL)
  59. return;
  60. else
  61. {
  62. inOrderRecursionVisit(bt->lChild);
  63. cout<<bt->data<<" ";
  64. inOrderRecursionVisit(bt->rChild);
  65. }
  66. }
  67. void inOrderNonRecursionVisit(BTNode *bt)
  68. {
  69. if(bt == NULL)
  70. return;
  71. BTNode *stack[MAXSIZE], *tNode;
  72. int top = -1;
  73. tNode = bt;
  74. while(top != -1 || tNode != NULL)
  75. {
  76. while(tNode != NULL)
  77. {
  78. stack[++top] = tNode;
  79. tNode = tNode->lChild;
  80. }
  81. if(top != -1)
  82. {
  83. tNode = stack[top--];
  84. cout<<tNode->data<<" ";
  85. if(tNode->rChild != NULL)
  86. tNode = tNode->rChild;
  87. else
  88. tNode = NULL;
  89. }
  90. }
  91. cout<<endl;
  92. }
  93. void postOrderRecursionVisit(BTNode *bt)
  94. {
  95. if(bt == NULL)
  96. return;
  97. else
  98. {
  99. postOrderRecursionVisit(bt->lChild);
  100. postOrderRecursionVisit(bt->rChild);
  101. cout<<bt->data<<" ";
  102. }
  103. }
  104. void postOrderNonRecursionVisit(BTNode *bt)
  105. {
  106. if(bt == NULL)
  107. return;
  108. BTNode *tNode, *stack1[MAXSIZE], *stack2[MAXSIZE];
  109. int top1 = -1, top2 = -1;
  110. stack1[++top1] = bt;
  111. while(top1 != -1)
  112. {
  113. tNode = stack1[top1--];
  114. stack2[++top2] = tNode;
  115. if(tNode->lChild != NULL)
  116. stack1[++top1] = tNode->lChild;
  117. if(tNode->rChild != NULL)
  118. stack1[++top1] = tNode->rChild;
  119. }
  120. while(top2 != -1)
  121. {
  122. tNode = stack2[top2--];
  123. cout<<tNode->data<<" ";
  124. }
  125. cout<<endl;
  126. }
  127. int main()
  128. {
  129. BTNode *bt = NULL;
  130. //124##57##8##3#69###
  131. createBinaryTree(bt);
  132. preOrderRecursionVisit(bt);
  133. cout<<endl;
  134. preOrderNonRecursionVisit(bt);
  135. inOrderRecursionVisit(bt);
  136. cout<<endl;
  137. inOrderNonRecursionVisit(bt);
  138. postOrderRecursionVisit(bt);
  139. cout<<endl;
  140. postOrderNonRecursionVisit(bt);
  141. }

发表评论

表情:
评论列表 (有 0 条评论,398人围观)

还没有评论,来说两句吧...

相关阅读