当前位置:   article > 正文

二叉树的括号表示法、遍历和打印

二叉树的括号表示法

题目:

给你一个括号表示法表示的二叉树,建立一棵用二叉链表方式存储的二叉树,并利用凹凸法进行二叉树的打印;并对其进行遍历(先序、中序、后序和层序),并打印输出遍历结果。

例子:

输入:A(B(C,D(E,F(G))))

输出:

先序遍历:ABCDEFG

中序遍历:CBEDGFA

后序遍历:CEGFDBA

层序遍历:ABCDEFG

 

代码:

  1. #pragma GCC optimize(3,"Ofast","inline")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<algorithm>
  5. #include<string>
  6. #include<vector>
  7. #include<cstring>
  8. #include<queue>
  9. #include<stack>
  10. #include<list>
  11. #include<map>
  12. #include<set>
  13. #include<cmath>
  14. #include<sstream>
  15. #include<cstdlib>
  16. #include<bitset>
  17. #include<climits>
  18. #include<functional>
  19. #define F(i,s,t) for(int i=(s);i<=(t);i++)
  20. #define D(i,s,t) for(int i=(s);i>=(t);i--)
  21. #define dBug(i) printf("Value=%d\n",i)
  22. #define ddBug(i,j) printf("Value=%d %d\n",i,j)
  23. #define ed putchar('\n')
  24. #define FO freopen("D:\\in.txt","r",stdin)
  25. #define IOS cin.tie(0) ,cout.tie(0), cout.sync_with_stdio(0)
  26. typedef long long ll;
  27. //const int INF = 1 << 30;
  28. //const double EPS = 1e-6;
  29. //#define MX 102
  30. //#define Mod 10000
  31. using namespace std;
  32. typedef struct Node{
  33. char data;
  34. Node* LChild;
  35. Node* RChild;
  36. }BiTree;
  37. //构造二叉链表
  38. BiTree* CreatBiTree(char str1[])
  39. {
  40. static BiTree *root = NULL;
  41. BiTree *demoFather[600], *TreePoint;
  42. int demoPoint = -1, curSurTree, strPoint = 0;
  43. char temp;
  44. temp = str1[strPoint]; //ch指向str1第一个字符,用[]访问
  45. while (temp != '\0'){
  46. switch (temp){
  47. case '(':
  48. demoPoint++;
  49. demoFather[demoPoint] = TreePoint; //记录括号里元素的父节点
  50. curSurTree = 1;
  51. break;
  52. case')':
  53. demoPoint--;
  54. break;
  55. case',':
  56. curSurTree = 2;
  57. break;
  58. default:
  59. TreePoint = (BiTree*)malloc(sizeof(BiTree));
  60. TreePoint->data = temp;
  61. TreePoint->LChild = TreePoint->RChild = NULL;
  62. if (root == NULL){
  63. root = TreePoint;
  64. }//if
  65. else{
  66. curSurTree == 1 ? demoFather[demoPoint]->LChild = TreePoint : demoFather[demoPoint]->RChild = TreePoint;
  67. }//else
  68. }//switch
  69. strPoint++;
  70. temp = str1[strPoint];
  71. }//while
  72. return root;
  73. }
  74. /*先序遍历二叉树, root为指向二叉树根结点的指针*/
  75. void PreOrderRecursion(BiTree *root)
  76. {
  77. if (root != NULL){
  78. printf("%c",root->data);
  79. PreOrderRecursion(root->LChild);
  80. PreOrderRecursion(root->RChild);
  81. }//if
  82. }
  83. /*中序遍历二叉树, root为指向二叉树根结点的指针*/
  84. void InOrderRecursion(BiTree *root)
  85. {
  86. if (root != NULL){
  87. InOrderRecursion(root->LChild);
  88. printf("%c", root->data);
  89. InOrderRecursion(root->RChild);
  90. }//if
  91. }
  92. /* 后序遍历二叉树,root为指向二叉树(或某一子树)根结点的指针*/
  93. void PostOrderRecursion(BiTree *root)
  94. {
  95. if (root != NULL){
  96. PostOrderRecursion(root->LChild);
  97. PostOrderRecursion(root->RChild);
  98. printf("%c",root->data);
  99. }//if
  100. }
  101. /* 先序遍历二叉树的非递归算法 */
  102. void PreOrder(BiTree *root)
  103. {
  104. BiTree *temp;
  105. stack<BiTree*> S;
  106. temp = root;
  107. while (!S.empty() || temp != NULL){
  108. while (temp != NULL){
  109. printf("%c", temp->data);
  110. S.push(temp);
  111. temp = temp->LChild;
  112. }//inner whlie
  113. if (!S.empty()){
  114. temp = S.top();
  115. S.pop();
  116. temp = temp->RChild;
  117. }//if
  118. }//extren while
  119. }
  120. /* 中序遍历二叉树的非递归算法 */
  121. void InOrder(BiTree *root)
  122. {
  123. BiTree *temp;
  124. stack<BiTree*> S;
  125. temp = root;
  126. while (!S.empty() || temp != NULL){
  127. while (temp != NULL){
  128. S.push(temp);
  129. temp = temp->LChild;
  130. }//inner whlie
  131. if (!S.empty()){
  132. temp = S.top();
  133. S.pop();
  134. printf("%c",temp->data);
  135. temp = temp->RChild;
  136. }//if
  137. }//extren while
  138. }
  139. /* 后序遍历二叉树的非递归算法 */
  140. void PostOrder(BiTree *root)
  141. {
  142. stack<BiTree*> S;
  143. BiTree *temp = root, *r = NULL;
  144. while (temp || !S.empty()){
  145. if (temp){
  146. S.push(temp);
  147. temp = temp->LChild;
  148. }//if
  149. else{
  150. temp = S.top();
  151. if (temp->RChild && temp->RChild != r){
  152. temp = temp->RChild;
  153. }//if
  154. else{
  155. temp = S.top();
  156. S.pop();
  157. printf("%c",temp->data);
  158. r = temp; // r 记录弹出的结点
  159. temp = NULL; //temp 调到NULL , 为了不让再往栈中添加结点了
  160. }//else
  161. }//extren else
  162. }//while
  163. }
  164. /* 层序遍历二叉树 */
  165. void LayerOrder(BiTree *root)
  166. {
  167. BiTree *temp;
  168. queue<BiTree*> Q;
  169. Q.push(root);
  170. while (!Q.empty()){
  171. temp = Q.front();
  172. Q.pop();
  173. printf("%c",temp->data);
  174. if (temp->LChild != NULL){
  175. Q.push(temp->LChild);
  176. }
  177. if (temp->RChild != NULL){
  178. Q.push(temp->RChild);
  179. }
  180. }//while
  181. }
  182. void PrintTree(BiTree *root, int nLayer)
  183. {
  184. if (root == NULL){
  185. return;
  186. }
  187. PrintTree(root->RChild,nLayer + 3);
  188. for (int i = 0; i < nLayer; i++){
  189. printf(" ");
  190. }
  191. printf("%c\n",root->data);
  192. PrintTree(root->LChild, nLayer + 3);
  193. }
  194. int main()
  195. {
  196. BiTree *T = NULL;
  197. char str1[1000]; //请输入括号法表示的二叉树序列
  198. printf("请输入括号法表示的二叉树序列:\n");
  199. scanf("%s", str1);
  200. printf("括号法表示的二叉树序列为:%s\n", str1);
  201. T = CreatBiTree(str1);//&T为BiNode类型指针的地址
  202. printf("先序遍历输出序列为:");
  203. PreOrderRecursion(T);
  204. printf("\n中序遍历输出序列为:");
  205. InOrderRecursion(T);
  206. printf("\n后序遍历输出序列为:");
  207. PostOrderRecursion(T);
  208. printf("\n先序非递归遍历输出序列为:");
  209. PreOrder(T);
  210. printf("\n中序非递归遍历输出序列为:");
  211. InOrder(T);
  212. printf("\n后序非递归遍历输出序列为:");
  213. PostOrder(T);
  214. printf("\n层序遍历输出序列为:");
  215. LayerOrder(T);
  216. printf("\n竖向打印二叉树:\n");
  217. PrintTree(T, 1);
  218. return 0;
  219. }

 

细节

上面代码实现了递归和非递归的遍历。

非递归遍历中,用栈来存储结点,为了可以在遍历它的孩子时,还可以通过栈找到孩子结点的父结点。

而后序非递归遍历较前两种遍历方法比较难实现,原因在于需要遍历完左子树,遍历完右子树,最后才去访问根节点。这样栈顶结点可能会从他的左子树返回,也有可能从他的右子树返回,需要区分这种情况,如果是第一次从左子树返回,那么还需要去遍历其右子树,如果是从右子树返回,那么直接返回该结点就可以了。这里使用辅助指针来区分来源。

  1. temp = S.top();
  2. if (temp->RChild && temp->RChild != r){
  3. temp = temp->RChild;
  4. }//if
  5. else{
  6. temp = S.top();
  7. S.pop();
  8. printf("%c",temp->data);
  9. r = temp; // r 记录弹出的结点
  10. temp = NULL; //temp 调到NULL , 为了不让再往栈中添加结点了
  11. }//else

 

好的!下篇实现Moris遍历!

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/233329
推荐阅读
相关标签
  

闽ICP备14008679号