/******************************************************************************/
/* */
/* FILE: january.cpp */
/* */
/* The treeterator */
/* =============== */
/* */
/* Compiled and tested with Visual C++ 6.0 */
/* */
/* V1.00 31-JAN-2004 P. Tellenbach http://www.heimetli.ch/ */
/* */
/******************************************************************************/
#include <iostream>
#include <iterator>
#include <queue>
#include <algorithm>
template< class T >
class tree
{
public:
struct node
{
T value ;
node *left ;
node *right ;
} ;
class iterator : public std::iterator<std::forward_iterator_tag, T>
{
public:
bool operator==( const iterator &it )
{
return q == it.q ;
}
bool operator!=( const iterator &it )
{
return !(*this == it) ;
}
T &operator*()
{
return q.front()->value ;
}
T *operator->()
{
return &q.front()->value ;
}
iterator &operator++()
{
if( q.front()->left != NULL )
q.push( q.front()->left ) ;
if( q.front()->right != NULL )
q.push( q.front()->right ) ;
q.pop() ;
return *this ;
}
iterator operator++( int )
{
iterator tmp( *this ) ;
++*this ;
return tmp ;
}
iterator()
{
}
iterator( node *pn )
{
q.push( pn ) ;
}
protected:
std::queue<node *> q ;
} ;
tree( node *r ) : root(r)
{
}
iterator begin()
{
return iterator( root ) ;
}
iterator end()
{
return iterator() ;
}
protected:
node *root ;
} ;
tree<char>::node nD = { '\n', NULL, NULL } ;
tree<char>::node nC = { 'w', NULL, NULL } ;
tree<char>::node nB = { '!', NULL, NULL } ;
tree<char>::node nA = { ' ', NULL, NULL } ;
tree<char>::node n9 = { ' ', &nA, &nB } ;
tree<char>::node n8 = { 'l', &n9, &nC } ;
tree<char>::node n7 = { 'd', NULL, NULL } ;
tree<char>::node n6 = { 'l', NULL, NULL } ;
tree<char>::node n5 = { 'o', &n6, &n7 } ;
tree<char>::node n4 = { 'r', NULL, NULL } ;
tree<char>::node n3 = { 'o', NULL, NULL } ;
tree<char>::node n2 = { 'l', &n3, &n4 } ;
tree<char>::node n1 = { 'e', &n2, &n5 } ;
tree<char>::node n0 = { 'H', &n1, &n8 } ;
int main()
{
tree<char> tc( &n0 ) ;
std::copy( tc.begin(), tc.end(), std::ostream_iterator<char>(std::cout) ) ;
return 0 ;
}
Update 31. January 2024
This program compiles and runs without any changes.