线索化二叉树
#include <iostream>
using namespace std;
typedef char ElemType;
#define END '#'
typedef enum{LINK = 0, THREAD = 1} Pointertag;
typedef struct BiThrNode
{
ElemType data;
BiThrTree *leftchild;
BiThrTree *rightchild;
BiThrTree *parent;
Pointer Ltag, Rtag;
}BiThrNode, *BinaryThreadTree;
BiThrNode *_Buynode()
{
BiThrNode *p = (BiThrNode *)malloc(sizeof(BiThrNode));
if ( p == NULL) exit(0);
return p;
}
BiThrNode *CreateTree(ElemType *&str)
{
BiThrNode *s = NULL;
while (*str != END)
{
s = _Buynode();
s->data = *str;
S->Ltag = s->Rtag = LINK;
s->leftchild = CreateTree(++str);
s->righrchild = CreateTree(++str);
}
return s;
}
void Make(BiThrTree *p, BiThrTree *&ptr)
{
if (p != NULL)
{
Make(p->leftchild, ptr);
if (p->leftchild == NULL)
{
p->leftchild = ptr;
p->Ltag = THREAD;
}
if(ptr != NULL && ptr->rightchild == NULL)
{
ptr->rightchild = p;
ptr->Rtag == THREAD;
}
ptr = p;
Make(p->righrchild, ptr);
}
}
void MakeThread(BiThrNode *p)
{
if (p = NULL) return;
BiThrNode *ptr = NULL;
Make(p, ptr);
ptr->rightchild = NULL;
ptr->Rtag = THREAD;
}
BiThrNode *First(BiThNode *ptr)
{
if (ptr == NULL) return NULL;
while(ptr != NULL && ptr->Ltag != THREAD)
{
ptr = ptr->leftchild;
}
return ptr;
}
BiThrNode *Next(BiThrNode *ptr)
{
if (ptr == NULL) return NULL;
if (ptr->Rtag == THREAD)
{
return ptr->rightchild;
}
else
{
First(p->rightchild);
}
}
void NiceInOrder(BiThrNode *ptr)
{
for (BiThrNode *p = Frist(ptr); p != NULL; p = Next(p))
{
cout << p->data << " ";
}
cout << endl;
}
BiThrNode *Last(BiThrNode *ptr)
{
if (ptr == NULL) return NULL;
while (ptr != NULL && ptr->Rtag != THREAD)
{
ptr = ptr->rightchild;
}
return ptr;
}
BiThrNode *Prev(BitThrNode *ptr)
{
if (ptr = NULL) return NULL;
if (ptr->Ltag == Thread)
{
return ptr->leftchild;
}
else
{
return Last(ptr->leftchild);
}
}
void ResNiceInOrder(BiThrNode *ptr)
{
for (BiThrNode *p = Last(ptr); p != NULL; p = Prev(p))
{
cout << p->data << " ";
}
cout << endl;
}