-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_iterator.hpp
More file actions
163 lines (139 loc) · 5.56 KB
/
tree_iterator.hpp
File metadata and controls
163 lines (139 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef TREE_ITERATOR_HPP
#define TREE_ITERATOR_HPP
#include "tree.hpp"
#include "iterator.hpp"
namespace ft {
template <class T, class Node>
class tree_iterator : public ft::iterator<std::bidirectional_iterator_tag, T>{
public :
typedef typename ft::iterator_traits<T>::value_type value_type;
typedef typename ft::iterator_traits<T>::pointer pointer;
typedef typename ft::iterator_traits<T>::reference reference;
typedef typename ft::iterator_traits<T>::difference_type difference_type;
typedef typename ft::iterator_traits<T>::iterator_category iterator_catergory;
public :
typedef Node* node_ptr;
private :
node_ptr current;
node_ptr nil;
node_ptr _end;
public :
tree_iterator(void) : current(NULL), nil(NULL), _end(NULL) {};
explicit tree_iterator(const node_ptr tmp, const node_ptr _nil, const node_ptr end) : current(tmp), nil(_nil), _end(end) {};
template <class T2, class Node2>
tree_iterator(const tree_iterator<T2, Node2>& x) : current(x.base()), nil(x.nilbase()), _end(x.endbase()) {};
~tree_iterator(void){};
node_ptr base(void) const { return (current); }
node_ptr nilbase(void) const { return (nil); }
node_ptr endbase(void) const { return (_end); }
template <class U>
tree_iterator& operator=(const tree_iterator<U, Node> it)
{
current = it.base();
nil = it.nilbase();
_end = it.endbase();
return (*this);
}
reference operator*() const
{
return (this->current->data);
}
pointer operator->() const
{
return (&(this->operator*()));
}
tree_iterator& operator++()
{
node_ptr tmp;
if (current == this->_end->parent || current == this->_end)
{
current = this->_end;
return (*this);
}
if (current->right != this->nil)
{
tmp = MinNode(current->right);
this->current = tmp;
}
else if (current->right == this->nil)
{
node_ptr parent = current->parent;
while (parent != this->nil && parent->right == current)
{
current = parent;
parent = parent->parent;
}
current = parent;
}
return (*this);
}
tree_iterator operator++(int)
{
tree_iterator tmp = *this;
this->operator++();
return (tmp);
}
tree_iterator& operator--()
{
node_ptr tmp;
if (current == this->_end)
{
this->current = this->_end->parent;
return (*this);
}
if (current->left != this->nil)
{
tmp = MaxNode(current->left);
this->current = tmp;
}
else if (current->left == this->nil)
{
node_ptr parent = current->parent;
while (parent != this->nil && parent->left == current)
{
current = parent;
parent = parent->parent;
}
current = parent;
}
return (*this);
}
tree_iterator operator--(int)
{
tree_iterator tmp = *this;
this->operator--();
return (tmp);
}
Node* MinNode(node_ptr node)
{
if (node == NULL)
return (NULL);
while (node->left != this->nil)
{
node = node->left;
}
return (node);
}
Node* MaxNode(node_ptr node)
{
if (node == NULL)
return (NULL);
while (node->right != this->nil)
{
node = node->right;
}
return (node);
}
};
template <class T, class Node>
bool operator==(const tree_iterator<T, Node>& x, const tree_iterator<T, Node>& y)
{
return (x.base() == y.base());
}
template <class T, class Node>
bool operator!=(const tree_iterator<T, Node>& x, const tree_iterator<T, Node>& y)
{
return (x.base() != y.base());
}
}
#endif