#include<stdio.h>
#include<stdlib.h>
#define M 1000
struct ThrTreeNode;
typedef struct ThrTreeNode *PThrTreeNode;
struct ThrTreeNode{
int info;
PThrTreeNode llink,rlink;
int ltag,rtag;
};
typedef struct ThrTreeNode *ThrTree;
typedef ThrTree *PThrTree;
struct SeqStack{
int MAXNUM;
int t;
ThrTree *s;
};
typedef struct SeqStack *PSeqStack;
PSeqStack createEmptyStack(int m){
PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if(pastack!=NULL){
pastack->s = (ThrTree*)malloc(sizeof(struct ThrTreeNode) * m);
if(pastack->s){
pastack->MAXNUM = m;
pastack->t = -1;
return pastack;
}
else free(pastack);
}
printf("out of space!\n");
return NULL;
}
void push_seq(PSeqStack pastack, ThrTree x)
{
if(pastack->t >= pastack->MAXNUM -1)
printf("Overflow!\n");
else
{
pastack->t = pastack->t+1;
// printf("t de zhi %d\n",pastack->t);
pastack->s[pastack->t] = x;
// printf("%d\n",x->info);
}
}
void pop_seq(PSeqStack pastack)
{
if(pastack->t == -1)
printf("Underflow!\n");
else
pastack->t = pastack->t-1;
}
ThrTree top_seq(PSeqStack pastack)
{
if(pastack->t == -1)
printf("It is empty!\n");
else
return (pastack->s[pastack->t]);
}
int search(PThrTree ptree, int key, PThrTreeNode *position) {
PThrTreeNode p,q;
//printf("%d",key);
p = *ptree;
// printf("%d",p->info);
q = p;
// printf("%d",key);
while(p!=NULL) {
q = p;
if(p->info == key) { *position = p; return 1;}
else if(p->info>key) p = p->llink;
else p = p->rlink;
}
*position = q; return 0;
}
int insert(PThrTree ptree,int key) {
PThrTreeNode p, position;
if(search(ptree,key,&position) == 1)
return 1;
p = (PThrTreeNode)malloc(sizeof(struct ThrTreeNode));
if(p == NULL) {
printf("Error!\n");
return 0;
}
p->info = key;
p->llink = p->rlink = NULL;
p->rtag = p->ltag = 0;
if(position == NULL) *ptree = p;
else if(key < position->info) position->llink = p;
else position->rlink = p;
return 1;
}
int createTree(PThrTree ptree,int *a,int num)
{
int i,b;
*ptree = NULL;
// printf("%d",a[5]);
for(i=0;i<num;i++){
if(!insert(ptree,a[i]))
return 0;
}
// printf("successed");
return 1;
}
int isEmptyStack_seq(PSeqStack pastack){
return (pastack->t==-1);
}
void thread(ThrTree t, int num)
{
PSeqStack st = createEmptyStack(num);
ThrTree p, pr;
if(t == NULL)
return;
p = t;
pr = NULL;
do{
while(p!=NULL){
push_seq(st,p);
p = p->llink;
}
// printf("1\n");
p = top_seq(st);
// printf("\nwww%dwww\n",p->info);
pop_seq(st);
if(pr!=NULL){
if(pr->rlink == NULL){
pr->rlink = p;pr->rtag = 1;
}
if(p->llink == NULL){
p->llink = pr;p->ltag=1;
// printf("%d\n",pr->ltag);
}
}
pr=p;p=p->rlink;
// printf("%d\n",p==NULL);
// printf("%d\n",isEmptyStack_seq(st));
}while(!isEmptyStack_seq(st)||p!=NULL);
}
void nInorder(ThrTree t){
ThrTree p = t;
int i;
if(t==NULL) return ;
while(p->llink!=NULL && p->ltag==0)
p=p->llink;
while(p!=NULL){
printf("%d",p->info);
if(p->rlink!=NULL && p->rtag == 0){
p = p->rlink;
while(p->llink!=NULL && p->ltag==0) p =p->llink;
}
else p = p->rlink;
}
}
//void visit(ThrTree p){
// printf("%d",p->info);
//}
int main(){
int num,m,i=0,c;
int a[1000];
PThrTree ptree;
ptree = (PThrTreeNode*)malloc(sizeof(struct ThrTreeNode));
// printf("%p\n",ptree);
printf("请输入个数\n");
scanf("%d", &num);
printf("请依次输入节点:\n");
for(i=0;i<num;i++)
scanf("%d",&a[i]);
c = createTree(ptree, &a[0], num);
thread(*ptree,num);
// printf("%d\n",(*ptree)->llink->ltag);
nInorder(*ptree);
//printf("%d",c);
// printf("%p\n",ptree);
// printf("1");
// printf("%d",a[1]);
}
// createTree(T,)
// T = (ThrTree)malloc(sizeof(ThrTree));
// printf("请输入根节点:\n");
// scanf("%d",&gen);
// T->info = gen;
// createEmptyThrTree(T);
// while(T!=NULL){
// printf("%d\n", T->info);
// T=T->llink;
// thread(T);
// nInorder(T);
// printf("%d",num);
// stack = createEmptyStack(num);
// T = createEmptyThrTree(num);