zoukankan      html  css  js  c++  java
  • c语言二叉树基本操作

    编译器为vs2013

    #include "stdafx.h"

    #include<malloc.h>

    #include<stdlib.h>

     

    #define OVERFLOW -1

     

    typedef char BElemType;

    typedef int Status;

     

    typedef struct BiTree{

        BElemType data;

        struct BiTree *lchild,*rchild;

    }BitNode,*BinTree;

     

    //函数声明

    void CreatTree(BinTree &T);                      //构建二叉树并赋值

    void PreOrderTaverse(BinTree T);                 //先序遍历二叉树并输出

    void InOrderTaverse(BinTree T);                  //中序遍历二叉树并输出

    void PostOrderTaverse(BinTree T);                //后序遍历二叉树并输出

    Status DepthTree(BinTree T);                     //返回树的深度

    Status LeafNode(BinTree T, int &leaves);         //返回叶子结点个数

    Status TreeNode(BinTree T, int &node);           //返回节点总数

     

    int main()

    {

        int h, leaves=0, node=0;

        BinTree T;

        CreatTree(T);

     

        PreOrderTaverse(T);

        printf(" ");

     

        InOrderTaverse(T);

        printf(" ");

     

        PostOrderTaverse(T);

        printf(" ");

     

        h=DepthTree(T);

        leaves=LeafNode(T,leaves);

        node = TreeNode(T,node);

        printf("树的高度为%d 叶子节点数为%d 节点总数为%d ", h, leaves, node);

    }

     

    //构建二叉树并赋值

    void CreatTree(BinTree &T)

    {

        BElemType ch;

        scanf_s("%c", &ch);

        if (ch== ' ')

           T = NULL;

        else

        {

           if (!(T = (BinTree)malloc(sizeof(BitNode))))

               exit(OVERFLOW);

           T->data = ch;

           CreatTree(T->lchild);

           CreatTree(T->rchild);

        }

    }

     

    //先序遍历二叉树并输出

    void PreOrderTaverse(BinTree T)

    {

        if (T)

        {

            printf("%c ", T->data);

           PreOrderTaverse(T->lchild);

           PreOrderTaverse(T->rchild);

        }

    }

     

    //中序遍历二叉树并输出

    void InOrderTaverse(BinTree T)

    {

        if (T)

        {

           InOrderTaverse(T->lchild);

           printf("%c ", T->data);

           InOrderTaverse(T->rchild);

        }

    }

     

    //后序遍历二叉树并输出

    void PostOrderTaverse(BinTree T)

    {

        if (T)

        {

           PostOrderTaverse(T->lchild);

           PostOrderTaverse(T->rchild);

           printf("%c ", T->data);

        }

    }

     

    //返回树的深度

    Status DepthTree(BinTree T)

    {

        int dl,dr,deep;

        if (!T)

           deep = 0;

        else if ((T->lchild == NULL)&&(T->rchild == NULL))

           deep = 1;

        else

        {

           dl=DepthTree(T->lchild);

           dr=DepthTree(T->rchild);

           deep = 1 + (dl > dr ? dl : dr);

        }

        return deep;

    }

     

    //返回叶子结点个数

    Status LeafNode(BinTree T,int &leaves)

    {

        if (T)

        {

           if ((T->lchild == NULL) && (T->rchild == NULL))

               leaves++;

           LeafNode(T->lchild, leaves);

           LeafNode(T->rchild, leaves);

        }

        return leaves;

    }

     

    //返回节点总数

    Status TreeNode(BinTree T,int &node)

    {

        if (T)

        {

           node++;

           TreeNode(T->lchild, node);

           TreeNode(T->rchild, node);

        }

        return node;

    }

  • 相关阅读:
    北航软院2012级C#期末考试部分考题解答
    题目1013:开门人和关门人(字符串处理)
    char * 与char []探究理解
    题目1017:还是畅通工程(最小生成树)
    最小生成树(Prim算法+Kruskal算法)
    题目1018:统计同成绩学生人数(hash简单应用)
    GoogLeNet InceptionV2/V3/V4
    Python库
    卷积为什么如此强大?一文全解深度学习中的卷积
    神经网络训练tricks
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/6202722.html
Copyright © 2011-2022 走看看