#include <stdio.h>#include<stdlib.h>typedefCharElemtype;//Cue store flag bit//Link (0): pointer to left and right child//Thread (1): Indicates a clue to the predecessor's successortypedefenum{Link, Thread} pointertag;typedefstructbithrnode{Chardata; structBithrnode *lchild,*Rchild; Pointertag Ltag; Pointertag Rtag;} Bithrnode,*Bithrtree;//defines a global variable that represents a node that has just passedBithrtree pre;//Create a binary tree and contract the user to enter the data in the same way as the pre-order traversalvoidCreatebithrtree (Bithrtree *T) {CharC; scanf ("%c",&c); if(' '==c) { *t =NULL; }Else { *t = (Bithrnode *)malloc(sizeof(Bithrnode)); (*t)->data =C; (*t)->ltag =Link; (*t)->rtag =Link; Createbithrtree (& (*t)lchild); Createbithrtree (& (*t)rchild); }}//sequential traversal of a threadvoidinthreading (Bithrtree T) {if(t) {inthreading (t->lchild);//recursive left child clue if( ! T->lchild) {T->ltag =Thread; T->lchild =Pre; } if(!pre->rchild) {Pre->rtag =Thread; Pre->rchild =T; } Pre=T; Inthreading (T->rchild);//recursive right child lead }}//initialize a head pointervoidInorderthreading (Bithrtree *p, Bithrtree T) { *p = (Bithrnode *)malloc(sizeof(Bithrnode)); (*P)->ltag =Link; (*P)->rtag =Link; (*P)->rchild = *p; if(!T) {(*P)->lchild = *p; }Else { (*P)->lchild =T; Pre= *p; Inthreading (T); Pre->rchild = *p; Pre->rtag =Thread; (*P)->rchild =Pre; }}voidVisit (Charc) {printf ("%c", c);}//Middle sequence Traversal binary tree, iterativevoidInordertraverse (Bithrtree T) {Bithrtree p; P= t->Lchild; while(p!=T) { while(P->ltag = =Link) {P= p->Lchild; } visit (P-data); while(P->rtag = = Thread && P->rchild! =T) {P= p->Rchild; Visit (P-data); } P= p->Rchild; }}intMain () {Bithrtree p,t=NULL; Createbithrtree (&T); Inorderthreading (&P, T); printf ("The results of the middle sequence traversal of the binary tree are:"); Inordertraverse (P); printf ("\ n"); return 0;}
Run
The construction and traversal of the clue two fork tree------The data structure and algorithm of the small turtle