Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. template<typename Type>
  2. class NodeIterator {
  3.  private:
  4.     const Node <Type> *p;
  5.     const Node<Type> *root;
  6.  
  7.  public:
  8.     NodeIterator(): p(nullptr), root(nullptr) {}
  9.  
  10.     NodeIterator(shared_ptr<const Node <Type>> p,
  11.                  shared_ptr<const Node <Type>> root): p(p.get()), root(root.get()) {}
  12.  
  13.     NodeIterator(shared_ptr<const Node<Type>> root): root(root.get()) {
  14.         p = this->root;
  15.         while (p != nullptr && p->l_son != nullptr) {
  16.             p = p->l_son.get();
  17.         }
  18.     }
  19.  
  20.     NodeIterator(const NodeIterator &other): p(other.p), root(other.root) {}
  21.  
  22.     NodeIterator &operator=(const NodeIterator &other) {
  23.         p = other.p;
  24.         root = other.root;
  25.         return *this;
  26.     }
  27.  
  28.     ~NodeIterator() {
  29.     }
  30.  
  31.     const Type &operator*() const {
  32.         return p->key;
  33.     }
  34.  
  35.     const Type *operator->() const {
  36.         return &(p->key);
  37.     }
  38.  
  39.     bool operator==(const NodeIterator<Type> &other) const {
  40.         return (p == other.p && root == other.root);
  41.     }
  42.  
  43.     bool operator!=(const NodeIterator<Type> &other) const {
  44.         return !(p == other.p && root == other.root);
  45.     }
  46.  
  47.     NodeIterator &operator++() {
  48.         if (p == nullptr) {
  49.             return *this;
  50.         }
  51.  
  52.         if (p->r_son != nullptr) {
  53.             p = p->r_son.get();
  54.         } else {
  55.             while (p->parent != nullptr && p->parent->r_son.get() == p) {
  56.                 p = p->parent.get();
  57.             }
  58.             p = p->parent.get();
  59.  
  60.             return *this;
  61.         }
  62.  
  63.         while (p->l_son != nullptr) {
  64.             p = p->l_son.get();
  65.         }
  66.  
  67.         return *this;
  68.     }
  69.  
  70.     NodeIterator operator++(int) {
  71.         auto res = *this;
  72.         ++*this;
  73.  
  74.         return res;
  75.     }
  76.  
  77.     NodeIterator &operator--() {
  78.         if (p == nullptr) {
  79.             p = root;
  80.             if (p == nullptr) {
  81.                 return *this;
  82.             }
  83.  
  84.             while (p->r_son != nullptr) {
  85.                 p = p->r_son.get();
  86.             }
  87.  
  88.             return *this;
  89.         }
  90.  
  91.         if (p->l_son != nullptr) {
  92.             p = p->l_son.get();
  93.             while (p->r_son != nullptr) {
  94.                 p = p->r_son.get();
  95.             }
  96.  
  97.             return *this;
  98.         }
  99.  
  100.         while (p->parent != nullptr && p->parent->l_son.get() == p) {
  101.             p = p->parent.get();
  102.         }
  103.         p = p->parent.get();
  104.  
  105.         return *this;
  106.     }
  107.  
  108.     NodeIterator operator--(int) {
  109.         auto res = *this;
  110.         --*this;
  111.  
  112.         return res;
  113.     }
  114. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement