二叉樹一棵(先,中,後序遍歷)

      好久沒來這裏了,上次寫的東西都不曉得放了多久了,想必快要腐爛了,還是來添點其他的東西吧,想來想去還是搞點綠化好,種一棵叔,希望這棵小樹能茁壯成長,將來爲祖國抵禦沙塵暴。

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

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