zoukankan      html  css  js  c++  java
  • 线索二叉树

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include "string.h"
    3. #include "stdio.h"
    4. #include "stdlib.h"
    5. #include "io.h"
    6. #include "math.h"
    7. #include "time.h"
    8. #define OK 1
    9. #define ERROR 0
    10. #define TRUE 1
    11. #define FALSE 0
    12. #define MAXSIZE 100 /* 存储空间初始分配量 */
    13. typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
    14. typedef char TElemType;
    15. typedef enum { Link, Thread } PointerTag; /* Link==0表示指向左右孩子指针, */
    16. /* Thread==1表示指向前驱或后继的线索 */
    17. typedef struct BiThrNode /* 二叉线索存储结点结构 */
    18. {
    19. TElemType data; /* 结点数据 */
    20. struct BiThrNode *lchild, *rchild; /* 左右孩子指针 */
    21. PointerTag LTag;
    22. PointerTag RTag; /* 左右标志 */
    23. } BiThrNode, *BiThrTree;
    24. TElemType Nil = '#'; /* 字符型以空格符为空 */
    25. Status visit(TElemType e)
    26. {
    27. printf("%c ", e);
    28. return OK;
    29. }
    30. /* 按前序输入二叉线索树中结点的值,构造二叉线索树T */
    31. /* 0(整型)/空格(字符型)表示空结点 */
    32. Status CreateBiThrTree(BiThrTree *T)
    33. {
    34. TElemType h;
    35. scanf("%c", &h);
    36. if (h == Nil)
    37. *T = NULL;
    38. else
    39. {
    40. *T = (BiThrTree)malloc(sizeof(BiThrNode));
    41. if (!*T)
    42. exit(OVERFLOW);
    43. (*T)->data = h; /* 生成根结点(前序) */
    44. CreateBiThrTree(&(*T)->lchild); /* 递归构造左子树 */
    45. if ((*T)->lchild) /* 有左孩子 */
    46. (*T)->LTag = Link;
    47. CreateBiThrTree(&(*T)->rchild); /* 递归构造右子树 */
    48. if ((*T)->rchild) /* 有右孩子 */
    49. (*T)->RTag = Link;
    50. }
    51. return OK;
    52. }
    53. BiThrTree pre; /* 全局变量,始终指向刚刚访问过的结点 */
    54. /* 中序遍历进行中序线索化 */
    55. void InThreading(BiThrTree p)
    56. {
    57. if (p)
    58. {
    59. InThreading(p->lchild); /* 递归左子树线索化 */
    60. if (!p->lchild) /* 没有左孩子 */
    61. {
    62. p->LTag = Thread; /* 前驱线索 */
    63. p->lchild = pre; /* 左孩子指针指向前驱 */
    64. }
    65. if (!pre->rchild) /* 前驱没有右孩子 */
    66. {
    67. pre->RTag = Thread; /* 后继线索 */
    68. pre->rchild = p; /* 前驱右孩子指针指向后继(当前结点p) */
    69. }
    70. pre = p; /* 保持pre指向p的前驱 */
    71. InThreading(p->rchild); /* 递归右子树线索化 */
    72. }
    73. }
    74. /* 中序遍历二叉树T,并将其中序线索化,Thrt指向头结点 */
    75. Status InOrderThreading(BiThrTree *Thrt, BiThrTree T)
    76. {
    77. *Thrt = (BiThrTree)malloc(sizeof(BiThrNode));
    78. if (!*Thrt)
    79. exit(OVERFLOW);
    80. (*Thrt)->LTag = Link; /* 建头结点 */
    81. (*Thrt)->RTag = Thread;
    82. (*Thrt)->rchild = (*Thrt); /* 右指针回指 */
    83. if (!T) /* 若二叉树空,则左指针回指 */
    84. (*Thrt)->lchild = *Thrt;
    85. else
    86. {
    87. (*Thrt)->lchild = T;
    88. pre = (*Thrt);
    89. InThreading(T); /* 中序遍历进行中序线索化 */
    90. pre->rchild = *Thrt;
    91. pre->RTag = Thread; /* 最后一个结点线索化 */
    92. (*Thrt)->rchild = pre;
    93. }
    94. return OK;
    95. }
    96. /* 中序遍历二叉线索树T(头结点)的非递归算法 */
    97. Status InOrderTraverse_Thr(BiThrTree T)
    98. {
    99. BiThrTree p;
    100. p = T->lchild; /* p指向根结点 */
    101. while (p != T)
    102. { /* 空树或遍历结束时,p==T */
    103. while (p->LTag == Link)
    104. p = p->lchild;
    105. if (!visit(p->data)) /* 访问其左子树为空的结点 */
    106. return ERROR;
    107. while (p->RTag == Thread&&p->rchild != T)
    108. {
    109. p = p->rchild;
    110. visit(p->data); /* 访问后继结点 */
    111. }
    112. p = p->rchild;
    113. }
    114. return OK;
    115. }
    116. int main()
    117. {
    118. BiThrTree H, T;
    119. printf("请按前序输入二叉树(如:'ABDH##I##EJ###CF##G##') ");
    120. CreateBiThrTree(&T); /* 按前序产生二叉树 */
    121. InOrderThreading(&H, T); /* 中序遍历,并中序线索化二叉树 */
    122. printf("中序遍历(输出)二叉线索树: ");
    123. InOrderTraverse_Thr(H); /* 中序遍历(输出)二叉线索树 */
    124. printf(" ");
    125. return 0;
    126. }





  • 相关阅读:
    DES算法实现
    2018-2019-2 20175226 实验五《网络编程与安全》实验报告
    2018-2019-2 20175226王鹏雲 实验四《Android程序设计》实验报告
    第05组 Beta冲刺(1/4)
    Alpha事后诸葛亮
    第05组 Alpha冲刺(4/4)
    第05组 Alpha冲刺(3/4)
    第05组 Alpha冲刺(2/4)
    第05组 Alpha冲刺(1/4)
    第05组 团队Git现场编程实战
  • 原文地址:https://www.cnblogs.com/zhuzhenfeng/p/4631671.html
Copyright © 2011-2022 走看看