注册 登录
考研论坛 返回首页

张华彬的个人空间 http://home.kaoyan.com/?4217302 [收藏] [复制] [分享] [RSS]

日志

二叉树三种遍历方法~~~多多指教哈

已有 718 次阅读2010-7-28 21:11 |

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
 int data;
 struct node *lchild, *rchild;
}bitree;
bitree *Q[100];
bitree *creat_tree()
{
 int front=1, rear=0;
 char ch;
 bitree *root, *s;
 printf("creat bitree!\n");
 printf("for ex");
 while((ch=getchar())!=10)
 {
  s=NULL;
  if(ch!='@')
  {
   s=(bitree *)malloc(sizeof(bitree));
   s->data=ch;
   s->lchild=NULL;
   s->rchild=NULL;
  }
  rear++;
  Q[rear]=s;
  if(rear==1)
   root=s;
  else
  {
   if( s && Q[front])
    if(rear%2==0)
     Q[front]->lchild=s;
    else
     Q[front]->rchild=s;
   if(rear%2==1)
   {
    front++;
   }
  } 
 }
 return root;
}
void PerOrder(bitree *T)
{
 if(T)
 {
  printf(" %c\t", T->data);
  PerOrder(T->lchild);
  PerOrder(T->rchild);
 }
 
}
void InOrder(bitree *T)
{
 if(T)
 {
  
  InOrder(T->lchild);
  printf(" %c\t", T->data);
  InOrder(T->rchild);
 }
}
void LastOrder(bitree *T)
{
 if(T)
 {
  
  LastOrder(T->lchild);
  LastOrder(T->rchild);
  printf(" %c\t", T->data);
 }
 
}
void main()
{
 char in;
 bitree *t;
 t=creat_tree();
 while(true)
 {
  printf("1 前续排列 2 中序排列 3 后序排列 4 退出\n");
  in=getchar();
  getchar();
  if(in=='1')
  {
   PerOrder(t);
   printf("\n");
  }
  else if(in=='2')
  {
   InOrder(t);
   printf("\n");
  }
  else if(in=='3')
  {
   LastOrder(t);
   printf("\n");
  }
  else if(in=='4')
  {
   exit(0);
  }
  else
   printf("error!!!\n");
 }
}

路过

雷人

握手

鲜花

鸡蛋
收藏 分享邀请 分享到人人 举报

评论 (0 个评论)

关于我们|商务合作|小黑屋|手机版|联系我们|服务条款|隐私保护|帮学堂| 网站地图|院校地图|漏洞提交|考研帮

GMT+8, 2025-6-16 14:02 , Processed in 0.073446 second(s), Total 10, Slave 10(Usage:2.75M, Links:[2]1,1_1) queries , Redis On.

Powered by Discuz!

© 2001-2017 考研 Inc.

返回顶部
× 关闭