java 二叉树迭代器_C,为二叉树实现自定义迭代器(长)

请你好 - 这是我的第一个问题 . = 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

};

我想就是这样 - 谢谢你的时间!如果您需要其他信息,请告诉我们 .

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值