java 二叉树迭代器_C,为二叉树实现自定义迭代器(长)
請(qǐng)你好 - 這是我的第一個(gè)問(wèn)題 . = P
基本上作為夏季項(xiàng)目,我一直在瀏覽wikipedia page上的數(shù)據(jù)結(jié)構(gòu)列表并嘗試實(shí)現(xiàn)它們 . 我上學(xué)期參加了一門C課程并發(fā)現(xiàn)它非常有趣,作為我實(shí)施二項(xiàng)式堆的最后一個(gè)項(xiàng)目 - 這也非常有趣 . 也許我很討厭,但我喜歡數(shù)據(jù)結(jié)構(gòu) .
無(wú)論如何,足夠的背景故事 . 項(xiàng)目進(jìn)展順利,我從二叉樹開(kāi)始 . 為了更進(jìn)一步,我需要?jiǎng)?chuàng)建迭代器來(lái)遍歷樹 . 我已經(jīng)決定為每個(gè)遍歷方法(常規(guī)迭代器和常量迭代器)創(chuàng)建兩種類型的迭代器,我只是不知道如何做到這一點(diǎn) . 我聽(tīng)說(shuō)從stl的迭代器繼承,甚至使用boosts iterator_facade(這似乎是個(gè)不錯(cuò)的選擇)
我還沒(méi)有嘗試編寫迭代器代碼,因?yàn)槲也恢缽哪睦镩_(kāi)始,但我確實(shí)在github上有我當(dāng)前的代碼 . 你可以看一下here .
如果你反對(duì)github,我會(huì)粘貼相關(guān)的類定義 . 這些功能的實(shí)現(xiàn)實(shí)際上沒(méi)有任何幫助,但如果您出于某種原因需要它們,請(qǐng)告訴我 . 此外,節(jié)點(diǎn)類具有用于迭代目的的父指針 .
#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;
};
這是我們抽象類的基本二叉樹擴(kuò)展,基本上它(將是)一個(gè)BST . 有關(guān)我需要迭代器的原因的示例,請(qǐng)查看查找函數(shù)的定義 . 它應(yīng)該將迭代器返回到找到東西的節(jié)點(diǎn) .
/* 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
};
我想就是這樣 - 謝謝你的時(shí)間!如果您需要其他信息,請(qǐng)告訴我們 .
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java 二叉树迭代器_C,为二叉树实现自定义迭代器(长)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 基于Java+SpringMvc+vue
- 下一篇: java list 获取索引_java