-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTreeNodeIterator.h
88 lines (65 loc) · 2.6 KB
/
TreeNodeIterator.h
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
#ifndef TreeNodeIterator_h
#define TreeNodeIterator_h
#include <iostream>
#include <stack>
#include "TreeNode.h"
class TreeNodeIterator {
std::stack<TreeNode *> stack_;
public:
// Обязательными являются первые два параметра:
// Тип итератора:
typedef std::input_iterator_tag iterator_category;
// Тип выбирается из следующих типов:
// input_iterator_tag
// Должен поддерживать префиксную форму инкремента, оператор !=,
// оператор * и оператор ->. Понадобится конструктор и конструктор
// копирования.
// output_iterator_tag
// forward_iterator_tag
// bidirectional_iterator_tag
// random_access_iterator_tag
// Тип значения которое хранится и возвращается операторами * и ->:
typedef TreeNode value_type;
// Тип который может описывать растояние между итераторами:
typedef std::ptrdiff_t difference_type;
// Тип указателя на значение:
typedef TreeNode *pointer;
// Тип ссылки на значения:
typedef TreeNode &reference;
explicit TreeNodeIterator(TreeNode *value = nullptr) {
if (value)
stack_.push(value);
}
// TreeNode * operator*() const {
// return !stack_.empty() ? stack_.top() : nullptr;
// }
typename TreeNodeIterator::reference operator*() const {
TreeNode *node = nullptr;
if (!stack_.empty()) {
node = stack_.top();
}
return *node;
}
TreeNode * operator->() const {
return !stack_.empty() ? stack_.top() : nullptr;
}
// Хорошая практика добавлять поддержку != и == вместе:
bool operator==(TreeNodeIterator const &other) const {
if (stack_.empty() || other.stack_.empty())
return stack_.empty() && other.stack_.empty();
return stack_.top() == other.stack_.top();
}
bool operator!=(TreeNodeIterator const &other) const {
return !(*this == other);
}
TreeNodeIterator & operator++() {
if (!stack_.empty()) {
TreeNode *node = stack_.top();
stack_.pop();
for (int i = node->GetChildrenCount() - 1; i >= 0; --i)
stack_.push(node->GetChildAtIndex(i));
}
return *this;
}
};
#endif /* TreeNodeIterator_h */