0

I want to make an iterator that can handle my selfmade structs:

struct node {
int n; //data element
node * parent;
node * left;
node * right;  
node (int elem): n(elem){ //create root
     parent = NULL; left = NULL; right = NULL;
}
node (int elem, node* p): n(elem), parent(p) { //addchild
    left = NULL;
    right = NULL; 
}
node(int elem, node * p, node * l, node * r): n(elem), parent(p), left(l), right(r){ //insert element
}
};

First of all, is this possible? If yes: how do I start to make the iterator that traverses the tree. If not: what do you suggest for me to do if I want to access data elements in the list.

Josh Hull
  • 1,723
  • 1
  • 16
  • 24
user2321611
  • 1,049
  • 1
  • 7
  • 17
  • Are you looking for algorithms in general? [Here's](http://en.wikipedia.org/wiki/Tree_traversal) the wikipedia on tree traversal. – keyser Dec 06 '13 at 19:49
  • no not really, I want a data structure to keep pointers to classes. Like I would make a list of pointers to a certain object. But I need a tree representation. – user2321611 Dec 06 '13 at 19:51
  • Do you know a good page where it shows me how to start making an iterator to traverse the tree? – user2321611 Dec 06 '13 at 19:55

2 Answers2

1

Yes.

Hint: Your iterator, when it advances, should go to the parent and figure out whether it's a left child or right child of that parent. That will tell you where to go next.

leewz
  • 3,201
  • 1
  • 18
  • 38
  • Check the address of parents right node and left node and check wether they are equal to my own address? – user2321611 Dec 06 '13 at 20:09
  • Since it's an iterator, it's not "my own address", it's "the address of the node I'm currently pointing to". – leewz Dec 06 '13 at 20:13
1

Yes, given the node structure you've outlined with a right pointer (which presumably points to the next node in traversal order), implementing a forward iterator should be quie easy.

The basic idea is something like this:

template <class Node>
class iterator { 
    Node *pos;
public:
    iterator(Node *tree = nullptr) : pos(tree) {}
    iterator operator++() { pos = pos->right; return *this; }
    Node &operator*() { return *pos; }
    bool operator!=(iterator const &other) const { return pos != other.pos; }
};

There is a little more than that involved in a real iterator though. In particular, you normally (always?) want to specify things like the value_type, reference_type and category of the iterator. Most of these are pretty close to pure boilerplate though, and you can handle most of them by deriving from std::iterator.

You also (typically) want to support a few more operations than I've shown here, such as post-fix increment, operator==, etc. Given this as a "framework", I think filling in those blanks should be fairly straightforward though.

I should probably also point out the existence of the Boost iterators library. This simplifies the task of implementing an iterator (somewhat) and cuts down on the boilerplate you have to write (quite a bit).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Correct me if I'm wrong: pos is an address of a node and the ++ operator determines the traversal of the tree? – user2321611 Dec 07 '13 at 13:23
  • In the constructor you set tree to the nullptr, why? I would create the iterator with the root node. – user2321611 Dec 07 '13 at 13:36
  • and how do I work with the operator*: `node & a = *iter; a.methodofnode(); ` this doesn't work for me! Iter is: `node root; iterator iter(node& root);` – user2321611 Dec 07 '13 at 15:55
  • The ctor lets you specify a pointer, or if you don't specify one, it defaults to a nullptr. Assuming reasonably sane usage of the `right` pointer, this will let a default-constructed pointer signal the end of iteration. Just like with most iterators, you use `*` to access the item the iterator points at. – Jerry Coffin Dec 07 '13 at 16:05
  • node & a = *iter; I get error: invalid use of member, did you forget &. – user2321611 Dec 07 '13 at 16:13
  • I declared node root; than i want an iterator: iterator(&root); which doesn't work(it says did you forget type), when I try iterator iter(node& root); it works. Now node & a = *iter; is supposed to give me a reference to a node, but it gives me an error saying: invalid use of member! i'm stuck .. – user2321611 Dec 07 '13 at 16:32