-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTuptrIterator.hpp
More file actions
190 lines (161 loc) · 4.41 KB
/
Copy pathBSTuptrIterator.hpp
File metadata and controls
190 lines (161 loc) · 4.41 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once
/**
* const iterator for Binary Search Tree (BST) specific for std::unique_ptr nodes.
* Multiple non-recursive tree traversal orders are provided including:
* 1) Inorder
* 2) Preorder
* 3) Postorder
* 4) Levelorder
*/
#include "BSTuptrNode.hpp"
#include <cstddef> // std::ptrdiff_t
#include <cstdint> // std::uint8_t
#include <iterator>
#include <deque>
#include <queue>
#include <stack>
#include <stdexcept> // std::invalid_argument
namespace wndx::algo {
namespace ds {
enum class TreeTravOrder : std::uint8_t
{
IN_ORDER,
PRE_ORDER,
POST_ORDER,
LEVEL_ORDER,
};
template <typename T>
class BSTuptrIterator
{
public:
// TODO: rewrite more properly (full class should be rewritten)
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = const std::ptrdiff_t;
using value_type = const T;
using reference = const T&;
using pointer = const T* const;
using node_ptr = const Node<T>*;
using const_iterator = BSTuptrIterator<T>;
using enum TreeTravOrder;
enum TreeTravOrder trav_order{ TreeTravOrder::IN_ORDER };
protected:
std::deque<node_ptr> m_deque;
node_ptr m_root;
std::size_t current {0};
public:
explicit BSTuptrIterator(const node_ptr ptr, const TreeTravOrder order=IN_ORDER)
: trav_order{ order }, m_root{ ptr }
{
traverse();
m_deque.push_back(nullptr); // end() of the tree
}
BSTuptrIterator() = default;
~BSTuptrIterator() = default;
// dereference
reference operator*() const { return m_deque[current]->m_data; }
// pre/post-increment
const_iterator& operator++() { ++current; return *this; };
const_iterator operator++(int) { const_iterator tmp = *this; ++current; return tmp; };
// pre/post-decrement
const_iterator& operator--() { --current; return *this; };
const_iterator operator--(int) { const_iterator tmp = *this; --current; return tmp; };
// eq/inequality
bool operator==(const_iterator const& rhs) const
{
return m_deque[current] == rhs.m_deque[rhs.current];
}
auto operator<=>(const_iterator const& rhs) const = default;
protected:
// O(1)
void ins_back(const node_ptr p)
{
m_deque.push_back(p);
}
// O(1)
void ins_front(const node_ptr p)
{
m_deque.push_front(p);
}
public:
void inorder()
{
if (!m_root) return;
std::stack<node_ptr> s;
node_ptr p{ m_root };
while (p || !s.empty()) {
while (p) {
s.push(p);
p = p->l.get();
}
p = s.top();
s.pop();
ins_back(p);
p = p->r.get();
}
}
void preorder()
{
if (!m_root) return;
std::stack<node_ptr> s;
node_ptr p{ m_root };
s.push(p);
while (!s.empty()) {
p = s.top();
s.pop();
ins_back(p);
if(p->r) s.push(p->r.get());
if(p->l) s.push(p->l.get());
}
}
void postorder()
{
if (!m_root) return;
std::stack<node_ptr> s;
node_ptr p{ m_root };
s.push(p);
while (!s.empty()) {
p = s.top();
s.pop();
ins_front(p);
if (p->l) s.push(p->l.get());
if (p->r) s.push(p->r.get());
}
}
void levelorder()
{
if (!m_root) return;
std::queue<node_ptr> q;
node_ptr p{ m_root };
q.push(p);
while (!q.empty()) {
p = q.front();
q.pop();
ins_back(p);
if (p->l) q.push(p->l.get());
if (p->r) q.push(p->r.get());
}
}
void traverse()
{
switch (trav_order) {
case IN_ORDER:
inorder();
break;
case PRE_ORDER:
preorder();
break;
case POST_ORDER:
postorder();
break;
case LEVEL_ORDER:
levelorder();
break;
// LCOV_EXCL_START - is it even possible to properly cover this case?
default:
throw std::invalid_argument("No Such Tree Traversal Order.");
// LCOV_EXCL_STOP
}
}
};
} // namespace ds
} // namespace wndx::algo