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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use crate::;
/// A depth first sequence is a representation of a tree in a linear storage of (depth, value) tuples.
/// This is useful in collecting trees from iterators, (de)-serializing trees or converting its variant
/// from one to another.
///
/// `DepthFirstSequence` struct is nothing but a wrapper around a `(usize, T)` iterator in order
/// to state explicitly that this iterator is expected to follow the depth-first-order of (depth, value) pairs.
///
/// A `DepthFirstSequence` can be created from any type that implements `IntoIterator<Item = (usize, T)>`
/// using `From` (or `Into`) traits.
///
/// In order to create a valid tree from the iterator, the order of pairs must satisfy certain conditions.
/// Assume (depth(i), value(i)) is the i-th item of the iterator.
/// Then, the following conditions summarize the valid relation between successive elements of the iterator:
///
/// * depth(0) = 0
/// * since the first node of the depth-first traversal is the root
/// * depth(i+1) < depth(i) is valid
/// * we are moving from a leaf node with depth(i) to next child of one of its ancestors
/// * depth(i+1) = depth(i) is valid
/// * we are moving from a leaf node to its sibling which is immediately right to it
/// * depth(i+1) = depth(i) + 1 is valid
/// * we are moving from a non-leaf node to its first child
///
/// On the contrary, if either of the following two conditions hold, we cannot build a valid tree.
///
/// * depth(0) > 0
/// * leads to [`DepthFirstSequenceError::NonZeroRootDepth`]
/// * depth(i + 1) = depth(i) + q where q > 1
/// * leads to [`DepthFirstSequenceError::DepthIncreaseGreaterThanOne`]
///
/// If either of these conditions hold, `try_from` or `try_into` methods will return the corresponding
/// error instead of a valid tree.
///
/// # Examples
///
/// ## Happy Paths
///
/// The following examples demonstrate the happy paths leading to successful collection of a tree from valid
/// depth-first sequences.
///
/// ```
/// use orx_tree::*;
///
/// // empty tree
///
/// let dfs = DepthFirstSequence::from([]);
/// let result: Result<DynTree<u32>, DepthFirstSequenceError> = dfs.try_into();
/// assert_eq!(result, Ok(Tree::empty()));
///
/// // non-empty tree
///
/// // 0
/// // ╱ ╲
/// // ╱ ╲
/// // 1 2
/// // ╱ ╱ ╲
/// // 3 4 5
/// // | |
/// // 6 7
/// let depth_value_pairs = [
/// (0, 0),
/// (1, 1),
/// (2, 3),
/// (3, 6),
/// (1, 2),
/// (2, 4),
/// (2, 5),
/// (3, 7),
/// ];
/// let dfs = DepthFirstSequence::from(depth_value_pairs.clone());
/// let result: Result<DynTree<u32>, DepthFirstSequenceError> = dfs.try_into();
///
/// assert!(result.is_ok());
/// let tree = result.unwrap();
///
/// let bfs: Vec<_> = tree.root().walk::<Bfs>().copied().collect();
/// assert_eq!(bfs, [0, 1, 2, 3, 4, 5, 6, 7]);
///
/// // we can get back the dfs-sequence from constructed tree using walk_with
///
/// let mut t = Traversal.dfs().with_depth();
/// let dfs_back_from_tree: Vec<_> = tree
/// .root()
/// .walk_with(&mut t)
/// .map(|(depth, val)| (depth, *val))
/// .collect();
/// assert_eq!(dfs_back_from_tree, depth_value_pairs);
///
/// // we can construct back any fitting tree variant from the sequence
///
/// let result = DepthFirstSequence::from(dfs_back_from_tree).try_into();
/// assert!(result.is_ok());
///
/// let tree_back: BinaryTree<u32> = result.unwrap();
/// assert_eq!(tree, tree_back);
/// ```
///
/// ## Error Paths
///
/// The following examples illustrate the two potential error cases that can be observed due to
/// the iterator not yielding a valid depth-first sequence.
///
/// ```
/// use orx_tree::*;
///
/// // root with depth > 0
///
/// let dfs = DepthFirstSequence::from([(1, 1)]);
/// let result: Result<DynTree<u32>, DepthFirstSequenceError> = dfs.try_into();
/// assert_eq!(result, Err(DepthFirstSequenceError::NonZeroRootDepth));
///
/// // missing node (or forgotten depth) in the sequence
///
/// // 0
/// // ╱ ╲
/// // ╱ ╲
/// // 1 2
/// // ╱ ╱ ╲
/// // ??? 4 5
/// // | |
/// // 6 7
/// let depth_value_pairs = [
/// (0, 0),
/// (1, 1),
/// // (2, 3), -> forgotten node leads to depth jump from 1 to 3
/// (3, 6),
/// (1, 2),
/// (2, 4),
/// (2, 5),
/// (3, 7),
/// ];
/// let dfs = DepthFirstSequence::from(depth_value_pairs.clone());
/// let result: Result<DynTree<u32>, DepthFirstSequenceError> = dfs.try_into();
/// assert_eq!(
/// result,
/// Err(DepthFirstSequenceError::DepthIncreaseGreaterThanOne {
/// depth: 1,
/// succeeding_depth: 3
/// })
/// );
/// ```
where
I: ;
/// A depth first sequence, or [`DepthFirstSequence`] is simply a sequence of `(usize, T)` tuples
/// corresponding to (depth, value) pairs of nodes of a tree which are ordered by the depth-first
/// traversal order.
///
/// Therefore, not all `IntoIterator<Item = (usize, T)>` types satisfy the depth-first sequence
/// requirement.
/// The invalid sequences are represented by the `DepthFirstSequenceError` type.
/// # Panics
///
/// Panics
///
/// * If the `iter` is an empty iterator; it must contain at least one child node.
/// * If the `tree` is empty, it must have at least the `root`.