zoukankan      html  css  js  c++  java
  • 二叉树的建立与遍历

    #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
  • 相关阅读:
    Linux 6.2 配置NTP服务
    配置SSH三大框架报错
    Linux 6.2 源码安装和配置Puppet
    PGA 内存管理
    调整linux系统时间和时区与Internet时间同步
    Java 文件分块上传客户端和服务器端源代码
    Java 文件分块上传服务器端源代码
    Java 服务器端支持断点续传的源代码【支持快车、迅雷】(仅支持 HTTP 协议)
    bbed使用
    OracleDatabase 11g安装bbed
  • 原文地址:https://www.cnblogs.com/a842297171/p/4774892.html
Copyright © 2011-2022 走看看