实现二叉树的基本操作:建立、遍历、计算深度、结点数、叶子数等

树的相关操作

在这里插入图片描述

#include <iostream>

using namespace std;

int LeavesNumber=0;
int NodeNumber=0;
int SpecificNodeNumber=0;

typedef struct Node
{
    struct Node *left;
    struct Node *right;
    char data;
}BinaryTreeNode,*BinaryTree;

void CreateBinaryTree(BinaryTree &B)
{
    char ch;
    cin>>ch;
    if(ch == '#')
    {
        B = NULL;
    }
    else
    {
        B = new BinaryTreeNode;
        B->data = ch;
        CreateBinaryTree(B->left);
        CreateBinaryTree(B->right);
    }
}

int CountHeight(BinaryTree B)
{
    if(!B)
    {
        return 0;
    }
    else
    {
        int LeftBinaryHeight = CountHeight(B->left);
        int RightBinaryHeight = CountHeight(B->right);
        if(LeftBinaryHeight>RightBinaryHeight)
        {
            return LeftBinaryHeight+1;
        }
        else
        {
            return RightBinaryHeight+1;
        }
    }
}

void PreTravel(BinaryTree B)
{
    if(B)
    {
        cout<<B->data<<" ";
        PreTravel(B->left);
        PreTravel(B->right);
    }
}

void MidTravel(BinaryTree B)
{
    if(B)
    {
        MidTravel(B->left);
        cout<<B->data<<" ";
        MidTravel(B->right);
    }
}

void PostTravel(BinaryTree B)
{
    if(B)
    {
        PostTravel(B->left);
        PostTravel(B->right);
        cout<<B->data<<" ";
    }
}

int CountLeaves(BinaryTree B)
{
    if(B)
    {
        if(B->left == NULL&&B->right == NULL)
        {
            LeavesNumber++;
        }
        CountLeaves(B->left);
        CountLeaves(B->right);
    }
    return LeavesNumber;
}

int CountNode(BinaryTree B)
{
    if(B)
    {
        NodeNumber++;
        CountNode(B->left);
        CountNode(B->right);
    }
    return NodeNumber;
}

int CountSpecificNode(BinaryTree B,char c)
{
    if(B)
    {
        if(B->data == c)
        {
            SpecificNodeNumber++;
        }
        CountSpecificNode(B->left,c);
        CountSpecificNode(B->right,c);
    }
    return SpecificNodeNumber;
}

void PrintBinaryTree(BinaryTree B)
{
    BinaryTree stack[100],p;
    int level[100],top,n;
    if (B)
    {
        top=1;
        stack[top]=B;
        level[top]=0;
        while(top>0)
        {
            p=stack[top];
            n=level[top];
            for(int i=1; i<=n; i++)
                cout<<" ";
            cout<<p->data<<endl;
            top--;
            if (p->right!=NULL)
            {
                top++;
                stack[top]=p->right;
                level[top]=n+2;
            }
            if (p->left!=NULL)
            {
                top++;
                stack[top]=p->left;
                level[top]=n+2;
            }
        }
    }
}

void FunctionSelect(BinaryTree &B,char ch)
{
    if(ch == 'C')
    {
        cin.get();
        CreateBinaryTree(B);
        cout<<"Created success!"<<endl;
    }
    else if(ch == 'H')
    {
        cout<<"Height="<<CountHeight(B)<<"."<<endl;
    }
    else if(ch == 'L')
    {
        cout<<"Leaf="<<CountLeaves(B)<<"."<<endl;
    }
    else if(ch == 'N')
    {
        cout<<"Nodes="<<CountNode(B)<<"."<<endl;
    }
    else if(ch == '1')
    {
        cout<<"Preorder is:";
        PreTravel(B);
        cout<<"."<<endl;
    }
    else if(ch == '2')
    {
        cout<<"Inorder is:";
        MidTravel(B);
        cout<<"."<<endl;
    }
    else if(ch == '3')
    {
        cout<<"Postorder is:";
        PostTravel(B);
        cout<<"."<<endl;
    }
    else if(ch == 'F')
    {
        cin.get();
        char q;
        cin>>q;
        cout<<"The count of "<<q<<" is "<<CountSpecificNode(B,q)<<"."<<endl;
    }
    else if(ch == 'P')
    {
        cout<<"The tree is:"<<endl;
        PrintBinaryTree(B);
    }
    else
    {
        cout<<"输入不符合规定,请重新输入:"<<endl;
    }
}
int main()
{
    char a;
    BinaryTree BT;
    while(cin>>a)
    {
        FunctionSelect(BT,a);
    }
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值