二叉树的非递归中序遍历(二叉线索存储结构)

举报
悦来客栈的老板 发表于 2020/12/30 01:34:56 2020/12/30
【摘要】   # include <stdio.h># include <malloc.h># include <stdlib.h># include <iostream.h> # define OK 1# define ERROR 0# define OVERFLOW -1 typedef char TElem...


   
  1. # include <stdio.h>
  2. # include <malloc.h>
  3. # include <stdlib.h>
  4. # include <iostream.h>
  5. # define OK 1
  6. # define ERROR 0
  7. # define OVERFLOW -1
  8. typedef char TElemType;
  9. typedef enum PointerTag { Link, Thread };
  10. typedef struct BiThrNode
  11. {
  12. TElemType data;
  13. struct BiThrNode *lchild, *rchild;
  14. PointerTag LTag, RTag;
  15. }BiThrNode,*BiThrTree;
  16. BiThrTree pre;
  17. void InThreading(BiThrTree p)
  18. {
  19. if (p)
  20. {
  21. InThreading(p->lchild);
  22. if (!p->lchild)
  23. {
  24. p->LTag = Thread;
  25. p->lchild = pre;
  26. }
  27. if (! pre->rchild)
  28. {
  29. pre->RTag = Thread;
  30. pre->rchild = p;
  31. }
  32. pre = p;
  33. InThreading(p->rchild);
  34. }
  35. }
  36. int InOrderThreading(BiThrTree &Thrt, BiThrTree T)
  37. {
  38. Thrt = (BiThrTree) malloc (sizeof (BiThrNode));
  39. if (!Thrt)
  40. exit (OVERFLOW);
  41. Thrt->LTag = Link;
  42. Thrt->RTag = Thread;
  43. Thrt->rchild = Thrt;
  44. if (!T)
  45. Thrt->lchild = Thrt;
  46. else
  47. {
  48. Thrt->lchild = T;
  49. pre = Thrt;
  50. InThreading(T);
  51. pre->rchild = Thrt;
  52. pre->RTag = Thread;
  53. Thrt->rchild = pre;
  54. }
  55. return OK;
  56. }
  57. int CreateBiThrTree(BiThrTree &T)
  58. {
  59. TElemType ch;
  60. cin>>ch;
  61. if (ch == '#')
  62. T = NULL;
  63. else
  64. {
  65. T= (BiThrTree) malloc (sizeof (BiThrNode));
  66. T->data = ch;
  67. CreateBiThrTree(T->lchild);
  68. if (T->lchild)
  69. T->LTag = Link;
  70. CreateBiThrTree(T->rchild);
  71. if (T->rchild)
  72. T->RTag = Link;
  73. }
  74. return OK;
  75. }
  76. int InOrderTraverse_Thr(BiThrTree T,int (*Visit)(TElemType e))
  77. {
  78. BiThrTree p;
  79. p = T->lchild ;
  80. while (p != T)
  81. {
  82. while (p->LTag == Link)
  83. p = p->lchild ;
  84. if (! Visit(p->data))
  85. return ERROR;
  86. while (p->RTag == Thread && p->rchild != T)
  87. {
  88. p = p->rchild ;
  89. Visit(p->data);
  90. }
  91. p = p->rchild ;
  92. }
  93. return OK;
  94. }
  95. int Print(TElemType e)
  96. {
  97. cout<<e;
  98. return OK;
  99. }
  100. int main(void)
  101. {
  102. BiThrTree T,H;
  103. cout<<"请按先序建立一棵二叉树,#代表空树!"<<endl;
  104. CreateBiThrTree(T);
  105. InOrderThreading(H,T);
  106. cout<<"它的中序遍历是:";
  107. InOrderTraverse_Thr(H,Print);
  108. cout<<endl;
  109. return 0;
  110. }



结果图:

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

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

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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