线索二叉树
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
typedef struct ThreadNode{
int data;
struct ThreadNode *lchild,*rchild;
int ltag,rtag;
}ThreadNode,*ThreadTree;
void Create(ThreadTree &T)
{
int d;
printf("输入结点(按0为空结点):");
scanf("%d",&d);
if(d!=0)
{
T = (ThreadTree)malloc(sizeof(ThreadNode));
T->data=d;
T->ltag=0;
T->rtag=0;
Create(T->lchild);
Create(T->rchild);
}
else{
T=NULL;
return;
}
}
ThreadNode *pre;
void visit(ThreadNode *q){
if(q->lchild==NULL){
q->lchild=pre;
q->ltag=1;
}
if(pre!=NULL&&q->rchild==NULL){
pre->rchild=q;
pre->rtag=1;
}
pre=q;
}
void InThread(ThreadTree &T){
if(T!=NULL){
InThread(T->lchild);
visit(T);
InThread(T->rchild);
}
}
void CreateInTread(ThreadTree &T){
pre=NULL;
if(T!=NULL){
InThread(T);
if(pre->rchild==NULL){
pre->rtag=1;
}
}
}
ThreadNode *FirstNode(ThreadNode *P){
while(P->ltag=0) P=P->lchild;
return P;
}
ThreadNode *NextNode(ThreadNode *P){
if(P->rtag==0) return FirstNode(P->rchild);
else return P->rchild;
}
void InOrder(ThreadNode *T){
for(ThreadNode *P=FirstNode(T);P!=NULL;P=NextNode(P))
printf("%d",P->data);
}
int main(){
ThreadTree T;
Create(T);
CreateInTread(T);
InOrder(T);
return 0;
}