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
};
我想就是這樣 - 謝謝你的時間!如果您需要其他信息,請告訴我們 .
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java 二叉树迭代器_C,为二叉树实现自定义迭代器(长)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于Java+SpringMvc+vue
- 下一篇: 修复计算机的英语,Windows Rep