请你好 - 这是我的第一个问题 . = P
基本上作为夏季项目,我一直在浏览wikipedia page上的数据结构列表并尝试实现它们 . 我上学期参加了一门C课程并发现它非常有趣,作为我实施二项式堆的最后一个项目 - 这也非常有趣 . 也许我很讨厌,但我喜欢数据结构 .
无论如何,足够的背景故事 . 项目进展顺利,我从二叉树开始 . 为了更进一步,我需要创建迭代器来遍历树 . 我已经决定为每个遍历方法(常规迭代器和常量迭代器)创建两种类型的迭代器,我只是不知道如何做到这一点 . 我听说从stl的迭代器继承,甚至使用boosts iterator_facade(这似乎是个不错的选择)
我还没有尝试编写迭代器代码,因为我不知道从哪里开始,但我确实在github上有我当前的代码 . 你可以看一下here .
如果你反对github,我会粘贴相关的类定义 . 这些功能的实现实际上没有任何帮助,但如果您出于某种原因需要它们,请告诉我 . 此外,节点类具有用于迭代目的的父指针 .
#ifndef __TREES_HXX
#define __TREES_HXX
#include // For NULL
#include // for std::max
// Node class definition. These nodes are to be used for any
// tree where the structure is
// node
// /\
// left right
// /\ /\
//
// etc., basically two children.
template
class Node
{
public:
T data_;
Node* left_;
Node* right_;
Node* parent_; // Needed for iterators
explicit Node(T const&);
Node(Node const&);
};
template
class BinaryTree
{
protected:
typedef Node* node_t;
size_t tree_size;
public:
typedef T value_type;
explicit BinaryTree();
explicit BinaryTree(T const&);
~BinaryTree();
virtual node_t insert(node_t&, T) = 0;
virtual T& lookup(node_t const&, T const&) const = 0;
inline virtual size_t size() const;
inline virtual size_t depth(node_t const&) const;
inline bool empty() const;
inline void clear(node_t);
node_t root;
};
这是我们抽象类的基本二叉树扩展,基本上它(将是)一个BST . 有关我需要迭代器的原因的示例,请查看查找函数的定义 . 它应该将迭代器返回到找到东西的节点 .
/* Implementation of our Binary Tree is in
* this file. The node class is in Trees.hxx
* because it's intended to be a general class.
*/
#ifndef __BINARY_TREE_HXX
#define __BINARY_TREE_HXX
#include "Trees.hxx"
template
class BiTree : public BinaryTree
{
private:
typedef typename BinaryTree::node_t node_t;
public:
typedef typename BinaryTree::value_type value_type;
BiTree() : BinaryTree()
{
}
BiTree(T const& data) : BinaryTree(data)
{
}
node_t insert(node_t&, T);
T& lookup(node_t const&, T const&) const; // Note: This should return an iterator to the node where the stuff is found
};
我想就是这样 - 谢谢你的时间!如果您需要其他信息,请告诉我们 .