zoukankan      html  css  js  c++  java
  • 二叉树

    复习下二叉树,创建二叉树,分别以先序,中序,后续三种遍历访问二叉树,输出二叉树的叶子节点及叶子节点的个数,并输出二叉树的高度

    #include<iostream> 
    #include<cstdio> 
    #include<cstring> 
    #include<cstdlib> 
    using namespace std; 
    typedef struct BiTNode 
    {    
        char data; 
        struct BiTNode *lchild,*rchild; 
    }BiTNode,*BiTree; 
    int Max(int x,int y) 

        return x>y?x:y; 

    void Create(BiTree &T)  //先序建一颗二叉树 

        char ch; 
        scanf("%c",&ch); 
        if(ch=='#') 
        T=NULL; 
        else 
        { 
            T=(BiTNode *)malloc(sizeof(BiTNode)); 
            T->data=ch; 
            Create(T->lchild); 
            Create(T->rchild); 
        } 

    void Preorder(BiTree &root)  //先序遍历打印二叉树 

        if(root!=NULL) 
        { 
            printf("%c ",root->data); 
            Preorder(root->lchild); 
            Preorder(root->rchild); 
        } 

    void Inorder(BiTree &root)  //中序遍历打印二叉树 

        if(root!=NULL) 
        { 
            Inorder(root->lchild); 
            printf("%c ",root->data); 
            Inorder(root->rchild); 
        } 

    void Postorder(BiTree &root)  //后续遍历打印二叉树 

        if(root!=NULL) 
        { 
            Postorder(root->lchild); 
            Postorder(root->rchild); 
            printf("%c ",root->data); 
        } 

    void Preorderleaf(BiTree &root) //先序遍历输出叶子节点 

        if(root!=NULL) 
        { 
            if(root->lchild==NULL&&root->rchild==NULL) 
            printf("%c ",root->data); 
            Preorderleaf(root->lchild); 
            Preorderleaf(root->rchild); 
        } 

    int LeafCount(BiTree &root)  //统计叶子节点的个数 

        int leaf; 
        if(root==NULL) 
        leaf=0; 
        else if(root->lchild==NULL&&root->rchild==NULL) 
        leaf=1; 
        else  
        leaf=LeafCount(root->lchild)+LeafCount(root->rchild); 
        return leaf; 

    int PostTreeDepth(BiTree &root)  //统计树的高度 

        int hl,hr,max; 
        if(root!=NULL) 
        { 
            hl=PostTreeDepth(root->lchild); 
            hr=PostTreeDepth(root->rchild); 
            max=Max(hl,hr); 
            return max+1; 
        } 
        else  
        return 0; 

    void dowork() 

        BiTree cam; 
        Create(cam); 
        Preorder(cam); 
        printf(" "); 
        Inorder(cam); 
        printf(" "); 
        Postorder(cam); 
        printf(" "); 
        printf("叶子节点:"); 
        Preorderleaf(cam); 
        printf(" "); 
        printf("叶子节点的个数为:%d ",LeafCount(cam)); 
        printf("树的深度为:%d ",PostTreeDepth(cam)); 

    int main() 

        dowork(); 
        return 0; 
    }

  • 相关阅读:
    从zk监控canal-client消费延迟情况
    python面向对象——类的参数
    python面向对象——类的继承
    python并发——进程间同步和通信(二)
    python并发——线程池与进程池(转)
    python从指定目录排除部分子目录——用于删除目录
    python并发统计s3目录大小
    Java对象的序列化和反序列化
    多态、抽象类和接口
    Java输入输出流
  • 原文地址:https://www.cnblogs.com/milantgh/p/3690247.html
Copyright © 2011-2022 走看看