#include <iostream>
using namespace std;
#define Status int
#define OK 1
#define ERROR 0
#define Elemtype char
typedef enum{
Link,Thread
}PointerTag;
typedef struct BiThrNode{
Elemtype data;
struct BiThrNode *lchild, *rchild;
struct BiThrNode *parent;
PointerTag LTag , RTag;
}BiThrNode, *BiThrTree;
BiThrTree pre;
Status visit(Elemtype e){
cout << e << endl;
return OK;
}
char Vexch[26]={'A','B','D','H','$','$','I','$','$','E','J','$','$','$','C','F','$','$','G','$','$'};
int i=0;
Status CreatBiThrTree(BiThrTree &T, BiThrTree &p)
{
if(Vexch[i++]=='$') T=NULL;
else {
T= new BiThrNode;
if(!T) return 0;
T->data=Vexch[i-1];
T->parent = p;
T->LTag=Link;
CreatBiThrTree(T->lchild, T);
T->RTag=Link;
CreatBiThrTree(T->rchild, T);
}
return 1;
}
void PostThreading(BiThrTree p) {
if(p) {
PostThreading(p->lchild);
PostThreading(p->rchild);
if(!p->lchild) {
p->LTag = Thread;
p->lchild = pre;
}
if(pre && !pre->rchild) {
pre->RTag = Thread;
pre->rchild = p ;
}
pre = p;
}
}
Status PostOrderTraverse_Thr(BiThrTree T) {
BiThrTree p;
p = T;
pre=NULL;
while(p != NULL){
while(p->LTag == Link)
p = p->lchild;
while(p->RTag == Thread) {
visit(p->data);
pre = p;
p = p->rchild ;
}
if(p == T){
visit(p->data);
break;
}
while(p && p->rchild == pre) {
visit(p->data);
pre = p;
p = p->parent;
}
if(p && p->RTag == Link)
p = p->rchild;
}
return OK;
}
int main() {
BiThrTree PostT;
pre = NULL;
CreatBiThrTree(PostT,pre);
PostThreading(PostT);
PostOrderTraverse_Thr(PostT);
printf("\n");
return 0;
}
10-18
84
04-10
4222