Please be nice - this is my first question. = P
Basically, as a summer project, I looked at the list of data structures on the wikipedia page and tried to implement them. Last semester, I took a C ++ course and found it to be a lot of fun, as a final project I implemented the Binomial Heap - it was also a lot of fun. I may be stubborn, but I like data structures.
In any case, enough background. The project is going well, I am starting with Binary Trees. To go much further, although I need to create iterators to traverse the trees. I already decided that I’ll create two types of iterators for each traversal method (regular iterator and constant iterator), I just don’t know how to do it. I heard about inheritance from stl iterator files or even using boosts iterator_facade (which seems like a good option)
I have not even tried to write the iterator code until I know where to start, but I have the current code on github. You can check here .
If you are against github, I am inserting the appropriate class definitions. Implementations of these functions would not really help, but if for some reason they let you know. In addition, 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 main binary tree extension of our abstract class, basically it is (will be) BST. For an example of why I need iterators, look at the definition of a search function. It should return an iterator to the node where the material is found.
#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 this is - thanks for your time! If you need more information, let me know.