#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char ElemType;
typedef enum{ LINK = 0, THREAD = 1 }PointTag;
typedef struct BiThrNode
{
BiThrNode *leftchild;
BiThrNode *rightchild;
ElemType data;
PointTag Rtag, Ltag;
};
BiThrNode * Buy_Node()
{
BiThrNode *s = (BiThrNode *)malloc(sizeof(BiThrNode));
if (NULL == s) exit(1);
memset(s, 0, sizeof(BiThrNode));
return s;
}
void Free_Node(BiThrNode *phead)
{
free(phead);
}
BiThrNode *Create_Tree1() //二叉树的创建
{
BiThrNode *s = NULL;
ElemType item;
scanf_s("%c", &item);
if (item != '#')
{
s = Buy_Node();
s->data = item;
s->leftchild = Create_Tree1();
s->rightchild = Create_Tree1();
}
}
BiThrNode *p = NULL;
void InOrder(BiThrNode *ptr) //给二叉树加上索引
{
if (ptr != NULL)
{
InOrder(ptr->leftchild);
if (NULL == ptr->leftchild) //没有左孩子
{
ptr->Ltag = THREAD; //左标志位为1
ptr->leftchild = p; //左孩子指向前驱
}
if (NULL!=p&&NULL == p->rightchild) //没有右孩子
{
p->Rtag = THREAD; //右标志位为1
p->rightchild = ptr; //右孩子指向后继
}
p = ptr;
InOrder(ptr->rightchild);
}
}
void InOrder_Thr(BiThrNode *ptr) //根据索引打印线索二叉树
{
BiThrNode *p = ptr->leftchild;
while (p)
{
while (p->Ltag == LINK)
{
p = p->leftchild;
}
printf("%c ", p->data);
while (p->Rtag == THREAD)
{
p = p->rightchild;
printf("%c ", p->data);
}
p = p->rightchild;
}
}
BiThrNode* xianmake(BiThrNode*root)//先序线索化二叉树
{
if (root)
{
if (!root->leftchild)
{
root->Ltag = THREAD;
root->leftchild = p;
}
if (p&& !p->rightchild)
{
p->Rtag = THREAD;
pre->rightchild = root;
}
p = root;
xianmake(root->leftchild);
xianmake(root->rightchild);
}
return NULL;
}
BiThrNode* houmake(BiThrNode*root)//后序线索化二叉树
{
if (root)
{
houmake(root->leftchild);
houmake(root->rightchild);
if (!root->leftchild)
{
root->Ltag = THREAD;
root->leftchild = p;
}
if (p && !p->rightchild)
{
p->Rtag = THREAD;
p->rightchild = root;
}
p = root;
}
return NULL;
}
BiThrNode *First(BiThrNode *p)
{
while (p != NULL&&p->Ltag == THREAD)
{
p = p->leftchild;
}
return p;
}
BiThrNode *Next(BiThrNode *p)
{
if (p == NULL)
return NULL;
if (p->Rtag == THREAD)
{
return p->rightchild;
}
}
void InOrder_Thr(BiThrNode *ptr) //打印线索二叉树
{
for (BiThrNode *p = First(ptr); p != NULL; p = Next(p))
{
printf("%c", p->data);
}
printf("\n");
}
BiThrNode *Last(BiThrNode *p) //逆序打印线索二叉树
{
while (p != NULL&&p->Rtag == LINK)
{
p = p->rightchild;
}
return p;
}
BiThrNode *Prev(BiThrNode *p)
{
if (p == NULL) return NULL;
if (p->Ltag == THREAD)
{
return p->leftchild;
}
else
{
p = p->leftchild;
return Last(p);
}
}
void ResThrInOrder(BiThrNode *ptr)
{
for (BiThrNode *p = Last(ptr); p != NULL; p = Prev(p))
{
printf("%c ", p->data);
}
printf("\n");
}
#include<stdlib.h>
#include<string.h>
typedef char ElemType;
typedef enum{ LINK = 0, THREAD = 1 }PointTag;
typedef struct BiThrNode
{
BiThrNode *leftchild;
BiThrNode *rightchild;
ElemType data;
PointTag Rtag, Ltag;
};
BiThrNode * Buy_Node()
{
BiThrNode *s = (BiThrNode *)malloc(sizeof(BiThrNode));
if (NULL == s) exit(1);
memset(s, 0, sizeof(BiThrNode));
return s;
}
void Free_Node(BiThrNode *phead)
{
free(phead);
}
BiThrNode *Create_Tree1() //二叉树的创建
{
BiThrNode *s = NULL;
ElemType item;
scanf_s("%c", &item);
if (item != '#')
{
s = Buy_Node();
s->data = item;
s->leftchild = Create_Tree1();
s->rightchild = Create_Tree1();
}
}
BiThrNode *p = NULL;
void InOrder(BiThrNode *ptr) //给二叉树加上索引
{
if (ptr != NULL)
{
InOrder(ptr->leftchild);
if (NULL == ptr->leftchild) //没有左孩子
{
ptr->Ltag = THREAD; //左标志位为1
ptr->leftchild = p; //左孩子指向前驱
}
if (NULL!=p&&NULL == p->rightchild) //没有右孩子
{
p->Rtag = THREAD; //右标志位为1
p->rightchild = ptr; //右孩子指向后继
}
p = ptr;
InOrder(ptr->rightchild);
}
}
void InOrder_Thr(BiThrNode *ptr) //根据索引打印线索二叉树
{
BiThrNode *p = ptr->leftchild;
while (p)
{
while (p->Ltag == LINK)
{
p = p->leftchild;
}
printf("%c ", p->data);
while (p->Rtag == THREAD)
{
p = p->rightchild;
printf("%c ", p->data);
}
p = p->rightchild;
}
}
BiThrNode* xianmake(BiThrNode*root)//先序线索化二叉树
{
if (root)
{
if (!root->leftchild)
{
root->Ltag = THREAD;
root->leftchild = p;
}
if (p&& !p->rightchild)
{
p->Rtag = THREAD;
pre->rightchild = root;
}
p = root;
xianmake(root->leftchild);
xianmake(root->rightchild);
}
return NULL;
}
BiThrNode* houmake(BiThrNode*root)//后序线索化二叉树
{
if (root)
{
houmake(root->leftchild);
houmake(root->rightchild);
if (!root->leftchild)
{
root->Ltag = THREAD;
root->leftchild = p;
}
if (p && !p->rightchild)
{
p->Rtag = THREAD;
p->rightchild = root;
}
p = root;
}
return NULL;
}
BiThrNode *First(BiThrNode *p)
{
while (p != NULL&&p->Ltag == THREAD)
{
p = p->leftchild;
}
return p;
}
BiThrNode *Next(BiThrNode *p)
{
if (p == NULL)
return NULL;
if (p->Rtag == THREAD)
{
return p->rightchild;
}
}
void InOrder_Thr(BiThrNode *ptr) //打印线索二叉树
{
for (BiThrNode *p = First(ptr); p != NULL; p = Next(p))
{
printf("%c", p->data);
}
printf("\n");
}
BiThrNode *Last(BiThrNode *p) //逆序打印线索二叉树
{
while (p != NULL&&p->Rtag == LINK)
{
p = p->rightchild;
}
return p;
}
BiThrNode *Prev(BiThrNode *p)
{
if (p == NULL) return NULL;
if (p->Ltag == THREAD)
{
return p->leftchild;
}
else
{
p = p->leftchild;
return Last(p);
}
}
void ResThrInOrder(BiThrNode *ptr)
{
for (BiThrNode *p = Last(ptr); p != NULL; p = Prev(p))
{
printf("%c ", p->data);
}
printf("\n");
}