#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef char datatype;/* Definition DataType Type * *
typedef enum {Link,thread}pointertag;
typedef struct node{
DataType data;
struct node *lchild, *rchild;/* left and right child subtree *
Pointertag Ltag,rtag;
}bithrnode; /* Node Type * *
typedef bithrnode *BITHRTREE///two fork tree type * *
void Creatbintree (Bithrtree *t)
{/* Constructs two forks list, note: The input sequence is the first order sequence * *
Char ch;
if ((Ch=getchar ()) = = ")
*t=null;
else{/* Read non-space * *
*t= (Bithrnode *) malloc (sizeof (Bithrnode));/* Generate node * *
(*t)->data=ch; (*t)->ltag=link; (*t)->rtag=link;
Creatbintree (& (*t)->lchild); /* Constructed left subtree * *
Creatbintree (& (*t)->rchild); /* Construct right subtree * *
}
}
Bithrtree pre;/* global Variable * *
void Inthreading (Bithrtree p)
{
if (p)
{inthreading (P->lchild);//* Zoozi tree Cue * *
if (!p->lchild) {p->ltag=thread;p->lchild=pre;} * Precursor leads/*
if (!pre->rchild) {pre->rtag=thread;pre->rchild=p;} * Follow-up leads/*
pre=p;/* keep the pre pointing to p*/
Inthreading (p->rchild)/* Right subtree cue/
}
}
int inorderthreading (Bithrtree *thrt,bithrtree T)
/* In the sequence Shing fork tree T, and will be the sequence of clues, Thrt point to the head node * *
{if (!) ( *thrt= (bithrtree) malloc (sizeof (Bithrnode))) exit (0);
(*thrt)->ltag=link; (*thrt)->rtag=thread;/* Building head Node * *
(*thrt)->rchild=*thrt;/* right pointer anaphora/
if (! T) (*thrt)->lchild=*thrt;
Else
{(*THRT)->lchild=t;pre=*thrt;
Inthreading (T);//* Middle sequence traversal in order to lead
pre->rchild=*thrt;pre->rtag=thread;/* the last knot of the clue.
(*THRT)->rchild=pre;
}
return 1;
}
int print (Bithrtree e)
{printf ("%d%c%d\n", E->ltag,e->data,e->rtag); return 1;}
int Inordertraverse (Bithrtree t,int (* visit) (Bithrtree e))
/*t point to the head node, the left chain of the head node Lchild point to the root node, in the sequence of Shing fork Tree * *
{Bithrtree p;
P=t->lchild;/*p point to root node * *
while (p!=t)/* Empty tree or at the end of the p==t*/
{while (P->ltag==link) p=p->lchild;
if (!visit (p)) return 0;/* Print * *
while (p->rtag==thread&&p->rchild!=t)
{p=p->rchild;visit (P);} * * Access to subsequent nodes * *
p=p->rchild;
}
return 1;
}
void Main ()
{/* Test procedure * *
Bithrtree T,thrt;
Creatbintree (&t);
Inorderthreading (&thrt,t);
Inordertraverse (Thrt,print);
}
/* Can be entered "-+a *b-c d/e F" To test (note the space) * *