二叉树的创建与递归遍历(二)

2013-11-20 14:23:48 · 作者: · 浏览: 200

 

  //二叉树的创建,第二种方法   {

  BiTree T;

  char c;

  printf("请输入数据:");

  c = getchar();

  flushall();

  if(c != '$')

  {

  T = (BiNode *)malloc(sizeof(BiNode));

  T->data = c;

  T->lch = Creat();

  T->rch = Creat();

  }

  else

  T = NULL;

  return T;

  }

  void Preorder(BiTree T)

  //先序遍历

  {

  if(T)

  {

  printf("%c",T->data);

  Preorder(T->lch);

  Preorder(T->rch);

  }   }

  void Inorder(BiTree T)

  //中序遍历

  {

  if(T)

  {

  Inorder(T->lch);

  printf("%c",T->data);

  Inorder(T->rch);

  }   }

  void Postorder(BiTree T)

  //后续遍历

  {

  if(T)

  {

  Postorder(T->lch);

  Postorder(T->rch);

  printf("%c",T->data);

  }

  }