• 二叉树的建立与递归遍历C语言版


    </pre><pre name="code" class="cpp">#include <stdio.h>
    #include <malloc.h>
    
    
    typedef struct BTNode
    {
    char data;
    struct BTNode *lchild;
    struct BTNode *rchild;
    }BTNode;
    
    
    void createTree(BTNode* *T)
    {
    char ch = getchar();
    if(ch=='#') 
    {
    *T=NULL;
    return;
    }
    (*T) = (BTNode*)malloc(sizeof(BTNode));
    (*T)->data = ch;
    createTree(&(*T)->lchild);
    createTree(&(*T)->rchild);
    }
     
    void preOrder(BTNode *T)//先序遍历
    {
    if(T==NULL) return;
    printf("%c	", T->data);
    if(T->lchild!=NULL)
    preOrder(T->lchild);
    if(T->rchild != NULL)
    preOrder(T->rchild);
    }
    
    
    void inOrder(BTNode *T)//中序遍历
    {
    if(T==NULL) return;
    
    if(T->lchild!=NULL)
    inOrder(T->lchild);
    printf("%c	", T->data);
    if(T->rchild != NULL)
    inOrder(T->rchild);
    }
    
    
    void postOrder(BTNode *T)//后序遍历
    {
    if(T==NULL) return;
    
    if(T->lchild!=NULL)
    postOrder(T->lchild);
    if(T->rchild != NULL)
    postOrder(T->rchild);
    printf("%c	", T->data);
    }
    
    
    int main(int argc, char const *argv[])
    {
    BTNode *T;
    createTree(&T);
    puts("PreOrder visit:");
    preOrder(T);
    putchar('
    ');
    
    
    puts("inOrder visit:");
    inOrder(T);
    putchar('
    ');
    
    
    puts("PostOrder visit:");
    postOrder(T);
    putchar('
    ');
    return 0;
    }
    /*运行结果:
    
    
    ABD###C##
    PreOrder visit:
    A       B       D       C
    inOrder visit:
    D       B       A       C
    PostOrder visit:
    D       B       C       A
    请按任意键继续. . .*/
    


  • 相关阅读:
    intellij idea 注册码
    python 爬虫
    打油诗
    vux 新建移动app步骤
    ubuntu支持中文配置
    pandas DataFrame 交集并集补集
    API精准定位IP地址
    Python获取本地位置和天气
    nginx配置uwsgi
    django ORM model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct
  • 原文地址:https://www.cnblogs.com/yldf/p/6249885.html
Copyright © 2020-2023  润新知