Tree Container Library: Node Iterators
tree

The Tree Container Library

A C++ generic tree container library which, not only works much like the STL, but is also compatible with the STL algorithms. The Tree Container Library is used worldwide in thousands of industries and organizations. It is completely free to use, and is licensed under the zlib/libpng license.

The child node iterators, node_iterator and const_node_iterator were introduced to the TCL with version 4.00. These iterators were introduced as a necessity for working with particular STL algorithms, as described below.

The child node iterators are almost identical in operation to the child element iterators. The primary difference between the two types is that the child node iterators expose the underlying node, while the child element iterators expose the underlying element. This means that the * and -> operations of the child node iterators return a pointer and reference (respecitively) to underlying node, or tree_type object. The child element iterator, on the other hand, returns a pointer and reference to a stored_type object, which is the element which resides in the node.

The signatures of these two operators are as follows.

  • tree_type& operator *()
    As described above, the * operator returns a reference to the underlying node. For the const_node_iterator, this operator returns a const reference to the node object, for node_iterator the operator returns a plain reference.
    Complexity: Constant
  • tree_type* operator ->()
    Also described above, this operator returns a pointer to the underlying node. For the const_node_iterator, this operator returns a pointer to const, for the iterator the operator returns a plain pointer.
    Complexity: Constant

The only other difference between the two types of iterators, is that the node() operation is not available to the child node iterators. Since the child node iterators expose the node with the dereference and pointer operators, there is no need for a node() operation.

Just as the child element iterator has access to the underlying node with the node() operation, the child node iterators can access the underlying element, with the node's get() operation. Note that this is an operation of the node, or tree_type, not an operation of the child node iterator.

Except for the differences described above, the child node iterator perform identically to the child element iterator, so see the discussion on the child element iterator for information on the child node iterator.

The reason for introducing the child node iterator in version 4.00, is to be able to use the child node iterators for particular STL algorithms. Certain STL algorithms, such as std::remove() need an iterator which exposes the underlying node rather than exposing the underlying element, to work properly. Other STL algorithms can take either type of iterator, but will behave differently depending on the type of iterator used.

The operations below, which are available for all four trees in the TCL, allow you to obtain a child node iterator from any node.

  • node_terator node_begin()
    const_node_iterator node_begin() const
    Returns a child node iterator which points to the first child of the node in which it's called. If the node which the operation is called has no children, returns node_end().
    Complexity: Constant
  • node_iterator node_end()
    const_node_iterator node_end() const
    Returns a child node iterator which points the the open end of the child nodes.
    Complexity: Constant