二叉树的递归与非递归遍历(二叉链表结构)

举报
悦来客栈的老板 发表于 2020/12/30 00:31:03 2020/12/30
【摘要】 # include <stdio.h># include <iostream.h># include <malloc.h># include <stdlib.h> # define STACK_INIT_SIZE 100# define STACKINCREMENT 10# define OK 1# define ERRO...

 

   
  1. # include <stdio.h>
  2. # include <iostream.h>
  3. # include <malloc.h>
  4. # include <stdlib.h>
  5. # define STACK_INIT_SIZE 100
  6. # define STACKINCREMENT 10
  7. # define OK 1
  8. # define ERROR 0
  9. # define OVERFLOW -1
  10. typedef char TElemtype;
  11. typedef struct BiTNode
  12. {
  13. TElemtype data;
  14. struct BiTNode *lchild,*rchild;
  15. }BiTNode,*BiTree;
  16. typedef struct
  17. {
  18. BiTree *top;
  19. BiTree *base;
  20. int stacksize;
  21. }SqStack;
  22. int Init_Stack(SqStack &S)
  23. {
  24. S.base = (BiTree *) malloc (STACK_INIT_SIZE * sizeof (BiTree));
  25. if (!S.base)
  26. exit (OVERFLOW);
  27. S.top = S.base;
  28. S.stacksize = STACK_INIT_SIZE;
  29. return OK;
  30. }
  31. int GetTop(SqStack S,BiTree &e)
  32. {
  33. if (S.base == S.top )
  34. return ERROR;
  35. e = *(S.top -1);
  36. return OK;
  37. }
  38. int StackEmpty(SqStack S)
  39. {
  40. if (S.top == S.base )
  41. return OK;
  42. else
  43. return ERROR;
  44. }
  45. int Push(SqStack &S,BiTree e)
  46. {
  47. if (S.top - S.base >= S.stacksize )
  48. {
  49. S.base = (BiTree *)realloc (S.base,(S.stacksize + STACKINCREMENT)*sizeof (BiTree));
  50. if (!S.base)
  51. exit (OVERFLOW);
  52. S.top = S.base + S.stacksize;
  53. S.stacksize += STACKINCREMENT;
  54. }
  55. *S.top++ = e;
  56. return OK;
  57. }
  58. int Pop(SqStack &S,BiTree &e)
  59. {
  60. if (S.top == S.base )
  61. return ERROR;
  62. e = * --S.top;
  63. return OK;
  64. }
  65. int CreateBiTree(BiTree &T)
  66. {
  67. TElemtype ch;
  68. cin>>ch;
  69. if (ch == '#')
  70. T = NULL;
  71. else
  72. {
  73. T = (BiTNode *) malloc (sizeof (BiTNode));
  74. if (!T)
  75. exit (OVERFLOW);
  76. T->data = ch;
  77. CreateBiTree(T->lchild);
  78. CreateBiTree(T->rchild);
  79. }
  80. return OK;
  81. }
  82. void PreOrderTraverse (BiTree T)
  83. {
  84. if (T)
  85. {
  86. cout<<T->data ;
  87. PreOrderTraverse(T->lchild);
  88. PreOrderTraverse(T->rchild);
  89. }
  90. }
  91. void InOrderTraverse (BiTree T)
  92. {
  93. if (T)
  94. {
  95. InOrderTraverse(T->lchild);
  96. cout<<T->data;
  97. InOrderTraverse(T->rchild);
  98. }
  99. }
  100. void PostOrderTraverse (BiTree T)
  101. {
  102. if (T)
  103. {
  104. PostOrderTraverse(T->lchild);
  105. PostOrderTraverse(T->rchild);
  106. cout<<T->data ;
  107. }
  108. }
  109. void PreOrderTraverse1 (BiTree T)
  110. {
  111. SqStack S;
  112. BiTree p;
  113. Init_Stack(S);
  114. Push(S,T);
  115. while (! StackEmpty(S))
  116. {
  117. while (GetTop(S,p) && p)
  118. {
  119. cout<<p->data ;
  120. Push(S,p->lchild);
  121. }
  122. Pop(S,p);
  123. if (!StackEmpty(S))
  124. {
  125. Pop(S,p);
  126. Push(S,p->rchild);
  127. }
  128. }
  129. }
  130. void InOrderTraverse1 (BiTree T)
  131. {
  132. SqStack S;
  133. BiTree p;
  134. Init_Stack(S);
  135. Push(S,T);
  136. while (! StackEmpty(S))
  137. {
  138. while (GetTop(S,p) && p)
  139. Push(S,p->lchild);
  140. Pop(S,p);
  141. if (!StackEmpty(S))
  142. {
  143. Pop(S,p);
  144. cout<<p->data ;
  145. Push(S,p->rchild);
  146. }
  147. }
  148. }
  149. void PostOrderTraverse1(BiTree T)
  150. {//本源码来源于网络
  151. SqStack S;
  152. BiTree p, p1;
  153. p = T;
  154. p1 = NULL;
  155. Init_Stack(S);
  156. while(p || !StackEmpty(S))
  157. {
  158. if(p){
  159. Push(S,p);
  160. //visit,先序
  161. p=p->lchild;
  162. }
  163. else
  164. {
  165. GetTop(S,p);
  166. //visit,中序
  167. if(p->rchild==p1)
  168. {
  169. Pop(S,p1);p=NULL;
  170. //visit,后序
  171. cout<<p1->data;
  172. }
  173. else
  174. {
  175. p=p->rchild;p1=NULL;
  176. }
  177. }
  178. }
  179. }
  180. int main(void)
  181. {
  182. BiTree T;
  183. cout<<"请按照先序序列建立一棵二叉树,#代表空树!"<<endl;
  184. CreateBiTree(T);
  185. cout<<"先序遍历二叉树:";
  186. PreOrderTraverse(T);
  187. cout<<endl;
  188. cout<<"中序遍历二叉树:";
  189. InOrderTraverse(T);
  190. cout<<endl;
  191. cout<<"后序遍历二叉树:";
  192. PostOrderTraverse(T);
  193. cout<<endl;
  194. cout<<"非递归先序遍历二叉树:";
  195. PreOrderTraverse1(T);
  196. cout<<endl;
  197. cout<<"非递归中序遍历二叉树:";
  198. InOrderTraverse1(T);
  199. cout<<endl;
  200. cout<<"非递归后序遍历二叉树:";
  201. PostOrderTraverse(T);
  202. cout<<endl;
  203. return 0;
  204. }



 运行结果:

文章来源: blog.csdn.net,作者:悦来客栈的老板,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq523176585/article/details/6758451

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。