C语言实现二叉排序树线索化再中序遍历

#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);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值