【东华大学oj】二叉树:叶子结点数

二叉树:叶子结点数

时间限制: 1s

类别: DS:树->简单

问题描述

目的:使用C++模板设计二叉树的抽象数据类型(ADT)。并在此基础上,使用二叉树ADT的基本操作,设计并实现简单应用的算法设计。

内容:(1)请参照链表的ADT模板,设计二叉树的抽象数据类型。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。参考教材、课件,以及网盘中的链表ADT原型文件,自行设计二叉树的ADT。)

注意:二叉树ADT的基本操作的算法设计很多要用到递归的程序设计方法。

(2)ADT的简单应用:使用该ADT设计并实现若干应用二叉树的算法设计。

应用2:要求设计一个递归算法,计算二叉树中叶子结点的数目。二叉树的存储结构的建立参见二叉树应用1。

参考函数原型:

//统计二叉树叶子的数目 (递归函数的外壳)

template<class ElemType>

int LeafCount( BinaryTree<ElemType> &T );     

//统计二叉树叶子的数目 (递归函数)

template<class ElemType>

int LeafCount_Cursive( BinaryTreeNode<ElemType> *root, int &sum);   //采用先序遍历的递归算法

输入说明

第一行:表示无孩子或指针为空的特殊分隔符

第二行:二叉树的先序序列(结点元素之间以空格分隔)

输出说明

第一行:叶子结点的数目

#include <iostream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

template<class ElemType>
struct BinaryTreeNode
{
    ElemType data;
    BinaryTreeNode<ElemType>* LChild;
    BinaryTreeNode<ElemType>* RChild;

    BinaryTreeNode(ElemType item = ElemType(), BinaryTreeNode<ElemType>* L = nullptr, BinaryTreeNode<ElemType>* R = nullptr)
        : data(item), LChild(L), RChild(R) {}
};

template<class ElemType>
class BinaryTree
{
public:
    BinaryTreeNode<ElemType>* root;

    void destroy(BinaryTreeNode<ElemType>*& node)
    {
        if (node != nullptr)
        {
            destroy(node->LChild);
            destroy(node->RChild);
            delete node;
            node = nullptr;
        }
    }

    void preorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
    {
        if (node != nullptr)
        {
            result.push_back(node->data);
            preorder(node->LChild, result);
            preorder(node->RChild, result);
        }
    }

    void inorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
    {
        if (node != nullptr)
        {
            inorder(node->LChild, result);
            result.push_back(node->data);
            inorder(node->RChild, result);
        }
    }

    void postorder(BinaryTreeNode<ElemType>* node, vector<ElemType>& result)
    {
        if (node != nullptr)
        {
            postorder(node->LChild, result);
            postorder(node->RChild, result);
            result.push_back(node->data);
        }
    }

    BinaryTree() : root(nullptr) {}
    ~BinaryTree()
    {
        destroy(root);
    }

    void createFromPreorder(vector<ElemType> elements, ElemType empty)
    {
        auto it = elements.begin();
        root = create(it, elements.end(), empty);
    }

    BinaryTreeNode<ElemType>* create(typename vector<ElemType>::iterator& it, typename vector<ElemType>::iterator end, ElemType empty)
    {
        if (it == end || *it == empty)
        {
            return nullptr;
        }

        BinaryTreeNode<ElemType>* node = new BinaryTreeNode<ElemType>(*it);
        ++it;
        node->LChild = create(it, end, empty);
        ++it;
        node->RChild = create(it, end, empty);
        return node;
    }

    void printPreorder()
    {
        vector<ElemType> result;
        preorder(root, result);
        printResult(result);
    }

    void printInorder()
    {
        vector<ElemType> result;
        inorder(root, result);
        printResult(result);
    }

    void printPostorder()
    {
        vector<ElemType> result;
        postorder(root, result);
        printResult(result);
    }

    void printResult(const vector<ElemType>& result)
    {
        if (!result.empty())
        {
            cout << result[0];
            for (size_t i = 1; i < result.size(); ++i)
            {
                cout << ',' << result[i];
            }
        }
        cout << endl;
    }

//统计二叉树叶子的数目 (递归函数)
    int LeafCount_Cursive( BinaryTreeNode<ElemType> *T, int &sum)
    {
        if (T == nullptr)
        {
            return 0;
        }
        else if(T->LChild==nullptr&&T->RChild==nullptr)
            sum++;

        LeafCount_Cursive(T->LChild,sum);
        LeafCount_Cursive(T->RChild,sum);

        return sum;
    }
};
//统计二叉树叶子的数目 (递归函数的外壳)
template<class ElemType>
int LeafCount( BinaryTree<ElemType> &T )
{
    int sum=0;
    return T.LeafCount_Cursive(T.root,sum);
}
int main()
{
    string nullSymbol;
    string preorderInput;

    getline(cin, nullSymbol);
    getline(cin, preorderInput);

    stringstream ss(preorderInput);
    string item;
    vector<string> elements;

    while (ss >> item)
    {
        elements.push_back(item);
    }
    BinaryTree<string> tree;
    tree.createFromPreorder(elements, nullSymbol);

    cout <<LeafCount(tree)<< endl;

    return 0;
}

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ixll625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值