• 二叉树的建立与遍历


    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    #include<malloc.h>
    
    typedef struct Node
    {
        int data;
        struct Node *LChild;
        struct Node *RChild; 
    } BitNode,*BitTree;
    
    
    //前序建立二叉树,遇到-1停止
    BitTree PreCreate(BitTree T)
    {
        int n;
        //printf("前序建立二叉树:请输入:
    ");
        scanf("%d",&n);
        if(n==-1)T=NULL;
        else{
            T=(BitTree)malloc(sizeof(BitNode));
            T->data=n;
            T->LChild=PreCreate(T->LChild);
            T->RChild=PreCreate(T->RChild);
        }
        return T;
    } 
    //中序建立二叉树:不可行 
    BitTree MidCreate(BitTree T)
    {
        int n;
        //printf("中序建立二叉树:请输入:
    ");
        scanf("%d",&n);
        if(n==-1)T=NULL;
        else{
            T=(BitTree)malloc(sizeof(BitNode));
            T->LChild=MidCreate(T->LChild);
            T->data=n;
            T->RChild=MidCreate(T->RChild);
        }
        return T;
    } 
    //后序建立二叉树:不唯一  
    BitTree LastCreate(BitTree T)
    {
        int n;
        //printf("后序建立二叉树:请输入:
    ");
        scanf("%d",&n);
        if(n==-1)T=NULL;
        else{
            T=(BitTree)malloc(sizeof(BitNode));
            T->LChild=LastCreate(T->LChild);
            T->RChild=LastCreate(T->RChild);
            T->data=n;
        }
        return T;
    } 
    
    //先序遍历
    void PreRead(BitTree T)
    {
        if(T)
        {
            printf("%d",T->data);
            PreRead(T->LChild);
            PreRead(T->RChild);
        }
    } 
    
    
    //中序遍历
    void MidRead(BitTree T)
    {
        if(T)
        {
            MidRead(T->LChild);
            printf("%d",T->data);
            MidRead(T->RChild);
        }
    } 
    
    
    //后序遍历 
    void LastRead(BitTree T)
    {
        if(T)
        {
            LastRead(T->LChild);
            LastRead(T->RChild);
            printf("%d",T->data);
        }
    } 
    
    
    int main()
    {
        printf("前序建立二叉树:请输入:
    ");
        BitNode *T=PreCreate(T);
        printf("前序遍历:");
        PreRead(T);
        printf("
    ");
        printf("中序遍历:");
        MidRead(T);
        printf("
    ");
        printf("后序遍历:");
        LastRead(T);
        printf("
    ");
        /*
        printf("中序建立二叉树:请输入:
    ");
        BitNode *T1=MidCreate(T1);
        printf("前序遍历:");
        PreRead(T1);
        printf("
    ");
        printf("中序遍历:");
        MidRead(T1);
        printf("
    ");
        printf("后序遍历:");
        LastRead(T1);
        printf("
    ");
        
        printf("后序建立二叉树:请输入:
    ");
        BitNode *T2=LastCreate(T2);
        printf("前序遍历:");
        PreRead(T2);
        printf("
    ");
        printf("中序遍历:");
        MidRead(T2);
        printf("
    ");
        printf("后序遍历:");
        LastRead(T2);
        printf("
    ");
        */
        return 0;
    }
    View Code
  • 相关阅读:
    问题:关于抛出例外的一个问题。
    向北京球迷致敬!!!
    [下载]高质量C++C编程指南
    WinCE.NET中播放声音
    WINCE.NET中程序只运行一次
    解决vs2003开发PDA(wince.net4.2)调试与部署问题
    WinCE.NET中设置系统日期时间
    网页上发送mail(PHP)
    点阵字库预览工具 V1.0.0
    WINCE.NET4.2下如何获取程序当前运行目录
  • 原文地址:https://www.cnblogs.com/a842297171/p/4774892.html
Copyright © 2020-2023  润新知