diff --git a/_04.树/03线索二叉树_ThreadBinaryTree - 副本.c b/_04.树/03线索二叉树_ThreadBinaryTree - 副本.c new file mode 100644 index 0000000..a17ac2f --- /dev/null +++ b/_04.树/03线索二叉树_ThreadBinaryTree - 副本.c @@ -0,0 +1,139 @@ +#include "string.h" +#include "stdio.h" +#include "stdlib.h" +#include "io.h" +#include "math.h" +#include "time.h" + +#define OK 1 +#define ERROR 0 +#define TRUE 1 +#define FALSE 0 + +#define MAXSIZE 100 /* 洢ռʼ */ + +typedef int Status; /* StatusǺ,ֵǺ״̬,OK */ +typedef char TElemType; +typedef enum {Link,Thread} PointerTag; /* Link==0ʾָҺָ, */ + /* Thread==1ʾָǰ̵ */ +typedef struct BiThrNode /* 洢ṹ */ +{ + TElemType data; /* */ + struct BiThrNode *lchild, *rchild; /* Һָ */ + PointerTag LTag; + PointerTag RTag; /* ұ־ */ +} BiThrNode, *BiThrTree; + +TElemType Nil='#'; /* ַԿոΪ */ + +Status visit(TElemType e) +{ + printf("%c ",e); + return OK; +} + +/* ǰнֵ,T */ +/* 0()/ո(ַ)ʾս */ +Status CreateBiThrTree(BiThrTree *T) +{ + TElemType h; + scanf("%c",&h); + + if(h==Nil) + *T=NULL; + else + { + *T=(BiThrTree)malloc(sizeof(BiThrNode)); + if(!*T) + exit(OVERFLOW); + (*T)->data=h; /* ɸ(ǰ) */ + CreateBiThrTree(&(*T)->lchild); /* ݹ鹹 */ + if((*T)->lchild) /* */ + (*T)->LTag=Link; + CreateBiThrTree(&(*T)->rchild); /* ݹ鹹 */ + if((*T)->rchild) /* Һ */ + (*T)->RTag=Link; + } + return OK; +} + +BiThrTree pre; /* ȫֱ,ʼָոշʹĽ */ +/* */ +void InThreading(BiThrTree p) +{ + if(p) + { + InThreading(p->lchild); /* ݹ */ + if(!p->lchild) /* û */ + { + p->LTag=Thread; /* ǰ */ + p->lchild=pre; /* ָָǰ */ + } + if(!pre->rchild) /* ǰûҺ */ + { + pre->RTag=Thread; /* */ + pre->rchild=p; /* ǰҺָָ(ǰp) */ + } + pre=p; /* preָpǰ */ + InThreading(p->rchild); /* ݹ */ + } +} + +/* T,,Thrtָͷ */ +Status InOrderThreading(BiThrTree *Thrt,BiThrTree T) +{ + *Thrt=(BiThrTree)malloc(sizeof(BiThrNode)); + if(!*Thrt) + exit(OVERFLOW); + (*Thrt)->LTag=Link; /* ͷ */ + (*Thrt)->RTag=Thread; + (*Thrt)->rchild=(*Thrt); /* ָָ */ + if(!T) /* ,ָָ */ + (*Thrt)->lchild=*Thrt; + else + { + (*Thrt)->lchild=T; + pre=(*Thrt); + InThreading(T); /* */ + pre->rchild=*Thrt; + pre->RTag=Thread; /* һ */ + (*Thrt)->rchild=pre; + } + return OK; +} + +/* T(ͷ)ķǵݹ㷨 */ +Status InOrderTraverse_Thr(BiThrTree T) +{ + BiThrTree p; + p=T->lchild; /* pָ */ + while(p!=T) + { /* ʱ,p==T */ + while(p->LTag==Link) + p=p->lchild; + if(!visit(p->data)) /* ΪյĽ */ + return ERROR; + while(p->RTag==Thread&&p->rchild!=T) + { + p=p->rchild; + visit(p->data); /* ʺ̽ */ + } + p=p->rchild; + } + return OK; +} + +int main() +{ + BiThrTree H,T; + printf("밴ǰ(:'ABDH##I##EJ###CF##G##')\n"); + CreateBiThrTree(&T); /* ǰ */ + InOrderThreading(&H,T); /* , */ + printf("():\n"); + InOrderTraverse_Thr(H); /* () */ + printf("\n"); + + return 0; +} + + diff --git a/_04.树/_c.线索二叉树.c b/_04.树/_c.线索二叉树.c index cb4e4f4..69adf8c 100644 --- a/_04.树/_c.线索二叉树.c +++ b/_04.树/_c.线索二叉树.c @@ -2,7 +2,7 @@ * @Author: Xu Bai * @Date: 2019-07-23 22:43:14 * @LastEditors: Xu Bai - * @LastEditTime: 2019-07-26 22:43:54 + * @LastEditTime: 2019-07-27 23:26:33 */ #include "string.h" #include "stdio.h" @@ -128,4 +128,46 @@ Status InOrderThreading(BiThrTree *Thrt, BiThrTree T) (*Thrt)->rchild = pre; } return OK; +} + +/*ǵݹ */ +Status InOrderTraverse_Thr(BiThrTree T) +{ + BiThrTree p; + /*pָڵ */ + p = T->lchild; + while (p != T) + { + /* ʱp==T */ + while (p->LTag == Link) + { + p = p->lchild; + } + if (!visit(p->data)) + { + /*ΪյĽ */ + return ERROR; + } + while (p->RTag == Thread && p->rchid != T) + { + p = p->rchild; + visit(p->data); + } + p = p->rchild; + } + return OK; +} + +int main() +{ + + BiThrTree H, T; + printf("밴ǰ(:'ABDH##I##EJ###CF##G##')\n"); + CreateBiThrTree(&T); /* ǰ */ + InOrderThreading(&H, T); /* , */ + printf("():\n"); + InOrderTraverse_Thr(H); /* () */ + printf("\n"); + getchar(); + return OK; } \ No newline at end of file