tags:

views:

137

answers:

1

I need an STL-like bidirectional iterator (operator<, begin(), rbegin(), end(), rend()) as an inner class to the following (I already spent considerable time all by myself to put together a working tree from a C# article in J of Object Tech and translated it to C++):

template<typename K, typename V> class rbtree {  
public:  
  rbtree(){  
    root = NULL;  
    numberElements = 0;  
    insertedNode = NULL;  
    nodeBeingDeleted = NULL; // Set in DeleteNode  
    siblingToRight = false; // Sibling of curNode  
    parentToRight = false; // Of grand parent  
    nodeToDeleteRed = false; // Color of deleted node  
  }  
  //...  
private:  
  struct Node {  
    // Fields  
    K key; // Generic object held by each node  
    Node* left; Node* right; Node* parent; // Links to children and parent  
    bool red;// = true; // Color of node  
    // Constructor  
    Node(){  
      red = true;  
    }  
    Node(K key, Node* parent) {  
      this->key = key;  
      this->parent = parent;  
      left = NULL; right = NULL;  
      red = true;  
    }  
  };  
  // Fields  
  Node* root;  
  //...  
};
+2  A: 

You would be suprised to know that a std::set is implemented as an Red-Black Tree. What are your reasons for writing one yourself?

For a real answer: Writing iterators isn't trivial. You should read about the common differences between iterator requirements. This Stackoverflow question is a kind of duplicate of your question and gives useful hints.

pmr
Reason for reinventing? Learning. (see comment above) Wouldn´t need to ask, if it was really trivial.Thx
gue22
@gue22: If you're learning, then going and asking for the answer doesn't exactly teach you a whole lot. Open up the source code for `std::set<T>` and you can see a working implementation all day long.
Billy ONeal
Thanks, Billy, that´s what I´m going to try next. I simply hoped for a quick answer here, cuz I got an exam tomorrow and the deadline on Fri.
gue22