• 二叉树的建立及递归遍历


    huangjing

    二叉树的的建立方式为前序  二叉树有三种遍历  前序遍历(NLR)  中序遍历(LNR)  兴许遍历(LRN)

    非递归的算法明天补上

    代码为:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<map>
    #include<vector>
    #include<cmath>
    #include<string>
    #include<queue>
    #define eps 1e-9
    #define ll long long
    #define INF 0x3f3f3f3f
    using namespace std;
    
    typedef struct  BITNode
    {
        char val;
        struct  BITNode *left,*right;
    }BITNode,*BITtree;
    
    
    void buildtree(BITtree &T)
    {
         char  ss;
         scanf("%c",&ss);
         if(ss=='#')
         {
             T=NULL;
             return;
         }
         else
         {
             T=(BITtree)malloc(sizeof(BITNode));
             T->val=ss;
             buildtree(T->left);
             buildtree(T->right);
         }
    }//先序建立二叉树
    
    void visit(BITtree T)
    {
        if(T->val!='#')
            printf("%c",T->val);
    }
    
    void pre_visit(BITtree T)
    {
        if(T!=NULL)
        {
             visit(T);
             pre_visit(T->left);
             pre_visit(T->right);
        }
    }//遍历方式为NLR
    
    
    void mid_visit(BITtree T)
    {
        if(T!=NULL)
        {
             mid_visit(T->left);
             visit(T);
             mid_visit(T->right);
        }
    }//遍历方式为LNR
    
    
    void beh_visit(BITtree T)
    {
        if(T!=NULL)
        {
            beh_visit(T->left);
            beh_visit(T->right);
            visit(T);
        }
    }//遍历方式为LRN
    
    
    int main()
    {
        BITtree p;
        buildtree(p);
        printf("前序遍历为:
    ");
        pre_visit(p);
        cout<<endl;
        printf("中序遍历为:
    ");
        mid_visit(p);
        cout<<endl;
        printf("后序遍历为:
    ");
        beh_visit(p);
    }
    
    
    /*
    ABC##DE#G##F###
    -+a##*b##-c##d##/e##f##
    */
    


  • 相关阅读:
    HDU1251 字典树 这题亮点在于是我自己写的字典树
    POJ3253 哈夫曼树+小根堆 【自己实现】
    poj3083 深搜
    用c语言的感觉
    poj1321 深搜
    POJ 2488 深搜
    HDU2037 今年暑假不AC 贪心
    hdu1247 Hat’s Words 字符串模拟
    Thrift框架具体使用
    如何使用Rose将类图转化为java代码
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/6978458.html
Copyright © 2020-2023  润新知