二叉树一棵(先,中,后序遍历)

      好久没来这里了,上次写的东西都不晓得放了多久了,想必快要腐烂了,还是来添点其他的东西吧,想来想去还是搞点绿化好,种一棵叔,希望这棵小树能茁壮成长,将来为祖国抵御沙尘暴。

#include   "stdio.h"
#include   "conio.h"
#include   "string.h"
#define   Null   0
struct   btnode
{
    char   c;
    struct   btnode   *lchild,*rchild;
};
struct   btnode   *creatbt()                 /*用递归创建*/
{
    struct   btnode   *root;

    root=(struct   btnode*)malloc(sizeof(struct   btnode));
    scanf("%c",&root->c);
    if(root->c=='#')
    root=Null;
    else   {


                root->lchild=creatbt();
                root->rchild=creatbt();
              }
    return   root;
}
void   preorder(struct   btnode   *root)     /*先序遍历二叉树   */
{
    if(root!=Null)
    {
      printf("%c",root->c);
      preorder(root->lchild);
      preorder(root->rchild);
    }
}
void   inorder(struct   btnode   *root)       /*中序遍历二叉树*/
{
    if(root!=Null)
    {
      inorder(root->lchild);
      printf("%c",root->c);
      inorder(root->rchild);
    }
}
void   postorder(struct   btnode   *root)     /*后序遍历*/
{
    if(root!=Null)
    {
      postorder(root->lchild);
      postorder(root->rchild);
      printf("%c",root->c);
    }
}
int   main(void)
{
    struct   btnode   *Root;
    Root=creatbt();
    printf("/n");
    preorder(Root);
    printf("/n");
    inorder(Root);
    printf("/n");
    postorder(Root);
    printf("/n");
    getch();
    return   0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章