一个带先根遍历iterator的模版树

来源:互联网 发布:mac 10.11 安装xcode 编辑:程序博客网 时间:2024/06/09 20:07

我自己写了一个模版树,用的是先根遍历的iterator, 下面是测试代码和模版树的源代码

暂时先这么多,如果可能的话,希望能把 遍历方式作为 policy 参数放入 模版树

开发环境 vc6 sp6  window xp

//测试函数
void tree_test()
{
 tree<int> ltree;
 tree<int>::iterator lp,ll,lr;
 
 ltree.AddLeftChild(ltree.begin(), 1);
 lp = ltree.AddLeftChild(ltree.begin(), 2);
 ll = ltree.AddLeftChild(lp, 3);
 ll = ltree.AddLeftChild(ll, 4);
 ltree.AddLeftChild(ll, 5);
 lr = ltree.AddRightChild(ll, 6);
 ltree.AddRightChild(lr,7);
 lr = ltree.AddRightChild(lp, 8);
 ll = ltree.AddLeftChild(lr, 9);
 lr = ltree.AddRightChild(ll,10);
 ltree.AddLeftChild(lr, 11);
 ltree.AddRightChild(lr, 12);
 ltree.AddRightChild(ltree.begin(),13);

 for (lp = ltree.begin(); lp != ltree.end(); lp++) {
  cout << *lp << endl;
 }
 cout << "the tree size is " << ltree.size() << endl;

 ltree.clear();

 cout << "now the tree size is " << ltree.size() << " after clear() be called"  << endl;

}

//模版树代码

#if !defined(EXT_STD_TREE_2ED68683_54C0_4aa6_B1BC_0C445F68DE4E_INCLUDED_)
#define EXT_STD_TREE_2ED68683_54C0_4aa6_B1BC_0C445F68DE4E_INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif //_MSC_VER > 1000


#include <cstddef>
#include <functional>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <xutility>


namespace ext_std
{
 template <typename _type, typename _alloc = allocator<_type> >
 class tree {
  protected:
   struct _Node;
   friend struct _Node;
   typedef _POINTER_X(_Node,_alloc) _Nodeptr;
   struct _Node {
    _Nodeptr _LeftChild, _RightChild, _Parent;
    _type _Value;
    bool is_leaf() {
     return (_LeftChild == this && _RightChild == this);
    }
    bool is_left_child() {
     return (_Parent->_LeftChild == this);
    }
    bool is_right_child() {
     return (_Parent->_RightChild == this);
    }
    bool have_parent() {
     return (_Parent != this);
    }
    bool have_left_child() {
     return (_LeftChild != this);
    }
    bool have_right_child() {
     return (_RightChild != this);
    }
    bool have_children() {
     return have_left_child() || have_right_child();
    }
   };
   struct _Acc;
   friend struct _Acc;
   struct _Acc {
    typedef _REFERENCE_X(_Nodeptr, _alloc) _Nodepref;
    typedef _alloc::reference _Vref;
    static _Nodepref _LeftChild(_Nodeptr _P) {
     return ((_Nodepref)(*_P)._LeftChild);
    }
    static _Nodepref _RightChild(_Nodeptr _P) {
     return ((_Nodepref)(*_P)._RightChild);
    }
    static _Nodepref _Parent(_Nodeptr _P) {
     return ((_Nodepref)(*_P)._Parent);
    }
    static _Vref _Value(_Nodeptr _P) {
     return ((_Vref)(*_P)._Value);
    }
   };

  public:
  typedef tree< _type, _alloc> _MyType;
  typedef _alloc allocator_type;
  typedef _alloc::size_type size_type;
  typedef _alloc::difference_type difference_type;
  typedef _alloc::pointer _Tptr;
  typedef _alloc::const_pointer _Ctptr;
  typedef _alloc::reference reference;
  typedef _alloc::const_reference const_reference;
  typedef _alloc::value_type value_type;

  
  class iterator;
  class const_iterator;
  friend class const_iterator;

  class const_iterator : public std::_Bidit<_type, difference_type> {
  public:
   const_iterator(){}
   const_iterator(_Nodeptr _P) : _mPtr(_P){}
   const_iterator(const iterator& other) : _mPtr(other._mPtr){}
   const_reference operator*() const {
    return _Acc::_Value(_mPtr);
   }
   _Ctptr operator->() const {
    return (&**this);
   }

   _Nodeptr get_next(_Nodeptr _node) {
    if (_node->is_left_child()) {
     if (_Acc::_Parent(_node)->have_right_child()) {
      return _Acc::_Parent(_node)->_RightChild;
     }else {
      if (_Acc::_Parent(_node)->have_parent()) {
       return get_next(_Acc::_Parent(_node));
      }else {
       return _Acc::_RightChild(_Acc::_Parent(_node));
      }
     }
    }else {
     return get_next(_Acc::_Parent(_node));
    }
   }

   const_iterator& leftchild() {
    _mPtr = _Acc::_LeftChild(_mPtr);
    return (*this);
   }

   const_iterator& rightchild() {
    _mPtr = _Acc::_RightChild(_mPtr);
    return (*this);
   }

   const_iterator& parent() {
    _mPtr = _Acc::_Parent(_mPtr);
    return (*this);
   }

   const_iterator& operator++() {
    if (!_mPtr.is_leaf()) {
     if (_Acc::_LeftChild(_mPtr) != _mPtr) {
      _mPtr = _Acc::_LeftChild(_mPtr);
     }else {
      _mPtr = _Acc::_RightChild(_mPtr);
     }
    }else {
     _mPtr = get_next(_mPtr);
    }    
    return (*this);
   }
   
   const_iterator operator++(int) {
    const_iterator _tmp = *this;
    ++*this;
    return _tmp;
   }
   const_iterator& operator--() {
    if (_Acc::_Parent(_mPtr) && _Acc::_LeftChild(_Acc::_Parent(_mPtr))) {
     _mPtr = _Acc::_LeftChild(_Acc::_Parent(_mPtr));
    }else {
     _mPtr = _Acc::_Parent(_mPtr);
    }
    return (*this);
   }
   const_iterator operator--(int) {
    const_iterator _tmp = *this;
    --*this;
    return _tmp;
   }
   bool operator==(const const_iterator& other) const{
    return (_mPtr == other._mPtr);
   }
   bool operator!=(const const_iterator& other) const {
    return (!(*this == other));
   }
   _Nodeptr _Mynode() const{
    return (_mPtr);
   }

  

  protected:
   _Nodeptr _mPtr;
  private:
  };

  friend class iterator;
  class iterator : public const_iterator {
  public:
   iterator(){}
   iterator(_Nodeptr _P) : const_iterator(_P){}
   reference operator*() const {
    return _Acc::_Value(_mPtr);
   }
   _Tptr operator->() const {
    return (&**this);
   }

   iterator& leftchild() {
    _mPtr = _Acc::_LeftChild(_mPtr);
    return (*this);
   }
   
   iterator& rightchild() {
    _mPtr = _Acc::_RightChild(_mPtr);
    return (*this);
   }
   
   iterator& parent() {
    _mPtr = _Acc::_Parent(_mPtr);
    return (*this);
   }

   iterator& operator++() {
    if (!_mPtr->is_leaf()) {
     if (_Acc::_LeftChild(_mPtr) != _mPtr) {
      _mPtr = _Acc::_LeftChild(_mPtr);
     }else {
      _mPtr = _Acc::_RightChild(_mPtr);
     }
    }else {
     _mPtr = get_next(_mPtr);
    }
    return (*this);
   }
   iterator operator++(int) {
    iterator _tmp = *this;
    ++*this;
    return (_tmp);
   }
   iterator& operator--() {
    if (_Acc::_Parent(_mPtr) && _Acc::_LeftChild(_Acc::_Parent(_mPtr))) {
     _mPtr = _Acc::_LeftChild(_Acc::_Parent(_mPtr));
    }else {
     _mPtr = _Acc::_Parent(_mPtr);
    }
    return (*this);
   }
   iterator operator--(int) {
    iterator _tmp = *this;
    --*this;
    return (_tmp);
   }
   bool operator==(const iterator& other) {
    return (_mPtr == other._mPtr);
   }
   bool operator!=(const iterator& other) {
    return (!(*this == other));
   }
  };
  
  typedef std::reverse_bidirectional_iterator<iterator, value_type,
   reference, _Tptr, difference_type> reverse_iterator;

  typedef std::reverse_bidirectional_iterator<const_iterator, value_type,
   const_reference, _Ctptr, difference_type> const_reverse_iterator;

  explicit tree(const _alloc& al = _alloc()) : allocator(al), _Root(_BuyNode()), _End(_Acc::_RightChild(_Root)), _size(0) {}
  iterator root() {
   return (iterator(_Acc::_LeftChild(_Root)));
  }
  const_iterator root() const{
   return (const_iterator(_Acc::_LeftChild(_Root)));
  }
  iterator begin() {
   return (iterator(_Acc::_LeftChild(_Root)));
  }
  const_iterator begin() const {
   return (const_iterator(_Acc::_LeftChild(_Root)));
  }
  iterator end() {
   return iterator(_End);
  }
  const_iterator end() const {
   reference const_iterator(_End);
  }
  private:
  bool insert(iterator _it, const _type& _val = _type()) {
   bool ret = false;
   _Nodeptr n = _BuyNode();
   _Acc::_Parent(n) = _Acc::_Parent(_it._Mynode());
   _Acc::_LeftChild(_Acc::_Parent(_it._Mynode())) = n;
   _Acc::_Parent(_it._Mynode()) = n;
   _Acc::_LeftChild(n) = _it._Mynode();
   _Acc::_Value(n) = _val;
   ret = true;
   return ret;
  }
  public:
  iterator AddLeftChild(iterator _it, const _type& _val = _type()) {
   _Nodeptr _s = _it._Mynode();
   if (_Acc::_LeftChild(_s) == _s) {
    _Nodeptr n = _BuyNode(_s);
    _Acc::_Value(n) = _val;
    _Acc::_LeftChild(_s) = n;
    _size++;
    return iterator(n);
   }
   return iterator(_Acc::_LeftChild(_s));
  }
  iterator AddRightChild(iterator _it, const _type& _val = _type()) {
   _Nodeptr _s = _it._Mynode();
   if (_Acc::_RightChild(_s) == _s) {
    _Nodeptr n = _BuyNode(_s);
    _Acc::_Value(n) = _val;
    _Acc::_RightChild(_s) = n;
    _size++;
    return iterator(n);
   }
   return iterator(_Acc::_RightChild(_s));
  }

  iterator erase(iterator _where) {
   _Nodeptr _node = _where._Mynode();
   _Nodeptr _p = _Acc::_Parent(_node);
   iterator ret;
   if (_node->is_left_child()) {
    _Acc::_LeftChild(_p) = _p;
    deletenode(_node);
    return iterator(_p)++;
   }else {
    _Acc::_RightChild(_p) = _p;
    deletenode(_node);
    if (_p->have_left_child()) {
     return iterator(_Acc::_LeftChild(_p))++;
    }else {
     return iterator(_p)++;
    }
   }
  }
  size_type size() {
   return _size;
  }
  size_type max_size() const {
   return 2000;
  }
  bool empty() const {
   return (begin() == end());
  }
  void clear() {
   erase(root());
  }
  private:
   _Nodeptr _BuyNode(_Nodeptr _parent = 0,_Nodeptr _lefe = 0, _Nodeptr _right = 0) {
    _Nodeptr _s = (_Nodeptr)allocator._Charalloc(sizeof(_Node));
    _Acc::_Parent(_s) = (_parent != NULL ? _parent : _s);
    _Acc::_LeftChild(_s) = (_lefe != NULL ? _lefe : _s);
    _Acc::_RightChild(_s) = (_right != NULL ? _right : _s);
    return _s;
   }
   void _Freenode(_Nodeptr _s) {
    allocator.deallocate(_s, 1);
   }

   void deletenode(_Nodeptr _node) {
    if (_node->have_left_child()) {
     deletenode(_Acc::_LeftChild(_node));
    }
    if (_node->have_right_child()) {
     deletenode(_Acc::_RightChild(_node));
    }
    _Freenode(_node);
    _size--;
   }

  private:
   _alloc allocator;
   _Nodeptr _Root,_End;
   size_type _size;
 };
}


#endif //!defined(EXT_STD_TREE_2ED68683_54C0_4aa6_B1BC_0C445F68DE4E_INCLUDED_)

原创粉丝点击