⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c.txt

📁 二叉树的建立和递归
💻 TXT
字号:
#include <stdio.h>
#include <stdlib.h>
struct node
{
    struct node  * lchild;
    char data;
    struct node * rchild;
};
typedef struct node * BTREE;
void CreatBT(BTREE T)
{
    char ch;
    gets(&ch);
    //scanf("%c%c",&ch,&m);        //读入一个字符
    if (ch=='#') T=NULL;
    else
    {
        T=(struct node *)malloc(sizeof(struct node)); //生成一个新结点
        T->data=ch;
        CreatBT(T->lchild);      //生成左子树
        CreatBT(T->rchild);      //生成右子树
    }
}
//先序遍历的递归
void Preorder(BTREE T)
{
    if (T!=NULL)
    {
        printf("%c ",T->data);      //访问结点
        Preorder(T->lchild);       //遍历左子树
        Preorder(T->rchild);       //遍历右子树
    }
}
//递归中根遍历
void inorder(BTREE T)
{
    if (T!=NULL)
    {
        inorder(T->lchild);
        printf ("%c",T->data);
        inorder(T->rchild);
    }
}
//递归后根遍历
void postorder(BTREE T)
{
    if (T!=NULL)
    {
        postorder(T->lchild);
        postorder(T->rchild);
        printf ("%c",T->data);
    }
}
int main()
{
    BTREE T=NULL;
    CreatBT(T);
    Preorder(T);
    printf ("\n");
    inorder(T);
    printf ("\n");
    postorder(T);
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -