From 2eea32a5e1cb03cbf30fe56c1d2fddb48c061956 Mon Sep 17 00:00:00 2001 From: Xu Bai <1373953675@qq.com> Date: Sat, 20 Jul 2019 23:16:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=B5=E8=84=91=E5=9D=8F=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _04.树/03线索二叉树_ThreadBinaryTree - 副本.c | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 _04.树/03线索二叉树_ThreadBinaryTree - 副本.c 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; +} + +