// First create a binary tree to generate clues for the tree and traverse the tree.
# Include <stdio. h>
# Include <malloc. h>
# Define elemtype char
Typedef Enum tag {link, thread };
Typedef struct threadtree
{
Elemtype data;
Struct threadtree * lchild, * rchild;
Tag ltag, rtag;
} Threadtree, * sthreadtree;
// Global variable
Sthreadtree pre;
// Create a binary tree, similar to sequential Traversal
Void creat (sthreadtree & T)
{
Char ch;
Scanf ("% C", & Ch );
If (CH = '#')
T = NULL;
Else
{
T = (sthreadtree) malloc (sizeof (threadtree ));
T-> DATA = CH;
T-> ltag = link; t-> rtag = link;
Creat (t-> lchild );
Creat (t-> rchild );
}
}
// Sequential Traversal
Void mid (sthreadtree T)
{
If (t)
{
Mid (t-> lchild );
Printf ("% C", T-> data );
Mid (t-> rchild );
}
}
// Clue-Based Binary Tree, similar to sequential Traversal
Void inthread (sthreadtree T)
{
If (t)
{
Inthread (t-> lchild );
If (! T-> lchild) {T-> ltag = thread; t-> lchild = pre ;}
If (! Pre-> rchild) {pre-> rtag = thread; pre-> rchild = T ;}
Pre = T;
Inthread (t-> rchild );
}
}
// Add a header Node
Void inorderthread (sthreadtree & head, sthreadtree T)
{
// Sthreadtree pre;
Head = (sthreadtree) malloc (sizeof (threadtree ));
Head-> ltag = link; head-> rtag = thread;
Head-> rchild = head;
If (! T) Head-> lchild = head;
Else
{
Head-> lchild = T; Pre = head;
Inthread (t );
Pre-> rchild = head; pre-> rtag = thread;
Head-> rchild = pre;
}
}
// Link the Binary Tree
Void inordertraverse (sthreadtree T)
{
Sthreadtree P;
P = T-> lchild;
While (P! = T)
{
While (p-> ltag = link) P = p-> lchild;
Printf ("% C", p-> data );
While (p-> rtag = thread & P-> rchild! = T)
{
P = p-> rchild;
Printf ("% C", p-> data );
}
P = p-> rchild;
}
}
Void main ()
{
Sthreadtree T = NULL;
Sthreadtree T = NULL; // head
Printf ("Enter the sequence of the tree to be created:/N ");
Creat (t); // mid (t); printf ("/N ");
Inorderthread (t, t );
Inordertraverse (t );
Printf ("/N ");
}
/*
Enter the sequence of the tree to be created:
Hda ## C # B ## GF # E ###
Adcbhfeg
Press any key to continue
*/