如何解决如何使每个工作为二叉树?
给出一个像这样的二叉树:
#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 举报,一经查实,本站将立刻删除。