1. /*
    2. 该文件用于创建三类线索二叉树,即中序线索二叉树、先序线索二叉树、后序线索二叉树
    3. */
    4. struct biTree {
    5. char data;
    6. struct biTree *lchild;
    7. struct biTree *rchild;
    8. int ltag, rtag;//用于进行标记
    9. };
    10. #include <stdio.h>
    11. void inThread(biTree *p, biTree *pre ) {//中序线索二叉树
    12. if (p!=NULL) {
    13. inThread(p->lchild,pre);
    14. if (p->lchild==NULL) {//如果左子树为空,建立前驱线索
    15. p->lchild = pre;
    16. p->ltag = 1;
    17. }
    18. if (pre!=NULL && pre->rchild==NULL) {//建立前驱结点的后继线索
    19. pre->rchild = p;
    20. pre->rtag = 1;
    21. }
    22. pre = p;
    23. inThread(p->rchild,pre);
    24. }
    25. }
    26. void preThread(biTree *p, biTree *pre) {//先序线索二叉树
    27. if (p != NULL) {
    28. if (p->lchild == NULL) {//如果左子树为空,建立前驱线索
    29. p->lchild = pre;
    30. p->ltag = 1;
    31. }
    32. if (pre != NULL && pre->rchild == NULL) {//建立前驱结点的后继线索
    33. pre->rchild = p;
    34. pre->rtag = 1;
    35. }
    36. pre = p;
    37. inThread(p->lchild, pre);
    38. inThread(p->rchild, pre);
    39. }
    40. }
    41. void postThread(biTree *p, biTree *pre) {//后序线索二叉树
    42. if (p != NULL) {
    43. inThread(p->lchild, pre);
    44. inThread(p->rchild, pre);
    45. if (p->lchild == NULL) {//如果左子树为空,建立前驱线索
    46. p->lchild = pre;
    47. p->ltag = 1;
    48. }
    49. if (pre != NULL && pre->rchild == NULL) {//建立前驱结点的后继线索
    50. pre->rchild = p;
    51. pre->rtag = 1;
    52. }
    53. pre = p;
    54. }
    55. }