tree.h

Go to the documentation of this file.
00001 /* 
00002 
00003    $Id: tree.hh,v 1.141 2007/04/07 21:43:13 peekas Exp $
00004 
00005    STL-like templated tree class.
00006    Copyright (C) 2001-2006  Kasper Peeters <kasper.peeters@aei.mpg.de>.
00007 
00008 */
00009 
00010 /** \mainpage tree.hh
00011     \author   Kasper Peeters
00012     \version  2.31
00013     \date     21-Aug-2007
00014     \see      http://www.aei.mpg.de/~peekas/tree/
00015     \see      http://www.aei.mpg.de/~peekas/tree/ChangeLog
00016 
00017    The tree.hh library for C++ provides an STL-like container class
00018    for n-ary trees, templated over the data stored at the
00019    nodes. Various types of iterators are provided (post-order,
00020    pre-order, and others). Where possible the access methods are
00021    compatible with the STL or alternative algorithms are
00022    available. 
00023 */
00024 
00025 
00026 /*
00027    This program is free software; you can redistribute it and/or modify
00028    it under the terms of the GNU General Public License as published by
00029    the Free Software Foundation; version 2 or 3.
00030    
00031    This program is distributed in the hope that it will be useful,
00032    but WITHOUT ANY WARRANTY; without even the implied warranty of
00033    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00034    GNU General Public License for more details.
00035    
00036    You should have received a copy of the GNU General Public License
00037    along with this program; if not, write to the Free Software
00038    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00039 */
00040 
00041 /** \todo 
00042    - New-style move members are not completely finished yet.
00043    - It would be good to have an iterator which can iterate over all
00044      nodes below a given node.
00045    - Fixed depth iterators do not iterate over the entire range if there
00046      are 'holes' in the tree.
00047    - If a range uses const iter_base& as end iterator, things will
00048      inevitably go wrong, because upcast from iter_base to a non-sibling_iter
00049      is incorrect. This upcast should be removed (and then all illegal uses
00050      as previously in 'equal' will be flagged by the compiler). This requires
00051      new copy constructors though.
00052    - There's a bug in replace(sibling_iterator, ...) when the ranges
00053      sit next to each other. Turned up in append_child(iter,iter)
00054      but has been avoided now.
00055    - "std::operator<" does not work correctly on our iterators, and for some
00056      reason a globally defined template operator< did not get picked up. 
00057      Using a comparison class now, but this should be investigated.
00058 */
00059 
00060 #ifndef tree_hh_
00061 #define tree_hh_
00062 
00063 #include <cassert>
00064 #include <memory>
00065 #include <stdexcept>
00066 #include <iterator>
00067 #include <set>
00068 #include <queue>
00069 #include <iostream>
00070 
00071 // HP-style construct/destroy have gone from the standard,
00072 // so here is a copy.
00073 
00074 namespace kp {
00075 
00076 template <class T1, class T2>
00077 void constructor(T1* p, T2& val) 
00078    {
00079    new ((void *) p) T1(val);
00080    }
00081 
00082 template <class T1>
00083 void constructor(T1* p) 
00084    {
00085    new ((void *) p) T1;
00086    }
00087 
00088 template <class T1>
00089 void destructor(T1* p)
00090    {
00091    p->~T1();
00092    }
00093 
00094 }
00095 
00096 /// A node in the tree, combining links to other nodes as well as the actual data.
00097 template<class T>
00098 class tree_node_ { // size: 5*4=20 bytes (on 32 bit arch), can be reduced by 8.
00099    public:
00100       tree_node_<T> *parent;
00101       tree_node_<T> *first_child, *last_child;
00102       tree_node_<T> *prev_sibling, *next_sibling;
00103       T data;
00104 }; // __attribute__((packed));
00105 
00106 template <class T, class tree_node_allocator = std::allocator<tree_node_<T> > >
00107 class tree {
00108    protected:
00109       typedef tree_node_<T> tree_node;
00110    public:
00111       /// Value of the data stored at a node.
00112       typedef T value_type;
00113 
00114       class iterator_base;
00115       class pre_order_iterator;
00116       class post_order_iterator;
00117       class sibling_iterator;
00118       class leaf_iterator;
00119 
00120       tree();
00121       tree(const T&);
00122       tree(const iterator_base&);
00123       tree(const tree<T, tree_node_allocator>&);
00124       ~tree();
00125       void operator=(const tree<T, tree_node_allocator>&);
00126 
00127       /// Base class for iterators, only pointers stored, no traversal logic.
00128 #ifdef __SGI_STL_PORT
00129       class iterator_base : public stlport::bidirectional_iterator<T, ptrdiff_t> {
00130 #else
00131       class iterator_base {
00132 #endif
00133          public:
00134             typedef T                               value_type;
00135             typedef T*                              pointer;
00136             typedef T&                              reference;
00137             typedef size_t                          size_type;
00138             typedef ptrdiff_t                       difference_type;
00139             typedef std::bidirectional_iterator_tag iterator_category;
00140 
00141             iterator_base();
00142             iterator_base(tree_node *);
00143 
00144             T&             operator*() const;
00145             T*             operator->() const;
00146 
00147             /// When called, the next increment/decrement skips children of this node.
00148             void         skip_children();
00149             /// Number of children of the node pointed to by the iterator.
00150             unsigned int number_of_children() const;
00151 
00152             sibling_iterator begin() const;
00153             sibling_iterator end() const;
00154 
00155             tree_node *node;
00156          protected:
00157             bool skip_current_children_;
00158       };
00159 
00160       /// Depth-first iterator, first accessing the node, then its children.
00161       class pre_order_iterator : public iterator_base { 
00162          public:
00163             pre_order_iterator();
00164             pre_order_iterator(tree_node *);
00165             pre_order_iterator(const iterator_base&);
00166             pre_order_iterator(const sibling_iterator&);
00167 
00168             bool    operator==(const pre_order_iterator&) const;
00169             bool    operator!=(const pre_order_iterator&) const;
00170             pre_order_iterator&  operator++();
00171             pre_order_iterator&  operator--();
00172             pre_order_iterator   operator++(int);
00173             pre_order_iterator   operator--(int);
00174             pre_order_iterator&  operator+=(unsigned int);
00175             pre_order_iterator&  operator-=(unsigned int);
00176       };
00177 
00178       /// Depth-first iterator, first accessing the children, then the node itself.
00179       class post_order_iterator : public iterator_base {
00180          public:
00181             post_order_iterator();
00182             post_order_iterator(tree_node *);
00183             post_order_iterator(const iterator_base&);
00184             post_order_iterator(const sibling_iterator&);
00185 
00186             bool    operator==(const post_order_iterator&) const;
00187             bool    operator!=(const post_order_iterator&) const;
00188             post_order_iterator&  operator++();
00189             post_order_iterator&  operator--();
00190             post_order_iterator   operator++(int);
00191             post_order_iterator   operator--(int);
00192             post_order_iterator&  operator+=(unsigned int);
00193             post_order_iterator&  operator-=(unsigned int);
00194 
00195             /// Set iterator to the first child as deep as possible down the tree.
00196             void descend_all();
00197       };
00198 
00199       /// Breadth-first iterator, using a queue
00200       class breadth_first_queued_iterator : public iterator_base {
00201          public:
00202             breadth_first_queued_iterator();
00203             breadth_first_queued_iterator(tree_node *);
00204             breadth_first_queued_iterator(const iterator_base&);
00205 
00206             bool    operator==(const breadth_first_queued_iterator&) const;
00207             bool    operator!=(const breadth_first_queued_iterator&) const;
00208             breadth_first_queued_iterator&  operator++();
00209             breadth_first_queued_iterator   operator++(int);
00210             breadth_first_queued_iterator&  operator+=(unsigned int);
00211 
00212          private:
00213             std::queue<tree_node *> traversal_queue;
00214       };
00215 
00216       /// The default iterator types throughout the tree class.
00217       typedef pre_order_iterator            iterator;
00218       typedef breadth_first_queued_iterator breadth_first_iterator;
00219 
00220       /// Iterator which traverses only the nodes at a given depth from the root.
00221       class fixed_depth_iterator : public iterator_base {
00222          public:
00223             fixed_depth_iterator();
00224             fixed_depth_iterator(tree_node *);
00225             fixed_depth_iterator(const iterator_base&);
00226             fixed_depth_iterator(const sibling_iterator&);
00227             fixed_depth_iterator(const fixed_depth_iterator&);
00228 
00229             bool    operator==(const fixed_depth_iterator&) const;
00230             bool    operator!=(const fixed_depth_iterator&) const;
00231             fixed_depth_iterator&  operator++();
00232             fixed_depth_iterator&  operator--();
00233             fixed_depth_iterator   operator++(int);
00234             fixed_depth_iterator   operator--(int);
00235             fixed_depth_iterator&  operator+=(unsigned int);
00236             fixed_depth_iterator&  operator-=(unsigned int);
00237 
00238             tree_node *first_parent_;
00239          private:
00240             void set_first_parent_();
00241             void find_leftmost_parent_();
00242       };
00243 
00244       /// Iterator which traverses only the nodes which are siblings of each other.
00245       class sibling_iterator : public iterator_base {
00246          public:
00247             sibling_iterator();
00248             sibling_iterator(tree_node *);
00249             sibling_iterator(const sibling_iterator&);
00250             sibling_iterator(const iterator_base&);
00251 
00252             bool    operator==(const sibling_iterator&) const;
00253             bool    operator!=(const sibling_iterator&) const;
00254             sibling_iterator&  operator++();
00255             sibling_iterator&  operator--();
00256             sibling_iterator   operator++(int);
00257             sibling_iterator   operator--(int);
00258             sibling_iterator&  operator+=(unsigned int);
00259             sibling_iterator&  operator-=(unsigned int);
00260 
00261             tree_node *range_first() const;
00262             tree_node *range_last() const;
00263             tree_node *parent_;
00264          private:
00265             void set_parent_();
00266       };
00267 
00268       /// Iterator which traverses only the leaves.
00269       class leaf_iterator : public iterator_base {
00270          public:
00271             leaf_iterator();
00272             leaf_iterator(tree_node *);
00273             leaf_iterator(const sibling_iterator&);
00274             leaf_iterator(const iterator_base&);
00275 
00276             bool    operator==(const leaf_iterator&) const;
00277             bool    operator!=(const leaf_iterator&) const;
00278             leaf_iterator&  operator++();
00279             leaf_iterator&  operator--();
00280             leaf_iterator   operator++(int);
00281             leaf_iterator   operator--(int);
00282             leaf_iterator&  operator+=(unsigned int);
00283             leaf_iterator&  operator-=(unsigned int);
00284       };
00285 
00286       /// Return iterator to the beginning of the tree.
00287       inline pre_order_iterator   begin() const;
00288       /// Return iterator to the end of the tree.
00289       inline pre_order_iterator   end() const;
00290       /// Return post-order iterator to the beginning of the tree.
00291       post_order_iterator  begin_post() const;
00292       /// Return post-order iterator to the end of the tree.
00293       post_order_iterator  end_post() const;
00294       /// Return fixed-depth iterator to the first node at a given depth.
00295       fixed_depth_iterator begin_fixed(const iterator_base&, unsigned int) const;
00296       /// Return fixed-depth iterator to end of the nodes at given depth.
00297       fixed_depth_iterator end_fixed(const iterator_base&, unsigned int) const;
00298       /// Return breadth-first iterator to the first node at a given depth.
00299       breadth_first_queued_iterator begin_breadth_first() const;
00300       /// Return breadth-first iterator to end of the nodes at given depth.
00301       breadth_first_queued_iterator end_breadth_first() const;
00302       /// Return sibling iterator to the first child of given node.
00303       sibling_iterator     begin(const iterator_base&) const;
00304       /// Return sibling iterator to the end of the children of a given node.
00305       sibling_iterator     end(const iterator_base&) const;
00306       /// Return leaf iterator to the first leaf of the tree.
00307       leaf_iterator   begin_leaf() const;
00308       /// Return leaf iterator to the last leaf of the tree.
00309       leaf_iterator   end_leaf() const;
00310 
00311       /// Return iterator to the parent of a node.
00312       template<typename iter> static iter parent(iter);
00313       /// Return iterator to the previous sibling of a node.
00314       template<typename iter> iter previous_sibling(iter) const;
00315       /// Return iterator to the next sibling of a node.
00316       template<typename iter> iter next_sibling(iter) const;
00317       /// Return iterator to the next node at a given depth.
00318       template<typename iter> iter next_at_same_depth(iter) const;
00319 
00320       /// Erase all nodes of the tree.
00321       void     clear();
00322       /// Erase element at position pointed to by iterator, return incremented iterator.
00323       template<typename iter> iter erase(iter);
00324       /// Erase all children of the node pointed to by iterator.
00325       void     erase_children(const iterator_base&);
00326 
00327       /// Insert empty node as last/first child of node pointed to by position.
00328       template<typename iter> iter append_child(iter position); 
00329       template<typename iter> iter prepend_child(iter position); 
00330       /// Insert node as last/first child of node pointed to by position.
00331       template<typename iter> iter append_child(iter position, const T& x);
00332       template<typename iter> iter prepend_child(iter position, const T& x);
00333       /// Append the node (plus its children) at other_position as last/first child of position.
00334       template<typename iter> iter append_child(iter position, iter other_position);
00335       template<typename iter> iter prepend_child(iter position, iter other_position);
00336       /// Append the nodes in the from-to range (plus their children) as last/first children of position.
00337       template<typename iter> iter append_children(iter position, sibling_iterator from, sibling_iterator to);
00338       template<typename iter> iter prepend_children(iter position, sibling_iterator from, sibling_iterator to);
00339 
00340       /// Short-hand to insert topmost node in otherwise empty tree.
00341       pre_order_iterator set_head(const T& x);
00342       /// Insert node as previous sibling of node pointed to by position.
00343       template<typename iter> iter insert(iter position, const T& x);
00344       /// Specialisation of previous member.
00345       sibling_iterator insert(sibling_iterator position, const T& x);
00346       /// Insert node (with children) pointed to by subtree as previous sibling of node pointed to by position.
00347       template<typename iter> iter insert_subtree(iter position, const iterator_base& subtree);
00348       /// Insert node as next sibling of node pointed to by position.
00349       template<typename iter> iter insert_after(iter position, const T& x);
00350       /// Insert node (with children) pointed to by subtree as next sibling of node pointed to by position.
00351       template<typename iter> iter insert_subtree_after(iter position, const iterator_base& subtree);
00352 
00353       /// Replace node at 'position' with other node (keeping same children); 'position' becomes invalid.
00354       template<typename iter> iter replace(iter position, const T& x);
00355       /// Replace node at 'position' with subtree starting at 'from' (do not erase subtree at 'from'); see above.
00356       template<typename iter> iter replace(iter position, const iterator_base& from);
00357       /// Replace string of siblings (plus their children) with copy of a new string (with children); see above
00358       sibling_iterator replace(sibling_iterator orig_begin, sibling_iterator orig_end, 
00359                                sibling_iterator new_begin,  sibling_iterator new_end); 
00360 
00361       /// Move all children of node at 'position' to be siblings, returns position.
00362       template<typename iter> iter flatten(iter position);
00363       /// Move nodes in range to be children of 'position'.
00364       template<typename iter> iter reparent(iter position, sibling_iterator begin, sibling_iterator end);
00365       /// Move all child nodes of 'from' to be children of 'position'.
00366       template<typename iter> iter reparent(iter position, iter from);
00367 
00368       /// Replace node with a new node, making the old node a child of the new node.
00369       template<typename iter> iter wrap(iter position, const T& x);
00370 
00371       /// Move 'source' node (plus its children) to become the next sibling of 'target'.
00372       template<typename iter> iter move_after(iter target, iter source);
00373       /// Move 'source' node (plus its children) to become the previous sibling of 'target'.
00374       template<typename iter> iter move_before(iter target, iter source);
00375       sibling_iterator move_before(sibling_iterator target, sibling_iterator source);
00376       /// Move 'source' node (plus its children) to become the node at 'target' (erasing the node at 'target').
00377       template<typename iter> iter move_ontop(iter target, iter source);
00378 
00379       /// Merge with other tree, creating new branches and leaves only if they are not already present.
00380       void     merge(sibling_iterator, sibling_iterator, sibling_iterator, sibling_iterator, 
00381                      bool duplicate_leaves=false);
00382       /// Sort (std::sort only moves values of nodes, this one moves children as well).
00383       void     sort(sibling_iterator from, sibling_iterator to, bool deep=false);
00384       template<class StrictWeakOrdering>
00385       void     sort(sibling_iterator from, sibling_iterator to, StrictWeakOrdering comp, bool deep=false);
00386       /// Compare two ranges of nodes (compares nodes as well as tree structure).
00387       template<typename iter>
00388       bool     equal(const iter& one, const iter& two, const iter& three) const;
00389       template<typename iter, class BinaryPredicate>
00390       bool     equal(const iter& one, const iter& two, const iter& three, BinaryPredicate) const;
00391       template<typename iter>
00392       bool     equal_subtree(const iter& one, const iter& two) const;
00393       template<typename iter, class BinaryPredicate>
00394       bool     equal_subtree(const iter& one, const iter& two, BinaryPredicate) const;
00395       /// Extract a new tree formed by the range of siblings plus all their children.
00396       tree     subtree(sibling_iterator from, sibling_iterator to) const;
00397       void     subtree(tree&, sibling_iterator from, sibling_iterator to) const;
00398       /// Exchange the node (plus subtree) with its sibling node (do nothing if no sibling present).
00399       void     swap(sibling_iterator it);
00400       /// Exchange two nodes (plus subtrees)
00401       void     swap(iterator, iterator);
00402       
00403       /// Count the total number of nodes.
00404       int      size() const;
00405       /// Count the total number of nodes below the indicated node (plus one).
00406       int      size(const iterator_base&) const;
00407       /// Check if tree is empty.
00408       bool     empty() const;
00409       /// Compute the depth to the root.
00410       int      depth(const iterator_base&) const;
00411       /// Count the number of children of node at position.
00412       static unsigned int number_of_children(const iterator_base&);
00413       /// Count the number of 'next' siblings of node at iterator.
00414       unsigned int number_of_siblings(const iterator_base&) const;
00415       /// Determine whether node at position is in the subtrees with root in the range.
00416       bool     is_in_subtree(const iterator_base& position, const iterator_base& begin, 
00417                              const iterator_base& end) const;
00418       /// Determine whether the iterator is an 'end' iterator and thus not actually pointing to a node.
00419       bool     is_valid(const iterator_base&) const;
00420 
00421       /// Determine the index of a node in the range of siblings to which it belongs.
00422       unsigned int index(sibling_iterator it) const;
00423       /// Inverse of 'index': return the n-th child of the node at position.
00424       sibling_iterator  child(const iterator_base& position, unsigned int) const;
00425       
00426       /// Comparator class for iterators (compares pointer values; why doesn't this work automatically?)
00427       class iterator_base_less {
00428          public:
00429             bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
00430                             const typename tree<T, tree_node_allocator>::iterator_base& two) const
00431                {
00432                return one.node < two.node;
00433                }
00434       };
00435       tree_node *head, *feet;    // head/feet are always dummy; if an iterator points to them it is invalid
00436    private:
00437       tree_node_allocator alloc_;
00438       void head_initialise_();
00439       void copy_(const tree<T, tree_node_allocator>& other);
00440 
00441       /// Comparator class for two nodes of a tree (used for sorting and searching).
00442       template<class StrictWeakOrdering>
00443       class compare_nodes {
00444          public:
00445             compare_nodes(StrictWeakOrdering comp) : comp_(comp) {};
00446             
00447             bool operator()(const tree_node *a, const tree_node *b) 
00448                {
00449                static StrictWeakOrdering comp;
00450                return comp(a->data, b->data);
00451                }
00452          private:
00453             StrictWeakOrdering comp_;
00454       };
00455 };
00456 
00457 //template <class T, class tree_node_allocator>
00458 //class iterator_base_less {
00459 // public:
00460 //    bool operator()(const typename tree<T, tree_node_allocator>::iterator_base& one,
00461 //                  const typename tree<T, tree_node_allocator>::iterator_base& two) const
00462 //       {
00463 //       txtout << "operatorclass<" << one.node < two.node << std::endl;
00464 //       return one.node < two.node;
00465 //       }
00466 //};
00467 
00468 // template <class T, class tree_node_allocator>
00469 // bool operator<(const typename tree<T, tree_node_allocator>::iterator& one,
00470 //                const typename tree<T, tree_node_allocator>::iterator& two)
00471 //    {
00472 //    txtout << "operator< " << one.node < two.node << std::endl;
00473 //    if(one.node < two.node) return true;
00474 //    return false;
00475 //    }
00476 // 
00477 // template <class T, class tree_node_allocator>
00478 // bool operator==(const typename tree<T, tree_node_allocator>::iterator& one,
00479 //                const typename tree<T, tree_node_allocator>::iterator& two)
00480 //    {
00481 //    txtout << "operator== " << one.node == two.node << std::endl;
00482 //    if(one.node == two.node) return true;
00483 //    return false;
00484 //    }
00485 // 
00486 // template <class T, class tree_node_allocator>
00487 // bool operator>(const typename tree<T, tree_node_allocator>::iterator_base& one,
00488 //                const typename tree<T, tree_node_allocator>::iterator_base& two)
00489 //    {
00490 //    txtout << "operator> " << one.node < two.node << std::endl;
00491 //    if(one.node > two.node) return true;
00492 //    return false;
00493 //    }
00494 
00495 
00496 
00497 // Tree
00498 
00499 template <class T, class tree_node_allocator>
00500 tree<T, tree_node_allocator>::tree() 
00501    {
00502    head_initialise_();
00503    }
00504 
00505 template <class T, class tree_node_allocator>
00506 tree<T, tree_node_allocator>::tree(const T& x) 
00507    {
00508    head_initialise_();
00509    set_head(x);
00510    }
00511 
00512 template <class T, class tree_node_allocator>
00513 tree<T, tree_node_allocator>::tree(const iterator_base& other)
00514    {
00515    head_initialise_();
00516    set_head((*other));
00517    replace(begin(), other);
00518    }
00519 
00520 template <class T, class tree_node_allocator>
00521 tree<T, tree_node_allocator>::~tree()
00522    {
00523    clear();
00524    alloc_.deallocate(head,1);
00525    alloc_.deallocate(feet,1);
00526    }
00527 
00528 template <class T, class tree_node_allocator>
00529 void tree<T, tree_node_allocator>::head_initialise_() 
00530    { 
00531    head = alloc_.allocate(1,0); // MSVC does not have default second argument 
00532    feet = alloc_.allocate(1,0);
00533 
00534    head->parent=0;
00535    head->first_child=0;
00536    head->last_child=0;
00537    head->prev_sibling=0; //head;
00538    head->next_sibling=feet; //head;
00539 
00540    feet->parent=0;
00541    feet->first_child=0;
00542    feet->last_child=0;
00543    feet->prev_sibling=head;
00544    feet->next_sibling=0;
00545    }
00546 
00547 template <class T, class tree_node_allocator>
00548 void tree<T, tree_node_allocator>::operator=(const tree<T, tree_node_allocator>& other)
00549    {
00550    copy_(other);
00551    }
00552 
00553 template <class T, class tree_node_allocator>
00554 tree<T, tree_node_allocator>::tree(const tree<T, tree_node_allocator>& other)
00555    {
00556    head_initialise_();
00557    copy_(other);
00558    }
00559 
00560 template <class T, class tree_node_allocator>
00561 void tree<T, tree_node_allocator>::copy_(const tree<T, tree_node_allocator>& other) 
00562    {
00563    clear();
00564    pre_order_iterator it=other.begin(), to=begin();
00565    while(it!=other.end()) {
00566       to=insert(to, (*it));
00567       it.skip_children();
00568       ++it;
00569       }
00570    to=begin();
00571    it=other.begin();
00572    while(it!=other.end()) {
00573       to=replace(to, it);
00574       to.skip_children();
00575       it.skip_children();
00576       ++to;
00577       ++it;
00578       }
00579    }
00580 
00581 template <class T, class tree_node_allocator>
00582 void tree<T, tree_node_allocator>::clear()
00583    {
00584    if(head)
00585       while(head->next_sibling!=feet)
00586          erase(pre_order_iterator(head->next_sibling));
00587    }
00588 
00589 template<class T, class tree_node_allocator> 
00590 void tree<T, tree_node_allocator>::erase_children(const iterator_base& it)
00591    {
00592 // std::cout << "erase_children " << it.node << std::endl;
00593    if(it.node==0) return;
00594 
00595    tree_node *cur=it.node->first_child;
00596    tree_node *prev=0;
00597 
00598    while(cur!=0) {
00599       prev=cur;
00600       cur=cur->next_sibling;
00601       erase_children(pre_order_iterator(prev));
00602       kp::destructor(&prev->data);
00603       alloc_.deallocate(prev,1);
00604       }
00605    it.node->first_child=0;
00606    it.node->last_child=0;
00607 // std::cout << "exit" << std::endl;
00608    }
00609 
00610 template<class T, class tree_node_allocator> 
00611 template<class iter>
00612 iter tree<T, tree_node_allocator>::erase(iter it)
00613    {
00614    tree_node *cur=it.node;
00615    assert(cur!=head);
00616    iter ret=it;
00617    ret.skip_children();
00618    ++ret;
00619    erase_children(it);
00620    if(cur->prev_sibling==0) {
00621       cur->parent->first_child=cur->next_sibling;
00622       }
00623    else {
00624       cur->prev_sibling->next_sibling=cur->next_sibling;
00625       }
00626    if(cur->next_sibling==0) {
00627       cur->parent->last_child=cur->prev_sibling;
00628       }
00629    else {
00630       cur->next_sibling->prev_sibling=cur->prev_sibling;
00631       }
00632 
00633    kp::destructor(&cur->data);
00634    alloc_.deallocate(cur,1);
00635    return ret;
00636    }
00637 
00638 template <class T, class tree_node_allocator>
00639 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::begin() const
00640    {
00641    return pre_order_iterator(head->next_sibling);
00642    }
00643 
00644 template <class T, class tree_node_allocator>
00645 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::end() const
00646    {
00647    return pre_order_iterator(feet);
00648    }
00649 
00650 template <class T, class tree_node_allocator>
00651 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::begin_breadth_first() const
00652    {
00653    return breadth_first_queued_iterator(head->next_sibling);
00654    }
00655 
00656 template <class T, class tree_node_allocator>
00657 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::end_breadth_first() const
00658    {
00659    return breadth_first_queued_iterator();
00660    }
00661 
00662 template <class T, class tree_node_allocator>
00663 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::begin_post() const
00664    {
00665    tree_node *tmp=head->next_sibling;
00666    if(tmp!=feet) {
00667       while(tmp->first_child)
00668          tmp=tmp->first_child;
00669       }
00670    return post_order_iterator(tmp);
00671    }
00672 
00673 template <class T, class tree_node_allocator>
00674 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::end_post() const
00675    {
00676    return post_order_iterator(feet);
00677    }
00678 
00679 template <class T, class tree_node_allocator>
00680 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::begin_fixed(const iterator_base& pos, unsigned int dp) const
00681    {
00682    tree_node *tmp=pos.node;
00683    unsigned int curdepth=0;
00684    while(curdepth<dp) { // go down one level
00685       while(tmp->first_child==0) {
00686          tmp=tmp->next_sibling;
00687          if(tmp==0)
00688             throw std::range_error("tree: begin_fixed out of range");
00689          }
00690       tmp=tmp->first_child;
00691       ++curdepth;
00692       }
00693    return tmp;
00694    }
00695 
00696 template <class T, class tree_node_allocator>
00697 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::end_fixed(const iterator_base& pos, unsigned int dp) const
00698    {
00699    assert(1==0); // FIXME: not correct yet
00700    tree_node *tmp=pos.node;
00701    unsigned int curdepth=1;
00702    while(curdepth<dp) { // go down one level
00703       while(tmp->first_child==0) {
00704          tmp=tmp->next_sibling;
00705          if(tmp==0)
00706             throw std::range_error("tree: end_fixed out of range");
00707          }
00708       tmp=tmp->first_child;
00709       ++curdepth;
00710       }
00711    return tmp;
00712    }
00713 
00714 template <class T, class tree_node_allocator>
00715 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::begin(const iterator_base& pos) const
00716    {
00717    assert(pos.node!=0);
00718    if(pos.node->first_child==0) {
00719       return end(pos);
00720       }
00721    return pos.node->first_child;
00722    }
00723 
00724 template <class T, class tree_node_allocator>
00725 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::end(const iterator_base& pos) const
00726    {
00727    sibling_iterator ret(0);
00728    ret.parent_=pos.node;
00729    return ret;
00730    }
00731 
00732 template <class T, class tree_node_allocator>
00733 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::begin_leaf() const
00734    {
00735    tree_node *tmp=head->next_sibling;
00736    if(tmp!=feet) {
00737       while(tmp->first_child)
00738          tmp=tmp->first_child;
00739       }
00740    return leaf_iterator(tmp);
00741    }
00742 
00743 template <class T, class tree_node_allocator>
00744 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::end_leaf() const
00745    {
00746    return leaf_iterator(feet);
00747    }
00748 
00749 template <class T, class tree_node_allocator>
00750 template <typename iter>
00751 iter tree<T, tree_node_allocator>::parent(iter position) 
00752    {
00753    assert(position.node!=0);
00754    return iter(position.node->parent);
00755    }
00756 
00757 template <class T, class tree_node_allocator>
00758 template <typename iter>
00759 iter tree<T, tree_node_allocator>::previous_sibling(iter position) const
00760    {
00761    assert(position.node!=0);
00762    iter ret(position);
00763    ret.node=position.node->prev_sibling;
00764    return ret;
00765    }
00766 
00767 template <class T, class tree_node_allocator>
00768 template <typename iter>
00769 iter tree<T, tree_node_allocator>::next_sibling(iter position) const
00770    {
00771    assert(position.node!=0);
00772    iter ret(position);
00773    ret.node=position.node->next_sibling;
00774    return ret;
00775    }
00776 
00777 template <class T, class tree_node_allocator>
00778 template <typename iter>
00779 iter tree<T, tree_node_allocator>::next_at_same_depth(iter position) const
00780    {
00781    assert(position.node!=0);
00782    iter ret(position);
00783 
00784    if(position.node->next_sibling) {
00785       ret.node=position.node->next_sibling;
00786       }
00787    else { 
00788       int relative_depth=0;
00789       upper:
00790       do {
00791          ret.node=ret.node->parent;
00792          if(ret.node==0) return ret;
00793          --relative_depth;
00794          } while(ret.node->next_sibling==0);
00795       lower:
00796       ret.node=ret.node->next_sibling;
00797       while(ret.node->first_child==0) {
00798          if(ret.node->next_sibling==0)
00799             goto upper;
00800          ret.node=ret.node->next_sibling;
00801          if(ret.node==0) return ret;
00802          }
00803       while(relative_depth<0 && ret.node->first_child!=0) {
00804          ret.node=ret.node->first_child;
00805          ++relative_depth;
00806          }
00807       if(relative_depth<0) {
00808          if(ret.node->next_sibling==0) goto upper;
00809          else                          goto lower;
00810          }
00811       }
00812    return ret;
00813    }
00814 
00815 template <class T, class tree_node_allocator>
00816 template <typename iter>
00817 iter tree<T, tree_node_allocator>::append_child(iter position)
00818    {
00819    assert(position.node!=head);
00820    assert(position.node);
00821 
00822    tree_node *tmp=alloc_.allocate(1,0);
00823    kp::constructor(&tmp->data);
00824    tmp->first_child=0;
00825    tmp->last_child=0;
00826 
00827    tmp->parent=position.node;
00828    if(position.node->last_child!=0) {
00829       position.node->last_child->next_sibling=tmp;
00830       }
00831    else {
00832       position.node->first_child=tmp;
00833       }
00834    tmp->prev_sibling=position.node->last_child;
00835    position.node->last_child=tmp;
00836    tmp->next_sibling=0;
00837    return tmp;
00838    }
00839 
00840 template <class T, class tree_node_allocator>
00841 template <typename iter>
00842 iter tree<T, tree_node_allocator>::prepend_child(iter position)
00843    {
00844    assert(position.node!=head);
00845    assert(position.node);
00846 
00847    tree_node *tmp=alloc_.allocate(1,0);
00848    kp::constructor(&tmp->data);
00849    tmp->first_child=0;
00850    tmp->last_child=0;
00851 
00852    tmp->parent=position.node;
00853    if(position.node->first_child!=0) {
00854       position.node->first_child->prev_sibling=tmp;
00855       }
00856    else {
00857       position.node->last_child=tmp;
00858       }
00859    tmp->next_sibling=position.node->first_child;
00860    position.node->prev_child=tmp;
00861    tmp->prev_sibling=0;
00862    return tmp;
00863    }
00864 
00865 template <class T, class tree_node_allocator>
00866 template <class iter>
00867 iter tree<T, tree_node_allocator>::append_child(iter position, const T& x)
00868    {
00869    // If your program fails here you probably used 'append_child' to add the top
00870    // node to an empty tree. From version 1.45 the top element should be added
00871    // using 'insert'. See the documentation for further information, and sorry about
00872    // the API change.
00873    assert(position.node!=head);
00874    assert(position.node);
00875 
00876    tree_node* tmp = alloc_.allocate(1,0);
00877    kp::constructor(&tmp->data, x);
00878    tmp->first_child=0;
00879    tmp->last_child=0;
00880 
00881    tmp->parent=position.node;
00882    if(position.node->last_child!=0) {
00883       position.node->last_child->next_sibling=tmp;
00884       }
00885    else {
00886       position.node->first_child=tmp;
00887       }
00888    tmp->prev_sibling=position.node->last_child;
00889    position.node->last_child=tmp;
00890    tmp->next_sibling=0;
00891    return tmp;
00892    }
00893 
00894 template <class T, class tree_node_allocator>
00895 template <class iter>
00896 iter tree<T, tree_node_allocator>::prepend_child(iter position, const T& x)
00897    {
00898    assert(position.node!=head);
00899    assert(position.node);
00900 
00901    tree_node* tmp = alloc_.allocate(1,0);
00902    kp::constructor(&tmp->data, x);
00903    tmp->first_child=0;
00904    tmp->last_child=0;
00905 
00906    tmp->parent=position.node;
00907    if(position.node->first_child!=0) {
00908       position.node->first_child->prev_sibling=tmp;
00909       }
00910    else {
00911       position.node->last_child=tmp;
00912       }
00913    tmp->next_sibling=position.node->first_child;
00914    position.node->first_child=tmp;
00915    tmp->prev_sibling=0;
00916    return tmp;
00917    }
00918 
00919 template <class T, class tree_node_allocator>
00920 template <class iter>
00921 iter tree<T, tree_node_allocator>::append_child(iter position, iter other)
00922    {
00923    assert(position.node!=head);
00924    assert(position.node);
00925 
00926    sibling_iterator aargh=append_child(position, value_type());
00927    return replace(aargh, other);
00928    }
00929 
00930 template <class T, class tree_node_allocator>
00931 template <class iter>
00932 iter tree<T, tree_node_allocator>::prepend_child(iter position, iter other)
00933    {
00934    assert(position.node!=head);
00935    assert(position.node);
00936 
00937    sibling_iterator aargh=prepend_child(position, value_type());
00938    return replace(aargh, other);
00939    }
00940 
00941 template <class T, class tree_node_allocator>
00942 template <class iter>
00943 iter tree<T, tree_node_allocator>::append_children(iter position, sibling_iterator from, sibling_iterator to)
00944    {
00945    assert(position.node!=head);
00946    assert(position.node);
00947 
00948    iter ret=from;
00949 
00950    while(from!=to) {
00951       insert_subtree(position.end(), from);
00952       ++from;
00953       }
00954    return ret;
00955    }
00956 
00957 template <class T, class tree_node_allocator>
00958 template <class iter>
00959 iter tree<T, tree_node_allocator>::prepend_children(iter position, sibling_iterator from, sibling_iterator to)
00960    {
00961    assert(position.node!=head);
00962    assert(position.node);
00963 
00964    iter ret=from;
00965 
00966    while(from!=to) {
00967       insert_subtree(position.begin(), from);
00968       ++from;
00969       }
00970    return ret;
00971    }
00972 
00973 template <class T, class tree_node_allocator>
00974 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::set_head(const T& x)
00975    {
00976    assert(head->next_sibling==feet);
00977    return insert(iterator(feet), x);
00978    }
00979 
00980 template <class T, class tree_node_allocator>
00981 template <class iter>
00982 iter tree<T, tree_node_allocator>::insert(iter position, const T& x)
00983    {
00984    if(position.node==0) {
00985       position.node=feet; // Backward compatibility: when calling insert on a null node,
00986                           // insert before the feet.
00987       }
00988    tree_node* tmp = alloc_.allocate(1,0);
00989    kp::constructor(&tmp->data, x);
00990    tmp->first_child=0;
00991    tmp->last_child=0;
00992 
00993    tmp->parent=position.node->parent;
00994    tmp->next_sibling=position.node;
00995    tmp->prev_sibling=position.node->prev_sibling;
00996    position.node->prev_sibling=tmp;
00997 
00998    if(tmp->prev_sibling==0) {
00999       if(tmp->parent) // when inserting nodes at the head, there is no parent
01000          tmp->parent->first_child=tmp;
01001       }
01002    else
01003       tmp->prev_sibling->next_sibling=tmp;
01004    return tmp;
01005    }
01006 
01007 template <class T, class tree_node_allocator>
01008 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::insert(sibling_iterator position, const T& x)
01009    {
01010    tree_node* tmp = alloc_.allocate(1,0);
01011    kp::constructor(&tmp->data, x);
01012    tmp->first_child=0;
01013    tmp->last_child=0;
01014 
01015    tmp->next_sibling=position.node;
01016    if(position.node==0) { // iterator points to end of a subtree
01017       tmp->parent=position.parent_;
01018       tmp->prev_sibling=position.range_last();
01019       tmp->parent->last_child=tmp;
01020       }
01021    else {
01022       tmp->parent=position.node->parent;
01023       tmp->prev_sibling=position.node->prev_sibling;
01024       position.node->prev_sibling=tmp;
01025       }
01026 
01027    if(tmp->prev_sibling==0) {
01028       if(tmp->parent) // when inserting nodes at the head, there is no parent
01029          tmp->parent->first_child=tmp;
01030       }
01031    else
01032       tmp->prev_sibling->next_sibling=tmp;
01033    return tmp;
01034    }
01035 
01036 template <class T, class tree_node_allocator>
01037 template <class iter>
01038 iter tree<T, tree_node_allocator>::insert_after(iter position, const T& x)
01039    {
01040    tree_node* tmp = alloc_.allocate(1,0);
01041    kp::constructor(&tmp->data, x);
01042    tmp->first_child=0;
01043    tmp->last_child=0;
01044 
01045    tmp->parent=position.node->parent;
01046    tmp->prev_sibling=position.node;
01047    tmp->next_sibling=position.node->next_sibling;
01048    position.node->next_sibling=tmp;
01049 
01050    if(tmp->next_sibling==0) {
01051       if(tmp->parent) // when inserting nodes at the head, there is no parent
01052          tmp->parent->last_child=tmp;
01053       }
01054    else {
01055       tmp->next_sibling->prev_sibling=tmp;
01056       }
01057    return tmp;
01058    }
01059 
01060 template <class T, class tree_node_allocator>
01061 template <class iter>
01062 iter tree<T, tree_node_allocator>::insert_subtree(iter position, const iterator_base& subtree)
01063    {
01064    // insert dummy
01065    iter it=insert(position, value_type());
01066    // replace dummy with subtree
01067    return replace(it, subtree);
01068    }
01069 
01070 template <class T, class tree_node_allocator>
01071 template <class iter>
01072 iter tree<T, tree_node_allocator>::insert_subtree_after(iter position, const iterator_base& subtree)
01073    {
01074    // insert dummy
01075    iter it=insert_after(position, value_type());
01076    // replace dummy with subtree
01077    return replace(it, subtree);
01078    }
01079 
01080 // template <class T, class tree_node_allocator>
01081 // template <class iter>
01082 // iter tree<T, tree_node_allocator>::insert_subtree(sibling_iterator position, iter subtree)
01083 //    {
01084 //    // insert dummy
01085 //    iter it(insert(position, value_type()));
01086 //    // replace dummy with subtree
01087 //    return replace(it, subtree);
01088 //    }
01089 
01090 template <class T, class tree_node_allocator>
01091 template <class iter>
01092 iter tree<T, tree_node_allocator>::replace(iter position, const T& x)
01093    {
01094    kp::destructor(&position.node->data);
01095    kp::constructor(&position.node->data, x);
01096    return position;
01097    }
01098 
01099 template <class T, class tree_node_allocator>
01100 template <class iter>
01101 iter tree<T, tree_node_allocator>::replace(iter position, const iterator_base& from)
01102    {
01103    assert(position.node!=head);
01104    tree_node *current_from=from.node;
01105    tree_node *start_from=from.node;
01106    tree_node *current_to  =position.node;
01107 
01108    // replace the node at position with head of the replacement tree at from
01109 // std::cout << "warning!" << position.node << std::endl;
01110    erase_children(position);  
01111 // std::cout << "no warning!" << std::endl;
01112    tree_node* tmp = alloc_.allocate(1,0);
01113    kp::constructor(&tmp->data, (*from));
01114    tmp->first_child=0;
01115    tmp->last_child=0;
01116    if(current_to->prev_sibling==0) {
01117       if(current_to->parent!=0)
01118          current_to->parent->first_child=tmp;
01119       }
01120    else {
01121       current_to->prev_sibling->next_sibling=tmp;
01122       }
01123    tmp->prev_sibling=current_to->prev_sibling;
01124    if(current_to->next_sibling==0) {
01125       if(current_to->parent!=0)
01126          current_to->parent->last_child=tmp;
01127       }
01128    else {
01129       current_to->next_sibling->prev_sibling=tmp;
01130       }
01131    tmp->next_sibling=current_to->next_sibling;
01132    tmp->parent=current_to->parent;
01133    kp::destructor(&current_to->data);
01134    alloc_.deallocate(current_to,1);
01135    current_to=tmp;
01136    
01137    // only at this stage can we fix 'last'
01138    tree_node *last=from.node->next_sibling;
01139 
01140    pre_order_iterator toit=tmp;
01141    // copy all children
01142    do {
01143       assert(current_from!=0);
01144       if(current_from->first_child != 0) {
01145          current_from=current_from->first_child;
01146          toit=append_child(toit, current_from->data);
01147          }
01148       else {
01149          while(current_from->next_sibling==0 && current_from!=start_from) {
01150             current_from=current_from->parent;
01151             toit=parent(toit);
01152             assert(current_from!=0);
01153             }
01154          current_from=current_from->next_sibling;
01155          if(current_from!=last) {
01156             toit=append_child(parent(toit), current_from->data);
01157             }
01158          }
01159       } while(current_from!=last);
01160 
01161    return current_to;
01162    }
01163 
01164 template <class T, class tree_node_allocator>
01165 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::replace(
01166    sibling_iterator orig_begin, 
01167    sibling_iterator orig_end, 
01168    sibling_iterator new_begin, 
01169    sibling_iterator new_end)
01170    {
01171    tree_node *orig_first=orig_begin.node;
01172    tree_node *new_first=new_begin.node;
01173    tree_node *orig_last=orig_first;
01174    while((++orig_begin)!=orig_end)
01175       orig_last=orig_last->next_sibling;
01176    tree_node *new_last=new_first;
01177    while((++new_begin)!=new_end)
01178       new_last=new_last->next_sibling;
01179 
01180    // insert all siblings in new_first..new_last before orig_first
01181    bool first=true;
01182    pre_order_iterator ret;
01183    while(1==1) {
01184       pre_order_iterator tt=insert_subtree(pre_order_iterator(orig_first), pre_order_iterator(new_first));
01185       if(first) {
01186          ret=tt;
01187          first=false;
01188          }
01189       if(new_first==new_last)
01190          break;
01191       new_first=new_first->next_sibling;
01192       }
01193 
01194    // erase old range of siblings
01195    bool last=false;
01196    tree_node *next=orig_first;
01197    while(1==1) {
01198       if(next==orig_last) 
01199          last=true;
01200       next=next->next_sibling;
01201       erase((pre_order_iterator)orig_first);
01202       if(last) 
01203          break;
01204       orig_first=next;
01205       }
01206    return ret;
01207    }
01208 
01209 template <class T, class tree_node_allocator>
01210 template <typename iter>
01211 iter tree<T, tree_node_allocator>::flatten(iter position)
01212    {
01213    if(position.node->first_child==0)
01214       return position;
01215 
01216    tree_node *tmp=position.node->first_child;
01217    while(tmp) {
01218       tmp->parent=position.node->parent;
01219       tmp=tmp->next_sibling;
01220       } 
01221    if(position.node->next_sibling) {
01222       position.node->last_child->next_sibling=position.node->next_sibling;
01223       position.node->next_sibling->prev_sibling=position.node->last_child;
01224       }
01225    else {
01226       position.node->parent->last_child=position.node->last_child;
01227       }
01228    position.node->next_sibling=position.node->first_child;
01229    position.node->next_sibling->prev_sibling=position.node;
01230    position.node->first_child=0;
01231    position.node->last_child=0;
01232 
01233    return position;
01234    }
01235 
01236 
01237 template <class T, class tree_node_allocator>
01238 template <typename iter>
01239 iter tree<T, tree_node_allocator>::reparent(iter position, sibling_iterator begin, sibling_iterator end)
01240    {
01241    tree_node *first=begin.node;
01242    tree_node *last=first;
01243 
01244    assert(first!=position.node);
01245    
01246    if(begin==end) return begin;
01247    // determine last node
01248    while((++begin)!=end) {
01249       last=last->next_sibling;
01250       }
01251    // move subtree
01252    if(first->prev_sibling==0) {
01253       first->parent->first_child=last->next_sibling;
01254       }
01255    else {
01256       first->prev_sibling->next_sibling=last->next_sibling;
01257       }
01258    if(last->next_sibling==0) {
01259       last->parent->last_child=first->prev_sibling;
01260       }
01261    else {
01262       last->next_sibling->prev_sibling=first->prev_sibling;
01263       }
01264    if(position.node->first_child==0) {
01265       position.node->first_child=first;
01266       position.node->last_child=last;
01267       first->prev_sibling=0;
01268       }
01269    else {
01270       position.node->last_child->next_sibling=first;
01271       first->prev_sibling=position.node->last_child;
01272       position.node->last_child=last;
01273       }
01274    last->next_sibling=0;
01275 
01276    tree_node *pos=first;
01277    while(1==1) {
01278       pos->parent=position.node;
01279       if(pos==last) break;
01280       pos=pos->next_sibling;
01281       }
01282 
01283    return first;
01284    }
01285 
01286 template <class T, class tree_node_allocator>
01287 template <typename iter> iter tree<T, tree_node_allocator>::reparent(iter position, iter from)
01288    {
01289    if(from.node->first_child==0) return position;
01290    return reparent(position, from.node->first_child, end(from));
01291    }
01292 
01293 template <class T, class tree_node_allocator>
01294 template <typename iter> iter tree<T, tree_node_allocator>::wrap(iter position, const T& x)
01295    {
01296    assert(position.node!=0);
01297    sibling_iterator fr=position, to=position;
01298    ++to;
01299    iter ret = insert(position, x);
01300    reparent(ret, fr, to);
01301    return ret;
01302    }
01303 
01304 template <class T, class tree_node_allocator>
01305 template <typename iter> iter tree<T, tree_node_allocator>::move_after(iter target, iter source)
01306    {
01307    tree_node *dst=target.node;
01308    tree_node *src=source.node;
01309    assert(dst);
01310    assert(src);
01311 
01312    if(dst==src) return source;
01313    if(dst->next_sibling)
01314       if(dst->next_sibling==src) // already in the right spot
01315          return source;
01316 
01317    // take src out of the tree
01318    if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
01319    else                     src->parent->first_child=src->next_sibling;
01320    if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
01321    else                     src->parent->last_child=src->prev_sibling;
01322 
01323    // connect it to the new point
01324    if(dst->next_sibling!=0) dst->next_sibling->prev_sibling=src;
01325    else                     dst->parent->last_child=src;
01326    src->next_sibling=dst->next_sibling;
01327    dst->next_sibling=src;
01328    src->prev_sibling=dst;
01329    src->parent=dst->parent;
01330    return src;
01331    }
01332 
01333 template <class T, class tree_node_allocator>
01334 template <typename iter> iter tree<T, tree_node_allocator>::move_before(iter target, iter source)
01335    {
01336    tree_node *dst=target.node;
01337    tree_node *src=source.node;
01338    assert(dst);
01339    assert(src);
01340 
01341    if(dst==src) return source;
01342    if(dst->prev_sibling)
01343       if(dst->prev_sibling==src) // already in the right spot
01344          return source;
01345 
01346    // take src out of the tree
01347    if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
01348    else                     src->parent->first_child=src->next_sibling;
01349    if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
01350    else                     src->parent->last_child=src->prev_sibling;
01351 
01352    // connect it to the new point
01353    if(dst->prev_sibling!=0) dst->prev_sibling->next_sibling=src;
01354    else                     dst->parent->first_child=src;
01355    src->prev_sibling=dst->prev_sibling;
01356    dst->prev_sibling=src;
01357    src->next_sibling=dst;
01358    src->parent=dst->parent;
01359    return src;
01360    }
01361 
01362 // specialisation for sibling_iterators
01363 template <class T, class tree_node_allocator>
01364 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::move_before(sibling_iterator target, 
01365                                                                                          sibling_iterator source)
01366    {
01367    tree_node *dst=target.node;
01368    tree_node *src=source.node;
01369    tree_node *dst_prev_sibling;
01370    if(dst==0) { // must then be an end iterator
01371       dst_prev_sibling=target.parent_->last_child;
01372       assert(dst_prev_sibling);
01373       }
01374    else dst_prev_sibling=dst->prev_sibling;
01375    assert(src);
01376 
01377    if(dst==src) return source;
01378    if(dst_prev_sibling)
01379       if(dst_prev_sibling==src) // already in the right spot
01380          return source;
01381 
01382    // take src out of the tree
01383    if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
01384    else                     src->parent->first_child=src->next_sibling;
01385    if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
01386    else                     src->parent->last_child=src->prev_sibling;
01387 
01388    // connect it to the new point
01389    if(dst_prev_sibling!=0) dst_prev_sibling->next_sibling=src;
01390    else                    target.parent_->first_child=src;
01391    src->prev_sibling=dst_prev_sibling;
01392    if(dst) {
01393       dst->prev_sibling=src;
01394       src->parent=dst->parent;
01395       }
01396    src->next_sibling=dst;
01397    return src;
01398    }
01399 
01400 template <class T, class tree_node_allocator>
01401 template <typename iter> iter tree<T, tree_node_allocator>::move_ontop(iter target, iter source)
01402    {
01403    tree_node *dst=target.node;
01404    tree_node *src=source.node;
01405    assert(dst);
01406    assert(src);
01407 
01408    if(dst==src) return source;
01409 
01410    // remember connection points
01411    tree_node *b_prev_sibling=dst->prev_sibling;
01412    tree_node *b_next_sibling=dst->next_sibling;
01413    tree_node *b_parent=dst->parent;
01414 
01415    // remove target
01416    erase(target);
01417 
01418    // take src out of the tree
01419    if(src->prev_sibling!=0) src->prev_sibling->next_sibling=src->next_sibling;
01420    else                     src->parent->first_child=src->next_sibling;
01421    if(src->next_sibling!=0) src->next_sibling->prev_sibling=src->prev_sibling;
01422    else                     src->parent->last_child=src->prev_sibling;
01423 
01424    // connect it to the new point
01425    if(b_prev_sibling!=0) b_prev_sibling->next_sibling=src;
01426    else                  b_parent->first_child=src;
01427    if(b_next_sibling!=0) b_next_sibling->prev_sibling=src;
01428    else                  b_parent->last_child=src;
01429    src->prev_sibling=b_prev_sibling;
01430    src->next_sibling=b_next_sibling;
01431    src->parent=b_parent;
01432    return src;
01433    }
01434 
01435 template <class T, class tree_node_allocator>
01436 void tree<T, tree_node_allocator>::merge(sibling_iterator to1,   sibling_iterator to2,
01437                                           sibling_iterator from1, sibling_iterator from2,
01438                                           bool duplicate_leaves)
01439    {
01440    sibling_iterator fnd;
01441    while(from1!=from2) {
01442       if((fnd=std::find(to1, to2, (*from1))) != to2) { // element found
01443          if(from1.begin()==from1.end()) { // full depth reached
01444             if(duplicate_leaves)
01445                append_child(parent(to1), (*from1));
01446             }
01447          else { // descend further
01448             merge(fnd.begin(), fnd.end(), from1.begin(), from1.end(), duplicate_leaves);
01449             }
01450          }
01451       else { // element missing
01452          insert_subtree(to2, from1);
01453          }
01454       ++from1;
01455       }
01456    }
01457 
01458 
01459 template <class T, class tree_node_allocator>
01460 void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to, bool deep)
01461    {
01462    std::less<T> comp;
01463    sort(from, to, comp, deep);
01464    }
01465 
01466 template <class T, class tree_node_allocator>
01467 template <class StrictWeakOrdering>
01468 void tree<T, tree_node_allocator>::sort(sibling_iterator from, sibling_iterator to, 
01469                                         StrictWeakOrdering comp, bool deep)
01470    {
01471    if(from==to) return;
01472    // make list of sorted nodes
01473    // CHECK: if multiset stores equivalent nodes in the order in which they
01474    // are inserted, then this routine should be called 'stable_sort'.
01475    std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> > nodes(comp);
01476    sibling_iterator it=from, it2=to;
01477    while(it != to) {
01478       nodes.insert(it.node);
01479       ++it;
01480       }
01481    // reassemble
01482    --it2;
01483 
01484    // prev and next are the nodes before and after the sorted range
01485    tree_node *prev=from.node->prev_sibling;
01486    tree_node *next=it2.node->next_sibling;
01487    typename std::multiset<tree_node *, compare_nodes<StrictWeakOrdering> >::iterator nit=nodes.begin(), eit=nodes.end();
01488    if(prev==0) {
01489       if((*nit)->parent!=0) // to catch "sorting the head" situations, when there is no parent
01490          (*nit)->parent->first_child=(*nit);
01491       }
01492    else prev->next_sibling=(*nit);
01493 
01494    --eit;
01495    while(nit!=eit) {
01496       (*nit)->prev_sibling=prev;
01497       if(prev)
01498          prev->next_sibling=(*nit);
01499       prev=(*nit);
01500       ++nit;
01501       }
01502    // prev now points to the last-but-one node in the sorted range
01503    if(prev)
01504       prev->next_sibling=(*eit);
01505 
01506    // eit points to the last node in the sorted range.
01507    (*eit)->next_sibling=next;
01508    (*eit)->prev_sibling=prev; // missed in the loop above
01509    if(next==0) {
01510       if((*eit)->parent!=0) // to catch "sorting the head" situations, when there is no parent
01511          (*eit)->parent->last_child=(*eit);
01512       }
01513    else next->prev_sibling=(*eit);
01514 
01515    if(deep) {  // sort the children of each node too
01516       sibling_iterator bcs(*nodes.begin());
01517       sibling_iterator ecs(*eit);
01518       ++ecs;
01519       while(bcs!=ecs) {
01520          sort(begin(bcs), end(bcs), comp, deep);
01521          ++bcs;
01522          }
01523       }
01524    }
01525 
01526 template <class T, class tree_node_allocator>
01527 template <typename iter>
01528 bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_) const
01529    {
01530    std::equal_to<T> comp;
01531    return equal(one_, two, three_, comp);
01532    }
01533 
01534 template <class T, class tree_node_allocator>
01535 template <typename iter>
01536 bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_) const
01537    {
01538    std::equal_to<T> comp;
01539    return equal_subtree(one_, two_, comp);
01540    }
01541 
01542 template <class T, class tree_node_allocator>
01543 template <typename iter, class BinaryPredicate>
01544 bool tree<T, tree_node_allocator>::equal(const iter& one_, const iter& two, const iter& three_, BinaryPredicate fun) const
01545    {
01546    pre_order_iterator one(one_), three(three_);
01547 
01548 // if(one==two && is_valid(three) && three.number_of_children()!=0)
01549 //    return false;
01550    while(one!=two && is_valid(three)) {
01551       if(!fun(*one,*three))
01552          return false;
01553       if(one.number_of_children()!=three.number_of_children()) 
01554          return false;
01555       ++one;
01556       ++three;
01557       }
01558    return true;
01559    }
01560 
01561 template <class T, class tree_node_allocator>
01562 template <typename iter, class BinaryPredicate>
01563 bool tree<T, tree_node_allocator>::equal_subtree(const iter& one_, const iter& two_, BinaryPredicate fun) const
01564    {
01565    pre_order_iterator one(one_), two(two_);
01566 
01567    if(!fun(*one,*two)) return false;
01568    if(number_of_children(one)!=number_of_children(two)) return false;
01569    return equal(begin(one),end(one),begin(two),fun);
01570    }
01571 
01572 template <class T, class tree_node_allocator>
01573 tree<T, tree_node_allocator> tree<T, tree_node_allocator>::subtree(sibling_iterator from, sibling_iterator to) const
01574    {
01575    tree tmp;
01576    tmp.set_head(value_type());
01577    tmp.replace(tmp.begin(), tmp.end(), from, to);
01578    return tmp;
01579    }
01580 
01581 template <class T, class tree_node_allocator>
01582 void tree<T, tree_node_allocator>::subtree(tree& tmp, sibling_iterator from, sibling_iterator to) const
01583    {
01584    tmp.set_head(value_type());
01585    tmp.replace(tmp.begin(), tmp.end(), from, to);
01586    }
01587 
01588 template <class T, class tree_node_allocator>
01589 int tree<T, tree_node_allocator>::size() const
01590    {
01591    int i=0;
01592    pre_order_iterator it=begin(), eit=end();
01593    while(it!=eit) {
01594       ++i;
01595       ++it;
01596       }
01597    return i;
01598    }
01599 
01600 template <class T, class tree_node_allocator>
01601 int tree<T, tree_node_allocator>::size(const iterator_base& top) const
01602    {
01603    int i=0;
01604    pre_order_iterator it=top, eit=top;
01605    eit.skip_children();
01606    ++eit;
01607    while(it!=eit) {
01608       ++i;
01609       ++it;
01610       }
01611    return i;
01612    }
01613 
01614 template <class T, class tree_node_allocator>
01615 bool tree<T, tree_node_allocator>::empty() const
01616    {
01617    pre_order_iterator it=begin(), eit=end();
01618    return (it==eit);
01619    }
01620 
01621 template <class T, class tree_node_allocator>
01622 int tree<T, tree_node_allocator>::depth(const iterator_base& it) const
01623    {
01624    tree_node* pos=it.node;
01625    assert(pos!=0);
01626    int ret=0;
01627    while(pos->parent!=0) {
01628       pos=pos->parent;
01629       ++ret;
01630       }
01631    return ret;
01632    }
01633 
01634 template <class T, class tree_node_allocator>
01635 unsigned int tree<T, tree_node_allocator>::number_of_children(const iterator_base& it) 
01636    {
01637    tree_node *pos=it.node->first_child;
01638    if(pos==0) return 0;
01639    
01640    unsigned int ret=1;
01641 //   while(pos!=it.node->last_child) {
01642 //      ++ret;
01643 //      pos=pos->next_sibling;
01644 //      }
01645    while((pos=pos->next_sibling))
01646       ++ret;
01647    return ret;
01648    }
01649 
01650 template <class T, class tree_node_allocator>
01651 unsigned int tree<T, tree_node_allocator>::number_of_siblings(const iterator_base& it) const
01652    {
01653    tree_node *pos=it.node;
01654    unsigned int ret=0;
01655    // count forward
01656    while(pos->next_sibling && 
01657          pos->next_sibling!=head &&
01658          pos->next_sibling!=feet) {
01659       ++ret;
01660       pos=pos->next_sibling;
01661       }
01662    // count backward
01663    pos=it.node;
01664    while(pos->prev_sibling && 
01665          pos->prev_sibling!=head &&
01666          pos->prev_sibling!=feet) {
01667       ++ret;
01668       pos=pos->prev_sibling;
01669       }
01670    
01671    return ret;
01672    }
01673 
01674 template <class T, class tree_node_allocator>
01675 void tree<T, tree_node_allocator>::swap(sibling_iterator it)
01676    {
01677    tree_node *nxt=it.node->next_sibling;
01678    if(nxt) {
01679       if(it.node->prev_sibling)
01680          it.node->prev_sibling->next_sibling=nxt;
01681       else
01682          it.node->parent->first_child=nxt;
01683       nxt->prev_sibling=it.node->prev_sibling;
01684       tree_node *nxtnxt=nxt->next_sibling;
01685       if(nxtnxt)
01686          nxtnxt->prev_sibling=it.node;
01687       else
01688          it.node->parent->last_child=it.node;
01689       nxt->next_sibling=it.node;
01690       it.node->prev_sibling=nxt;
01691       it.node->next_sibling=nxtnxt;
01692       }
01693    }
01694 
01695 template <class T, class tree_node_allocator>
01696 void tree<T, tree_node_allocator>::swap(iterator one, iterator two)
01697    {
01698    // if one and two are adjacent siblings, use the sibling swap
01699    if(one.node->next_sibling==two.node) swap(one);
01700    else if(two.node->next_sibling==one.node) swap(two);
01701    else {
01702       tree_node *nxt1=one.node->next_sibling;
01703       tree_node *nxt2=two.node->next_sibling;
01704       tree_node *pre1=one.node->prev_sibling;
01705       tree_node *pre2=two.node->prev_sibling;
01706       tree_node *par1=one.node->parent;
01707       tree_node *par2=two.node->parent;
01708 
01709       // reconnect
01710       one.node->parent=par2;
01711       one.node->next_sibling=nxt2;
01712       if(nxt2) nxt2->prev_sibling=one.node;
01713       else     par2->last_child=one.node;
01714       one.node->prev_sibling=pre2;
01715       if(pre2) pre2->next_sibling=one.node;
01716       else     par2->first_child=one.node;    
01717 
01718       two.node->parent=par1;
01719       two.node->next_sibling=nxt1;
01720       if(nxt1) nxt1->prev_sibling=two.node;
01721       else     par1->last_child=two.node;
01722       two.node->prev_sibling=pre1;
01723       if(pre1) pre1->next_sibling=two.node;
01724       else     par1->first_child=two.node;
01725       }
01726    }
01727 
01728 // template <class BinaryPredicate>
01729 // tree<T, tree_node_allocator>::iterator tree<T, tree_node_allocator>::find_subtree(
01730 //    sibling_iterator subfrom, sibling_iterator subto, iterator from, iterator to, 
01731 //    BinaryPredicate fun) const
01732 //    {
01733 //    assert(1==0); // this routine is not finished yet.
01734 //    while(from!=to) {
01735 //       if(fun(*subfrom, *from)) {
01736 //          
01737 //          }
01738 //       }
01739 //    return to;
01740 //    }
01741 
01742 template <class T, class tree_node_allocator>
01743 bool tree<T, tree_node_allocator>::is_in_subtree(const iterator_base& it, const iterator_base& begin, 
01744                                                  const iterator_base& end) const
01745    {
01746    // FIXME: this should be optimised.
01747    pre_order_iterator tmp=begin;
01748    while(tmp!=end) {
01749       if(tmp==it) return true;
01750       ++tmp;
01751       }
01752    return false;
01753    }
01754 
01755 template <class T, class tree_node_allocator>
01756 bool tree<T, tree_node_allocator>::is_valid(const iterator_base& it) const
01757    {
01758    if(it.node==0 || it.node==feet || it.node==head) return false;
01759    else return true;
01760    }
01761 
01762 template <class T, class tree_node_allocator>
01763 unsigned int tree<T, tree_node_allocator>::index(sibling_iterator it) const
01764    {
01765    unsigned int ind=0;
01766    if(it.node->parent==0) {
01767       while(it.node->prev_sibling!=head) {
01768          it.node=it.node->prev_sibling;
01769          ++ind;
01770          }
01771       }
01772    else {
01773       while(it.node->prev_sibling!=0) {
01774          it.node=it.node->prev_sibling;
01775          ++ind;
01776          }
01777       }
01778    return ind;
01779    }
01780 
01781 
01782 template <class T, class tree_node_allocator>
01783 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::child(const iterator_base& it, unsigned int num) const
01784    {
01785    tree_node *tmp=it.node->first_child;
01786    while(num--) {
01787       assert(tmp!=0);
01788       tmp=tmp->next_sibling;
01789       }
01790    return tmp;
01791    }
01792 
01793 
01794 
01795 
01796 // Iterator base
01797 
01798 template <class T, class tree_node_allocator>
01799 tree<T, tree_node_allocator>::iterator_base::iterator_base()
01800    : node(0), skip_current_children_(false)
01801    {
01802    }
01803 
01804 template <class T, class tree_node_allocator>
01805 tree<T, tree_node_allocator>::iterator_base::iterator_base(tree_node *tn)
01806    : node(tn), skip_current_children_(false)
01807    {
01808    }
01809 
01810 template <class T, class tree_node_allocator>
01811 T& tree<T, tree_node_allocator>::iterator_base::operator*() const
01812    {
01813    return node->data;
01814    }
01815 
01816 template <class T, class tree_node_allocator>
01817 T* tree<T, tree_node_allocator>::iterator_base::operator->() const
01818    {
01819    return &(node->data);
01820    }
01821 
01822 template <class T, class tree_node_allocator>
01823 bool tree<T, tree_node_allocator>::post_order_iterator::operator!=(const post_order_iterator& other) const
01824    {
01825    if(other.node!=this->node) return true;
01826    else return false;
01827    }
01828 
01829 template <class T, class tree_node_allocator>
01830 bool tree<T, tree_node_allocator>::post_order_iterator::operator==(const post_order_iterator& other) const
01831    {
01832    if(other.node==this->node) return true;
01833    else return false;
01834    }
01835 
01836 template <class T, class tree_node_allocator>
01837 bool tree<T, tree_node_allocator>::pre_order_iterator::operator!=(const pre_order_iterator& other) const
01838    {
01839    if(other.node!=this->node) return true;
01840    else return false;
01841    }
01842 
01843 template <class T, class tree_node_allocator>
01844 bool tree<T, tree_node_allocator>::pre_order_iterator::operator==(const pre_order_iterator& other) const
01845    {
01846    if(other.node==this->node) return true;
01847    else return false;
01848    }
01849 
01850 template <class T, class tree_node_allocator>
01851 bool tree<T, tree_node_allocator>::sibling_iterator::operator!=(const sibling_iterator& other) const
01852    {
01853    if(other.node!=this->node) return true;
01854    else return false;
01855    }
01856 
01857 template <class T, class tree_node_allocator>
01858 bool tree<T, tree_node_allocator>::sibling_iterator::operator==(const sibling_iterator& other) const
01859    {
01860    if(other.node==this->node) return true;
01861    else return false;
01862    }
01863 
01864 template <class T, class tree_node_allocator>
01865 bool tree<T, tree_node_allocator>::leaf_iterator::operator!=(const leaf_iterator& other) const
01866    {
01867    if(other.node!=this->node) return true;
01868    else return false;
01869    }
01870 
01871 template <class T, class tree_node_allocator>
01872 bool tree<T, tree_node_allocator>::leaf_iterator::operator==(const leaf_iterator& other) const
01873    {
01874    if(other.node==this->node) return true;
01875    else return false;
01876    }
01877 
01878 template <class T, class tree_node_allocator>
01879 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::begin() const
01880    {
01881    if(node->first_child==0) 
01882       return end();
01883 
01884    sibling_iterator ret(node->first_child);
01885    ret.parent_=this->node;
01886    return ret;
01887    }
01888 
01889 template <class T, class tree_node_allocator>
01890 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::iterator_base::end() const
01891    {
01892    sibling_iterator ret(0);
01893    ret.parent_=node;
01894    return ret;
01895    }
01896 
01897 template <class T, class tree_node_allocator>
01898 void tree<T, tree_node_allocator>::iterator_base::skip_children()
01899    {
01900    skip_current_children_=true;
01901    }
01902 
01903 template <class T, class tree_node_allocator>
01904 unsigned int tree<T, tree_node_allocator>::iterator_base::number_of_children() const
01905    {
01906    tree_node *pos=node->first_child;
01907    if(pos==0) return 0;
01908    
01909    unsigned int ret=1;
01910    while(pos!=node->last_child) {
01911       ++ret;
01912       pos=pos->next_sibling;
01913       }
01914    return ret;
01915    }
01916 
01917 
01918 
01919 // Pre-order iterator
01920 
01921 template <class T, class tree_node_allocator>
01922 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator() 
01923    : iterator_base(0)
01924    {
01925    }
01926 
01927 template <class T, class tree_node_allocator>
01928 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(tree_node *tn)
01929    : iterator_base(tn)
01930    {
01931    }
01932 
01933 template <class T, class tree_node_allocator>
01934 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const iterator_base &other)
01935    : iterator_base(other.node)
01936    {
01937    }
01938 
01939 template <class T, class tree_node_allocator>
01940 tree<T, tree_node_allocator>::pre_order_iterator::pre_order_iterator(const sibling_iterator& other)
01941    : iterator_base(other.node)
01942    {
01943    if(this->node==0) {
01944       if(other.range_last()!=0)
01945          this->node=other.range_last();
01946       else 
01947          this->node=other.parent_;
01948       this->skip_children();
01949       ++(*this);
01950       }
01951    }
01952 
01953 template <class T, class tree_node_allocator>
01954 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator++()
01955    {
01956    assert(this->node!=0);
01957    if(!this->skip_current_children_ && this->node->first_child != 0) {
01958       this->node=this->node->first_child;
01959       }
01960    else {
01961       this->skip_current_children_=false;
01962       while(this->node->next_sibling==0) {
01963          this->node=this->node->parent;
01964          if(this->node==0)
01965             return *this;
01966          }
01967       this->node=this->node->next_sibling;
01968       }
01969    return *this;
01970    }
01971 
01972 template <class T, class tree_node_allocator>
01973 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator--()
01974    {
01975    assert(this->node!=0);
01976    if(this->node->prev_sibling) {
01977       this->node=this->node->prev_sibling;
01978       while(this->node->last_child)
01979          this->node=this->node->last_child;
01980       }
01981    else {
01982       this->node=this->node->parent;
01983       if(this->node==0)
01984          return *this;
01985       }
01986    return *this;
01987 }
01988 
01989 template <class T, class tree_node_allocator>
01990 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator++(int n)
01991    {
01992    pre_order_iterator copy = *this;
01993    ++(*this);
01994    return copy;
01995    }
01996 
01997 template <class T, class tree_node_allocator>
01998 typename tree<T, tree_node_allocator>::pre_order_iterator tree<T, tree_node_allocator>::pre_order_iterator::operator--(int n)
01999 {
02000   pre_order_iterator copy = *this;
02001   --(*this);
02002   return copy;
02003 }
02004 
02005 template <class T, class tree_node_allocator>
02006 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator+=(unsigned int num)
02007    {
02008    while(num>0) {
02009       ++(*this);
02010       --num;
02011       }
02012    return (*this);
02013    }
02014 
02015 template <class T, class tree_node_allocator>
02016 typename tree<T, tree_node_allocator>::pre_order_iterator& tree<T, tree_node_allocator>::pre_order_iterator::operator-=(unsigned int num)
02017    {
02018    while(num>0) {
02019       --(*this);
02020       --num;
02021       }
02022    return (*this);
02023    }
02024 
02025 
02026 
02027 // Post-order iterator
02028 
02029 template <class T, class tree_node_allocator>
02030 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator() 
02031    : iterator_base(0)
02032    {
02033    }
02034 
02035 template <class T, class tree_node_allocator>
02036 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(tree_node *tn)
02037    : iterator_base(tn)
02038    {
02039    }
02040 
02041 template <class T, class tree_node_allocator>
02042 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const iterator_base &other)
02043    : iterator_base(other.node)
02044    {
02045    }
02046 
02047 template <class T, class tree_node_allocator>
02048 tree<T, tree_node_allocator>::post_order_iterator::post_order_iterator(const sibling_iterator& other)
02049    : iterator_base(other.node)
02050    {
02051    if(this->node==0) {
02052       if(other.range_last()!=0)
02053          this->node=other.range_last();
02054       else 
02055          this->node=other.parent_;
02056       this->skip_children();
02057       ++(*this);
02058       }
02059    }
02060 
02061 template <class T, class tree_node_allocator>
02062 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator++()
02063    {
02064    assert(this->node!=0);
02065    if(this->node->next_sibling==0) {
02066       this->node=this->node->parent;
02067       this->skip_current_children_=false;
02068       }
02069    else {
02070       this->node=this->node->next_sibling;
02071       if(this->skip_current_children_) {
02072          this->skip_current_children_=false;
02073          }
02074       else {
02075          while(this->node->first_child)
02076             this->node=this->node->first_child;
02077          }
02078       }
02079    return *this;
02080    }
02081 
02082 template <class T, class tree_node_allocator>
02083 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator--()
02084    {
02085    assert(this->node!=0);
02086    if(this->skip_current_children_ || this->node->last_child==0) {
02087       this->skip_current_children_=false;
02088       while(this->node->prev_sibling==0)
02089          this->node=this->node->parent;
02090       this->node=this->node->prev_sibling;
02091       }
02092    else {
02093       this->node=this->node->last_child;
02094       }
02095    return *this;
02096    }
02097 
02098 template <class T, class tree_node_allocator>
02099 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator++(int)
02100    {
02101    post_order_iterator copy = *this;
02102    ++(*this);
02103    return copy;
02104    }
02105 
02106 template <class T, class tree_node_allocator>
02107 typename tree<T, tree_node_allocator>::post_order_iterator tree<T, tree_node_allocator>::post_order_iterator::operator--(int)
02108    {
02109    post_order_iterator copy = *this;
02110    --(*this);
02111    return copy;
02112    }
02113 
02114 
02115 template <class T, class tree_node_allocator>
02116 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator+=(unsigned int num)
02117    {
02118    while(num>0) {
02119       ++(*this);
02120       --num;
02121       }
02122    return (*this);
02123    }
02124 
02125 template <class T, class tree_node_allocator>
02126 typename tree<T, tree_node_allocator>::post_order_iterator& tree<T, tree_node_allocator>::post_order_iterator::operator-=(unsigned int num)
02127    {
02128    while(num>0) {
02129       --(*this);
02130       --num;
02131       }
02132    return (*this);
02133    }
02134 
02135 template <class T, class tree_node_allocator>
02136 void tree<T, tree_node_allocator>::post_order_iterator::descend_all()
02137    {
02138    assert(this->node!=0);
02139    while(this->node->first_child)
02140       this->node=this->node->first_child;
02141    }
02142 
02143 
02144 // Breadth-first iterator
02145 
02146 template <class T, class tree_node_allocator>
02147 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator()
02148    : iterator_base()
02149    {
02150    }
02151 
02152 template <class T, class tree_node_allocator>
02153 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(tree_node *tn)
02154    : iterator_base(tn)
02155    {
02156    traversal_queue.push(tn);
02157    }
02158 
02159 template <class T, class tree_node_allocator>
02160 tree<T, tree_node_allocator>::breadth_first_queued_iterator::breadth_first_queued_iterator(const iterator_base& other)
02161    : iterator_base(other.node)
02162    {
02163    traversal_queue.push(other.node);
02164    }
02165 
02166 template <class T, class tree_node_allocator>
02167 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator!=(const breadth_first_queued_iterator& other) const
02168    {
02169    if(other.node!=this->node) return true;
02170    else return false;
02171    }
02172 
02173 template <class T, class tree_node_allocator>
02174 bool tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator==(const breadth_first_queued_iterator& other) const
02175    {
02176    if(other.node==this->node) return true;
02177    else return false;
02178    }
02179 
02180 template <class T, class tree_node_allocator>
02181 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++()
02182    {
02183    assert(this->node!=0);
02184 
02185    // Add child nodes and pop current node
02186    sibling_iterator sib=this->begin();
02187    while(sib!=this->end()) {
02188       traversal_queue.push(sib.node);
02189       ++sib;
02190       }
02191    traversal_queue.pop();
02192    if(traversal_queue.size()>0)
02193       this->node=traversal_queue.front();
02194    else 
02195       this->node=0;
02196    return (*this);
02197    }
02198 
02199 template <class T, class tree_node_allocator>
02200 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator++(int n)
02201    {
02202    breadth_first_queued_iterator copy = *this;
02203    ++(*this);
02204    return copy;
02205    }
02206 
02207 template <class T, class tree_node_allocator>
02208 typename tree<T, tree_node_allocator>::breadth_first_queued_iterator& tree<T, tree_node_allocator>::breadth_first_queued_iterator::operator+=(unsigned int num)
02209    {
02210    while(num>0) {
02211       ++(*this);
02212       --num;
02213       }
02214    return (*this);
02215    }
02216 
02217 
02218 
02219 // Fixed depth iterator
02220 
02221 template <class T, class tree_node_allocator>
02222 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator()
02223    : iterator_base()
02224    {
02225    set_first_parent_();
02226    }
02227 
02228 template <class T, class tree_node_allocator>
02229 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(tree_node *tn)
02230    : iterator_base(tn)
02231    {
02232    set_first_parent_();
02233    }
02234 
02235 template <class T, class tree_node_allocator>
02236 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const iterator_base& other)
02237    : iterator_base(other.node)
02238    {
02239    set_first_parent_();
02240    }
02241 
02242 template <class T, class tree_node_allocator>
02243 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const sibling_iterator& other)
02244    : iterator_base(other.node), first_parent_(other.parent_)
02245    {
02246    find_leftmost_parent_();
02247    }
02248 
02249 template <class T, class tree_node_allocator>
02250 tree<T, tree_node_allocator>::fixed_depth_iterator::fixed_depth_iterator(const fixed_depth_iterator& other)
02251    : iterator_base(other.node), first_parent_(other.first_parent_)
02252    {
02253    }
02254 
02255 template <class T, class tree_node_allocator>
02256 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator==(const fixed_depth_iterator& other) const
02257    {
02258    if(other.node==this->node && other.first_parent_==first_parent_) return true;
02259    else return false;
02260    }
02261 
02262 template <class T, class tree_node_allocator>
02263 bool tree<T, tree_node_allocator>::fixed_depth_iterator::operator!=(const fixed_depth_iterator& other) const
02264    {
02265    if(other.node!=this->node || other.first_parent_!=first_parent_) return true;
02266    else return false;
02267    }
02268 
02269 template <class T, class tree_node_allocator>
02270 void tree<T, tree_node_allocator>::fixed_depth_iterator::set_first_parent_()
02271    {
02272    return; // FIXME: we do not use first_parent_ yet, and it actually needs some serious reworking if
02273            // it is ever to work at the 'head' level.
02274    first_parent_=0;
02275    if(this->node==0) return;
02276    if(this->node->parent!=0)
02277       first_parent_=this->node->parent;
02278    if(first_parent_)
02279       find_leftmost_parent_();
02280    }
02281 
02282 template <class T, class tree_node_allocator>
02283 void tree<T, tree_node_allocator>::fixed_depth_iterator::find_leftmost_parent_()
02284    {
02285    return; // FIXME: see 'set_first_parent()'
02286    tree_node *tmppar=first_parent_;
02287    while(tmppar->prev_sibling) {
02288       tmppar=tmppar->prev_sibling;
02289       if(tmppar->first_child)
02290          first_parent_=tmppar;
02291       }
02292    }
02293 
02294 template <class T, class tree_node_allocator>
02295 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator++()
02296    {
02297    assert(this->node!=0);
02298 
02299    if(this->node->next_sibling) {
02300       this->node=this->node->next_sibling;
02301       }
02302    else { 
02303       int relative_depth=0;
02304       upper:
02305       do {
02306          this->node=this->node->parent;
02307          if(this->node==0) return *this;
02308          --relative_depth;
02309          } while(this->node->next_sibling==0);
02310       lower:
02311       this->node=this->node->next_sibling;
02312       while(this->node->first_child==0) {
02313          if(this->node->next_sibling==0)
02314             goto upper;
02315          this->node=this->node->next_sibling;
02316          if(this->node==0) return *this;
02317          }
02318       while(relative_depth<0 && this->node->first_child!=0) {
02319          this->node=this->node->first_child;
02320          ++relative_depth;
02321          }
02322       if(relative_depth<0) {
02323          if(this->node->next_sibling==0) goto upper;
02324          else                          goto lower;
02325          }
02326       }
02327    return *this;
02328 
02329 // if(this->node->next_sibling!=0) {
02330 //    this->node=this->node->next_sibling;
02331 //    assert(this->node!=0);
02332 //    if(this->node->parent==0 && this->node->next_sibling==0) // feet element
02333 //       this->node=0;
02334 //    }
02335 // else {
02336 //    tree_node *par=this->node->parent;
02337 //    do {
02338 //       par=par->next_sibling;
02339 //       if(par==0) { // FIXME: need to keep track of this!
02340 //          this->node=0;
02341 //          return *this;
02342 //          }
02343 //       } while(par->first_child==0);
02344 //    this->node=par->first_child;
02345 //    }
02346    return *this;
02347    }
02348 
02349 template <class T, class tree_node_allocator>
02350 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator--()
02351    {
02352    assert(this->node!=0);
02353    if(this->node->prev_sibling!=0) {
02354       this->node=this->node->prev_sibling;
02355       assert(this->node!=0);
02356       if(this->node->parent==0 && this->node->prev_sibling==0) // head element
02357          this->node=0;
02358       }
02359    else {
02360       tree_node *par=this->node->parent;
02361       do {
02362          par=par->prev_sibling;
02363          if(par==0) { // FIXME: need to keep track of this!
02364             this->node=0;
02365             return *this;
02366             }
02367          } while(par->last_child==0);
02368       this->node=par->last_child;
02369       }
02370    return *this;
02371 }
02372 
02373 template <class T, class tree_node_allocator>
02374 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator++(int)
02375    {
02376    fixed_depth_iterator copy = *this;
02377    ++(*this);
02378    return copy;
02379    }
02380 
02381 template <class T, class tree_node_allocator>
02382 typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_allocator>::fixed_depth_iterator::operator--(int)
02383 {
02384   fixed_depth_iterator copy = *this;
02385   --(*this);
02386   return copy;
02387 }
02388 
02389 template <class T, class tree_node_allocator>
02390 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator-=(unsigned int num)
02391    {
02392    while(num>0) {
02393       --(*this);
02394       --(num);
02395       }
02396    return (*this);
02397    }
02398 
02399 template <class T, class tree_node_allocator>
02400 typename tree<T, tree_node_allocator>::fixed_depth_iterator& tree<T, tree_node_allocator>::fixed_depth_iterator::operator+=(unsigned int num)
02401    {
02402    while(num>0) {
02403       ++(*this);
02404       --(num);
02405       }
02406    return *this;
02407    }
02408 
02409 // FIXME: add the other members of fixed_depth_iterator.
02410 
02411 
02412 // Sibling iterator
02413 
02414 template <class T, class tree_node_allocator>
02415 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator() 
02416    : iterator_base()
02417    {
02418    set_parent_();
02419    }
02420 
02421 template <class T, class tree_node_allocator>
02422 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(tree_node *tn)
02423    : iterator_base(tn)
02424    {
02425    set_parent_();
02426    }
02427 
02428 template <class T, class tree_node_allocator>
02429 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const iterator_base& other)
02430    : iterator_base(other.node)
02431    {
02432    set_parent_();
02433    }
02434 
02435 template <class T, class tree_node_allocator>
02436 tree<T, tree_node_allocator>::sibling_iterator::sibling_iterator(const sibling_iterator& other)
02437    : iterator_base(other), parent_(other.parent_)
02438    {
02439    }
02440 
02441 template <class T, class tree_node_allocator>
02442 void tree<T, tree_node_allocator>::sibling_iterator::set_parent_()
02443    {
02444    parent_=0;
02445    if(this->node==0) return;
02446    if(this->node->parent!=0)
02447       parent_=this->node->parent;
02448    }
02449 
02450 template <class T, class tree_node_allocator>
02451 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator++()
02452    {
02453    if(this->node)
02454       this->node=this->node->next_sibling;
02455    return *this;
02456    }
02457 
02458 template <class T, class tree_node_allocator>
02459 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator--()
02460    {
02461    if(this->node) this->node=this->node->prev_sibling;
02462    else {
02463       assert(parent_);
02464       this->node=parent_->last_child;
02465       }
02466    return *this;
02467 }
02468 
02469 template <class T, class tree_node_allocator>
02470 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator++(int)
02471    {
02472    sibling_iterator copy = *this;
02473    ++(*this);
02474    return copy;
02475    }
02476 
02477 template <class T, class tree_node_allocator>
02478 typename tree<T, tree_node_allocator>::sibling_iterator tree<T, tree_node_allocator>::sibling_iterator::operator--(int)
02479    {
02480    sibling_iterator copy = *this;
02481    --(*this);
02482    return copy;
02483    }
02484 
02485 template <class T, class tree_node_allocator>
02486 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator+=(unsigned int num)
02487    {
02488    while(num>0) {
02489       ++(*this);
02490       --num;
02491       }
02492    return (*this);
02493    }
02494 
02495 template <class T, class tree_node_allocator>
02496 typename tree<T, tree_node_allocator>::sibling_iterator& tree<T, tree_node_allocator>::sibling_iterator::operator-=(unsigned int num)
02497    {
02498    while(num>0) {
02499       --(*this);
02500       --num;
02501       }
02502    return (*this);
02503    }
02504 
02505 template <class T, class tree_node_allocator>
02506 typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_first() const
02507    {
02508    tree_node *tmp=parent_->first_child;
02509    return tmp;
02510    }
02511 
02512 template <class T, class tree_node_allocator>
02513 typename tree<T, tree_node_allocator>::tree_node *tree<T, tree_node_allocator>::sibling_iterator::range_last() const
02514    {
02515    return parent_->last_child;
02516    }
02517 
02518 // Leaf iterator
02519 
02520 template <class T, class tree_node_allocator>
02521 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator() 
02522    : iterator_base(0)
02523    {
02524    }
02525 
02526 template <class T, class tree_node_allocator>
02527 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(tree_node *tn)
02528    : iterator_base(tn)
02529    {
02530    }
02531 
02532 template <class T, class tree_node_allocator>
02533 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const iterator_base &other)
02534    : iterator_base(other.node)
02535    {
02536    }
02537 
02538 template <class T, class tree_node_allocator>
02539 tree<T, tree_node_allocator>::leaf_iterator::leaf_iterator(const sibling_iterator& other)
02540    : iterator_base(other.node)
02541    {
02542    if(this->node==0) {
02543       if(other.range_last()!=0)
02544          this->node=other.range_last();
02545       else 
02546          this->node=other.parent_;
02547       ++(*this);
02548       }
02549    }
02550 
02551 template <class T, class tree_node_allocator>
02552 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator++()
02553    {
02554    assert(this->node!=0);
02555    while(this->node->next_sibling==0) {
02556       if (this->node->parent==0) return *this;
02557       this->node=this->node->parent;
02558       }
02559    this->node=this->node->next_sibling;
02560    while(this->node->first_child)
02561       this->node=this->node->first_child;
02562    return *this;
02563    }
02564 
02565 template <class T, class tree_node_allocator>
02566 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator--()
02567    {
02568    assert(this->node!=0);
02569    while (this->node->prev_sibling==0) {
02570       if (this->node->parent==0) return *this;
02571       this->node=this->node->parent;
02572       }
02573    this->node=this->node->prev_sibling;
02574    while(this->node->last_child)
02575       this->node=this->node->last_child;
02576    return *this;
02577    }
02578 
02579 template <class T, class tree_node_allocator>
02580 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator++(int)
02581    {
02582    leaf_iterator copy = *this;
02583    ++(*this);
02584    return copy;
02585    }
02586 
02587 template <class T, class tree_node_allocator>
02588 typename tree<T, tree_node_allocator>::leaf_iterator tree<T, tree_node_allocator>::leaf_iterator::operator--(int)
02589    {
02590    leaf_iterator copy = *this;
02591    --(*this);
02592    return copy;
02593    }
02594 
02595 
02596 template <class T, class tree_node_allocator>
02597 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator+=(unsigned int num)
02598    {
02599    while(num>0) {
02600       ++(*this);
02601       --num;
02602       }
02603    return (*this);
02604    }
02605 
02606 template <class T, class tree_node_allocator>
02607 typename tree<T, tree_node_allocator>::leaf_iterator& tree<T, tree_node_allocator>::leaf_iterator::operator-=(unsigned int num)
02608    {
02609    while(num>0) {
02610       --(*this);
02611       --num;
02612       }
02613    return (*this);
02614    }
02615 
02616 #endif
02617 
02618 // Local variables:
02619 // default-tab-width: 3
02620 // End:

Generated on Wed Feb 27 20:31:06 2008 for php.ast.svn.src. by  doxygen 1.5.3