BDE 4.14.0 Production release
|
Provide an STL compliant iterator for a tree of TreeNode
objects.
This component provides an STL-compliant bidirectional iterator over a binary tree of bslstl::TreeNode
objects. The requirements of a bidirectional STL iterator are outlined in the C++11 standard in section [24.2.6] under the tag [bidirectional.iterators]. A TreeIterator
object is parameterized on VALUE
, NODE
, and DIFFERENCE_TYPE
. The parameterized VALUE
indicates the type of the value value to which this iterator provides a references, and may be const-qualified for const-iterators. The parameterized NODE
indicates the type of the node objects in this tree. Note that NODE
is not necessarily TreeNode<VALUE>
as VALUE
may be const-qualified. Finally, the parameterized DIFFERENCE_TYPE
determines the, standard required, difference_type for the iterator. NODE
must derives from bslalg::RbTreeNode
, and contains a value
method that returns a reference providing modifiable access to a type that is convertible to the parameterized VALUE
(e.g., a bslstl::TreeNode
object).
In this section we show intended use of this component.
In the following example we create a simple tree and then use a TreeIterator
to navigate its elements.
First, we define a type IntNode
for the nodes of our tree. IntNode
inherits from bslalg::RbTreeNode
(allowing it to be operated on by bslalg::RbTreeUtil
), and supplies an integer payload:
Then, we define a comparison function for IntNode
objects. This type is designed to be supplied to functions in bslalg::RbTreeUtil
:
Next, we define the signature of the example function where we will navigate a tree using a TreeIterator
:
Then, we define a bslalg::RbTreeAnchor
object to hold our tree, and a series of nodes that we will use to populate the tree:
Next, we assign values to each of the nodes
and insert them into tree
using bslalg::RbTreeUtil
, supplying the insert
function an instance of the comparator we defined earlier:
Then, we create a type alias for a TreeIterator
providing non-modifiable access to elements in the tree. Note that in this instance, the iterator must provide non-modifiable access as modifying the key value for a node would invalidate ordering of the binary search tree:
Now, we create an instance of the TreeIterator
and use it to navigate the elements of tree
, printing their values to the console:
Finally, we observe the following console output: