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

    const int MAXN = 1000010;
    //二叉树的节点的结构体的表示形式
    struct Node
    {
        char data;
        struct Node *leftchild,*rightchild;
    }BTree;
    //栈的结构体的表示形式
    struct sstack
    {
        BTree *map_stack[MAXN];
        int top;
    }Stack;
    //队列的结构体的表示形式
    struct qqueue
    {
        BTree *map_queue[MAXN];
        int head;
        int tail;
    }Queue;
    //创建二叉树,利用递归的方法
    BTree *BuildTree()
    {
        char char_str;
        printf(" %c",&char_str);
        if(char_str == '#')//"#"表示的是该树的该孩子是空的
        {
            return null;
        }
        else
        {
            BTree * Bree = (BTree*)malloc(sizeof(BTree));
            if(Bree == null)
            {
                return null;
            }
            Bree.data = char_str;
            Bree -> leftchild = BuildTree();
            Bree -> rightchild = BuildTree();
            return Bree;
        }
    }
    //前序遍历,递归方法实现
    void Preorder(BTree *BT)
    {
        if(BT == null)
        {
            return ;
        }
        printf("%c ",BT -> data);
        Preorder(BT -> leftchild);
        Preorder(BT -> rightchild);
    }
    //中序遍历,递归方法实现
    void Inorder(BTree *BT)
    {
        if(BT == null)
        {
            return ;
        }
        Inorder(BT -> leftchild);
        printf("%c ",BT -> data);
        Inorder(BT -> rightchild);
    }
    //后序遍历,递归方法实现
    void Postorder(BTree *BT)
    {
        if(BT == null)
        {
            return ;
        }
        Postorder(BT -> leftchild);
        Postorder(Bt -> rightchild);
        printf("%c ",BT -> data);
    }

  • 相关阅读:
    uboot配置和编译过程详解
    gcc 与 g++的区别
    ARM交叉编译器GNUEABI、NONE-EABI、ARM-EABI、GNUEABIHF等的区别
    SPI UART区别是什么
    C#获取时间戳的封装方法函数+使用获取当前时间时间戳
    C#中Timer定时器的使用示例
    Linux查看文件夹大小
    Python对象的创建和赋值
    使用mutt自动发送邮件
    pyTorch安装
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3370254.html
Copyright © 2011-2022 走看看