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);
    }

  • 相关阅读:
    java Class的 getSuperclass与getGenericSuperclass区别
    再有人问你volatile是什么,把这篇文章也发给他
    深入理解乐观锁与悲观锁
    再有人问你Java内存模型是什么,就把这篇文章发给他
    再有人问你synchronized是什么,就把这篇文章发给他
    《成神之路-基础篇》JVM——Java内存模型(已完结)
    css
    css
    require.js
    css -border
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3370254.html
Copyright © 2011-2022 走看看