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

  • 相关阅读:
    UIWebView 视频播放获取开始播放和结束播放通知
    显示图像数据的高级接口 UIImage
    如何跳到系统设置里的WiFi界面
    Objective-C 去掉NSString 前后中空格
    iOS 属性修饰符的区别
    iOS 线程锁同步机制
    XCode 6 以后使用编程处理一些图片效果
    iOS 精益编程
    iOS7以后UITextView 技巧
    2016年12月英语六级阅读真题及答案 第3套
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3370254.html
Copyright © 2011-2022 走看看