Please be nice- this is my first question. =P
Basically as a summer project I've been going through the list of Data Structures on the wikipedia page and trying to implement them. I took a C++ course last semester and found it to be great fun, as a final project I implemented a Binomial Heap- this was also very fun. Maybe I'm nerdy but I love data structures.
Anyway, enough back story. The project is going well, I'm starting with Binary Trees. To go much further though I need to create iterators to traverse the trees. I've already decided I'll create two types of iterators for each traversal method (regular iterator and const iterator), I just have no idea how to do this. I've heard of inheriting from stl's iterator stuff, or even using boosts iterator_facade (which seems like a good option)
I have not even attempted to write the iterator code yet as I do not know where to start, but I do have my current code up on github. You can check it out here.
In case you're opposed to github, I'm pasting the relevant class definitions. The implementations of these functions wouldn't really be of any help, but if you need them for some reason let me know. Also, the node class has a parent pointer for iteration purposes.
#ifndef __TREES_HXX
#define __TREES_HXX
#include <cstdlib> // For NULL
#include <algorithm> // 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 <typename T>
class Node
{
public:
T data_;
Node<T>* left_;
Node<T>* right_;
Node<T>* parent_; // Needed for iterators
explicit Node(T const&);
Node(Node<T> const&);
};
template <typename T>
class BinaryTree
{
protected:
typedef Node<T>* 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;
};
This is the basic Binary Tree extension of our abstract class, basically it (will be) a BST. For an example of why I need iterators, look at the definition of the lookup function. It should be returning an iterator to the node where the stuff is found.
/* 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 <typename T>
class BiTree : public BinaryTree<T>
{
private:
typedef typename BinaryTree<T>::node_t node_t;
public:
typedef typename BinaryTree<T>::value_type value_type;
BiTree() : BinaryTree<T>()
{
}
BiTree(T const& data) : BinaryTree<T>(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
};
I think that's it- thanks for your time! If you need additional info let me know.
__) or begins with an underscore followed by an uppercase letter is reserved to the implementation for any use."std::mapandstd::set? (Those are typically red-black trees)