二叉树

#include<queue>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;


typedef struct node
{
    char  date;
    node * Lchild;
    node * Rchild;
}BTnode, * BiTree;


void CreateBiTree(BiTree * bitree)
{
    char c=getchar();
    if(c=='.')
        *bitree = NULL;
    else
        {
            (*bitree)=(BiTree)malloc(sizeof(BTnode));
            (*bitree)->date = c;


            createBiTree(& ((*bitree)->Lchild));
            createBiTree(& ((*bitree)->Rchild));
        }
}


void PreOder(BiTree root)
{
    if(root != NULL)
    {
        print("%c",root->date);
        PreOder(root->Lchild);
        PreOder(root->Rchild);
    }
}




void InOder(BiTree root)
{
    if(root != NULL)
    {
        InOder(root->Lchild);
        print("%c", root->date);
        InOder(root->Rchild);
    }
}


void PostOder(BiTree bitree)
{
    if(root != NULL)
    {
        PostOder(root->Lchild);
        PostOder(root->Rchild);
        print("%c",root-date);
    }
}
void LayerOder(Bitree bitree)
{
    if( bitree == NULL) return;
    BTnode * p = (BTnode*)malloc(sizeof(BTnode));
    queue <BTnode*> Q;
    while( !Q.empty()) Q.pop();
    Q.push(bitree);
    while( !Q.empty())
    {
        p = Q.front();
        Q.pop();
        printf("%c",p->date);
        if( p->Lchild ) Q.push( p->Lchild );
        if( p->Rchild ) Q.push( p->Rchild );


    }
}


int CountLeaf( BiTree root)
{
    if( root==NULL ) return 0;
    if( root->Lchild==NULL && root->Rchild==NULL) return 1;
    return CountLeaf(root->Lchild)+CountLeaf(root->Rchild);
}


int GetBiTreeHigh(BiTree bitree)
{
    if(bitree==NULL)
        return 0;
        return max(GetBiTreeHigh(bitree->Lchild),GetBiTreeHigh(bitree->Rchild))+1;


}




int main()
{
    print("Input date of BiTree Node by PreOder , \nUppercase is Ok , .is NULL\n");
    int Case = 10;


    while(Case--)
    {
        BiTree tree;


        CreateBiTree(&tree);


        deepcount = 0;
        Leafcount = 0;


        printf("\n\nPreOder:\n\n")
        PreOder(tree);


        printf("\n\nInOder:\n\n")
        InOder(tree);


        printf("\n\nPostOder:\n\n")
        PostOder(tree);


        printf("\n\nLayerOder:\n\n")
        LayerOder(tree);


        printf("\n\n\nLeaves Node:\n")


        printf("\n%d Leaves Node in this BiTree\n\n",CountLeaf(tree));
        printf("\n%d is the High of this BiTree\n\n\n", GetBiTreeHigh(tree));
        printf("\n%d You can start testing this Program again\n");


        char ch = getchar();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值