如何使每个工作为二叉树?

问题描述

给出一个像这样的二叉树:

#include <iostream>

template <typename InfoType>
struct Node
{
    InfoType info;
    Node<InfoType> *left;
    Node<InfoType> *right;
};

template <typename InfoType>
class BinaryTree
{
public:
    bool search(const InfoType& searchItem) const;
    void insert(const InfoType& insertItem);
    void delete(const InfoType& deleteItem);
    void orderedPrint() const;

protected:
    Node<InfoType> *root;

private:
    void orderedPrint(Node<InfoType>* p) const;
};

template <typename InfoType>
void BinaryTree<InfoType>::orderedPrint() const
{
    orderedPrint(root);
}

template <typename InfoType>
void BinaryTree<InfoType>::orderedPrint(Node<InfoType>* p) const
{
    if (p != nullptr)
    {
        orderedPrint(p->left);
        std::cout << p->info << " ";
        orderedPrint(p->right);
    }
}

如何编辑该类,以便可以编写如下内容

#include <vector>

int main()
{
    std::vector<int> v;
    BinaryTree<int> tree;
    
    for (auto i: tree)
    {
        v.push_back(i);
    }

    return 0;
}

想法是能够遍历二叉树的所有内容,类似于遍历标准容器中的所有元素。

具体来说,鉴于遍历节点的递归性质,如何编写迭代代码(例如operator++())?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)