#include <stdio.h>
#include <stdlib.h>
typedef struct binary_tree
{
char data;
struct binary_tree * lc, * rc;
int ltag, rtag;
}bit;
bit * pre;
void creat(bit * & );
void headnode(bit * & , bit * );
void threading(bit * );
void inorder(bit * );
int main(void)
{
bit * root, * head;
creat(root);
headnode(head, root);
inorder(head);
return 0;
}
void inorder(bit * node)
{
bit * p;
p = node->lc;
while (p != node)
{
while (!p->ltag)
p = p->lc;
printf("%c ", p->data);
while (p->rtag && p->rc != node)
{
p = p->rc;
printf("%c ", p->data);
}
p = p->rc;
}
}
void threading(bit * node)
{
if (node)
{
threading(node->lc);
if (!node->lc)
{
node->ltag = 1;
node->lc = pre;
}
else
node->ltag = 0;
if (!pre->rc)
{
pre->rtag = 1;
pre->rc = node;
}
else
pre->rtag = 0;
pre = node;
threading(node->rc);
}
}
void headnode(bit * &head, bit * root)
{
head = (bit * )malloc(sizeof(bit));
head->ltag = 0;
head->rtag = 1;
head->rc = head;
if (root == NULL)
head->lc = head;
else
{
head->lc = root;
pre = head;
threading(root);
pre->rc = head;
pre->rtag = 1;
head->rc = pre;
}
}
void creat(bit * &node)
{
char ch;
scanf("%c", &ch);
if (ch == '#')
node = NULL;
else
{
node = (bit * )malloc(sizeof(bit));
node->data = ch;
creat(node->lc);
creat(node->rc);
}
}