• 二叉树


    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    char ch;
    typedef struct BitNode{
        char data;
        struct BitNode *lchid, *rchid;
    }BitNode, *BitTree;
    
    void Create_BitTree(BitTree &T){
        if(ch == '
    ')
            return;
        scanf("%c", &ch);
        if(ch == '#')
            T = NULL;
        else{
            T=(BitTree)malloc(sizeof(BitNode));
            T->data=ch;
            Create_BitTree(T->lchid);
            Create_BitTree(T->rchid);
        }
    }
    
    void Pre_Order(BitTree &T){
        if(T) {
            printf("%c ", T->data);
            Pre_Order(T->lchid);
            Pre_Order(T->rchid);
        }
    }
    
    void In_Order(BitTree &T){
        if(T){
            In_Order(T->lchid);
            printf("%c ", T->data);
            In_Order(T->rchid);
        }
    }
    
    void Post_Order(BitTree &T){
        if(T){
            Post_Order(T->lchid);
            Post_Order(T->rchid);
            printf("%c ", T->data);
        }
    }
    
    int TreeNode(BitTree &T){
        if(T){
            return TreeNode(T->lchid) + TreeNode(T->rchid) + 1;
        }
        return 0;
    }
    
    int Leaf(BitTree &T){
        if(T==NULL) return 0;
        else if(!T->lchid&&!T->rchid) return 1;
        else return Leaf(T->lchid) + Leaf(T->rchid);
    }
    
    int BitTreeDepth(BitTree &T){
        int m, n;
        if(!T) return 0;
        else{
            m = BitTreeDepth(T->lchid);
            n = BitTreeDepth(T->rchid);
            if(m > n) return(m + 1);
            else return(n+1);
        }
    }
    // XAC#D##B##EF###
    int main(){
        BitTree T;
        Create_BitTree(T);
        printf("PreOrder:
    ");
        Pre_Order(T);
        printf("
    InOrder
    ");
        In_Order(T);
        printf("
    PostOrder
    ");
        Post_Order(T);
        printf("
    TreeNode num:
    %d
    ", TreeNode(T));
        printf("
    BitTree Depth:
    %d
    ", BitTreeDepth(T));
        printf("
    BitTree Leaf:
    %d
    ", Leaf(T));
        return 0;
    }
    

      

  • 相关阅读:
    Linux下fork机制详解(以PHP为例)
    查看Nginx、PHP、Apache和MySQL的编译参数
    MySQL更新
    Map集合的四种遍历方式
    Selenium2工作原理
    Web自动化测试框架-PO模式
    jmeter(十二)处理Cookie与Session
    java 字符串截取的几种方式
    操作JavaScript的Alert弹框
    selenium 延迟等待的三种方式
  • 原文地址:https://www.cnblogs.com/coodyz/p/10825828.html
Copyright © 2020-2023  润新知