zoukankan      html  css  js  c++  java
  • 二叉树的建立和遍历(c语言描述)

      二叉树是比较特殊的树,二叉树的存储方式有顺序存储和链式存储,我们基本上都是用的链式存储,

    1.声明结构体

    typedef char ElemType;
    typedef struct BiTNode
    {
        ElemType data; 
        struct BiTNode *lchild,*rchild; //左右孩子树
    }BiTNode,*BiTree;

    2.创建二叉树

    采用递归的方式:这里采用的是按照先序序列建立二叉树,

    void createBiTree(BiTree *T)
    {
        char s;
        scanf("%c",&s);
        
        if(s=='$')
        {
            *T=NULL;
        }
        else
        {
            *T=(BiTNode*)malloc(sizeof(BiTNode)); //生成结点
            if(!(*T))
            {
                printf("申请空间失败
    ");
            }
            (*T)->data=s;
            createBiTree(&(*T)->lchild);
            createBiTree(&(*T)->rchild);
        }
    }

    所有的代码如下:

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef char ElemType;
    typedef struct BiTNode
    {
        ElemType data; 
        struct BiTNode *lchild,*rchild; //左右孩子树
    }BiTNode,*BiTree;
    
    //二叉树的建立  按照先序遍历建立 
    void createBiTree(BiTree *T)
    {
        char s;
        scanf("%c",&s);
        
        if(s=='$')
        {
            *T=NULL;
        }
        else
        {
            *T=(BiTNode*)malloc(sizeof(BiTNode)); //生成结点
            if(!(*T))
            {
                printf("申请空间失败
    ");
            }
            (*T)->data=s;
            createBiTree(&(*T)->lchild);
            createBiTree(&(*T)->rchild);
        }
    }
    
    //二叉树先序遍历
    void preorderVisit(BiTree T) //这里不要使用BiTNode *T , 不要直接对树进行操作 
    {
        if(T)  //先要进行判断,只有结点不为空才会遍历 
        {
            printf("%c ",T->data);
            preorderVisit(T->lchild);
            preorderVisit(T->rchild);
        }
    }
    
    //二叉树中序遍历
    void middleVisit(BiTree T)   //这里不要使用BiTNode *T , 不要直接对树进行操作 
    {
        if(T)   //先要进行判断,只有结点不为空才会遍历 
        {
            middleVisit(T->lchild);
            printf("%c ",T->data);
            middleVisit(T->rchild);
        }
    
    }
    
    //二叉树后序遍历
    void postVisit(BiTree T)   //这里不要使用BiTNode *T , 不要直接对树进行操作 
    {
        if(T)   //先要进行判断,只有结点不为空才会遍历 
        {
            postVisit(T->lchild);
            postVisit(T->rchild);
            printf("%c ",T->data);
        }
    }
    
    int main()
    {
        printf("请输入二叉树,按照先序遍历,无字树用$号表示
    "); 
        
        BiTree T;
        createBiTree(&T);
        
        printf("先序遍历结果如下:
    ");
        preorderVisit(T);
        printf("
    ");
        
        printf("中序遍历结果如下:
    ");
        middleVisit(T);
        printf("
    ");
        
        printf("后序遍历结果如下:
    ");
        postVisit(T);
        printf("
    ");
    } 
     
  • 相关阅读:
    maven打包成jar文件与打包成tar.gz文件
    maven命令错误:-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME
    Nginx监听多个端口配置实例 Linux
    怎么修改redis-cli访问的地址
    eclipse的.properties文件中文显示问题
    Linux关闭防火墙命令red hat/CentOs7
    CentOS 7防火墙快速开放端口配置方法
    Window下Beego环境搭建和bee工具使用
    .netcore2.1 使用middleware对api请求头进行验证
    .netcore2.1 统一接口返回属性名称
  • 原文地址:https://www.cnblogs.com/qian-yi/p/12732172.html
Copyright © 2011-2022 走看看