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

    不说废话了,直接上代码

    #include<iostream>
    #define MAXSIZE 100

    using namespace std;

    struct node
    {
        struct node *lchlid;
        struct node *rchlid;
        char date;
    };

    void BulidTree(struct node *&T)
    {
        char ch;
        cin>>ch;
        if(ch=='#')
            T=NULL;
        else
        {
            T = new struct node;//申请新的空间
            T->date=ch;
            BulidTree(T->lchlid);
            BulidTree(T->rchlid);
        }
    }

    void Preorder(struct node *&T)//先序遍历:访问根节点,先序遍历左子树,先序遍历右子树
    {
        if(T)
        {
            cout<<T->date<<" ";
            Preorder(T->lchlid);
            Preorder(T->rchlid);
        }
    }

    void Midorder(struct node *&T)//中序遍历:中序遍历左子树,访问根节点,中序遍历右子树
    {
        if(T)
        {
            Midorder(T->lchlid);
            cout<<T->date<<" ";
            Midorder(T->rchlid);
        }

    }

    void Postorder(struct node *&T)//后续遍历:后续遍历左子树,后续遍历右子树,访问根节点
    {
        if(T)
        {
            Midorder(T->lchlid);
            Midorder(T->rchlid);
            cout<<T->date<<" ";
        }
    }

    int main()
    {
        struct node *T;
        BulidTree(T);
        cout<<"Get Tree"<<endl;

        cout<<"Preorder"<<endl;
        Preorder(T);cout<<endl;

        cout<<"Midorder"<<endl;
        Midorder(T);cout<<endl;

        cout<<"Postorder"<<endl;
        Postorder(T);cout<<endl;

        return 0;
    }

  • 相关阅读:
    爬虫requests模块 1
    刷题记录:ctf473831530_2018_web_virink_web
    刷题记录:[RCTF 2019]Nextphp
    CTF XSS
    PHP 7 源码学习 序&第一、二章
    刷题记录:[安洵杯 2019]iamthinking
    刷题记录:[GWCTF 2019]枯燥的抽奖
    preg_match绕过总结
    刷题记录:[SWPU2019]easy_python
    刷题记录:[GWCTF 2019]我有一个数据库
  • 原文地址:https://www.cnblogs.com/alan-W/p/6091023.html
Copyright © 2011-2022 走看看