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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! Tree statistics and metrics.
use crate::tree::Tree;
/// Statistics about a tree structure.
///
/// This struct provides various metrics about a tree, including its depth,
/// width, and counts of nodes and leaves.
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(
any(
feature = "serde",
feature = "serde-json",
feature = "serde-yaml",
feature = "serde-toml",
feature = "serde-ron"
),
derive(serde::Serialize, serde::Deserialize)
)]
pub struct TreeStats {
/// Maximum depth of the tree
pub depth: usize,
/// Maximum width at any level
pub width: usize,
/// Total number of nodes
pub node_count: usize,
/// Total number of leaves
pub leaf_count: usize,
/// Total number of lines across all leaves
pub total_lines: usize,
}
impl Tree {
/// Returns the maximum depth of the tree.
///
/// The depth is the number of levels from the root to the deepest node.
/// A single node or leaf has depth 0.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
///
/// let tree = Tree::Node("root".to_string(), vec![
/// Tree::Leaf(vec!["item".to_string()])
/// ]);
/// assert_eq!(tree.depth(), 1);
/// ```
pub fn depth(&self) -> usize {
match self {
Tree::Node(_, children) => {
if children.is_empty() {
0
} else {
1 + children
.iter()
.map(|child| child.depth())
.max()
.unwrap_or(0)
}
}
Tree::Leaf(_) => 0,
}
}
/// Returns the maximum width at any level of the tree.
///
/// The width is the maximum number of children at any single level.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
///
/// let tree = Tree::Node("root".to_string(), vec![
/// Tree::Leaf(vec!["a".to_string()]),
/// Tree::Leaf(vec!["b".to_string()]),
/// Tree::Leaf(vec!["c".to_string()]),
/// ]);
/// assert_eq!(tree.width(), 3);
/// ```
pub fn width(&self) -> usize {
let mut level_widths = Vec::new();
self.collect_widths_at_level(0, &mut level_widths);
// Width is the maximum number of children at any level
level_widths.into_iter().max().unwrap_or(0)
}
fn collect_widths_at_level(&self, level: usize, widths: &mut Vec<usize>) {
match self {
Tree::Node(_, children) => {
// Count children at this level
if level >= widths.len() {
widths.resize(level + 1, 0);
}
widths[level] = widths[level].max(children.len());
for child in children {
child.collect_widths_at_level(level + 1, widths);
}
}
Tree::Leaf(_) => {
// Leaves don't contribute to width at their level
}
}
}
/// Returns the total number of nodes in the tree.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
///
/// let tree = Tree::Node("root".to_string(), vec![
/// Tree::Node("child".to_string(), vec![
/// Tree::Leaf(vec!["leaf".to_string()])
/// ])
/// ]);
/// assert_eq!(tree.node_count(), 2);
/// ```
pub fn node_count(&self) -> usize {
match self {
Tree::Node(_, children) => {
1 + children
.iter()
.map(|child| child.node_count())
.sum::<usize>()
}
Tree::Leaf(_) => 0,
}
}
/// Returns the total number of leaves in the tree.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
///
/// let tree = Tree::Node("root".to_string(), vec![
/// Tree::Leaf(vec!["a".to_string()]),
/// Tree::Leaf(vec!["b".to_string()]),
/// ]);
/// assert_eq!(tree.leaf_count(), 2);
/// ```
pub fn leaf_count(&self) -> usize {
match self {
Tree::Node(_, children) => children.iter().map(|child| child.leaf_count()).sum(),
Tree::Leaf(_) => 1,
}
}
/// Returns the total number of lines across all leaves in the tree.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
///
/// let tree = Tree::Node("root".to_string(), vec![
/// Tree::Leaf(vec!["line1".to_string(), "line2".to_string()]),
/// Tree::Leaf(vec!["line3".to_string()]),
/// ]);
/// assert_eq!(tree.total_lines(), 3);
/// ```
pub fn total_lines(&self) -> usize {
match self {
Tree::Node(_, children) => children.iter().map(|child| child.total_lines()).sum(),
Tree::Leaf(lines) => lines.len(),
}
}
/// Returns statistics about the tree.
///
/// # Examples
///
/// ```
/// use treelog::Tree;
///
/// let tree = Tree::Node("root".to_string(), vec![
/// Tree::Leaf(vec!["item".to_string()])
/// ]);
/// let stats = tree.stats();
/// assert_eq!(stats.depth, 1);
/// assert_eq!(stats.node_count, 1);
/// assert_eq!(stats.leaf_count, 1);
/// ```
pub fn stats(&self) -> TreeStats {
TreeStats {
depth: self.depth(),
width: self.width(),
node_count: self.node_count(),
leaf_count: self.leaf_count(),
total_lines: self.total_lines(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_depth() {
let tree = Tree::Node(
"root".to_string(),
vec![Tree::Leaf(vec!["item".to_string()])],
);
assert_eq!(tree.depth(), 1);
let deep_tree = Tree::Node(
"root".to_string(),
vec![Tree::Node(
"child".to_string(),
vec![Tree::Node(
"grandchild".to_string(),
vec![Tree::Leaf(vec!["leaf".to_string()])],
)],
)],
);
assert_eq!(deep_tree.depth(), 3);
let single_node = Tree::new_node("root");
assert_eq!(single_node.depth(), 0);
let single_leaf = Tree::new_leaf("leaf");
assert_eq!(single_leaf.depth(), 0);
}
#[test]
fn test_width() {
let tree = Tree::Node(
"root".to_string(),
vec![
Tree::Leaf(vec!["a".to_string()]),
Tree::Leaf(vec!["b".to_string()]),
Tree::Leaf(vec!["c".to_string()]),
],
);
assert_eq!(tree.width(), 3);
let tree2 = Tree::Node(
"root".to_string(),
vec![
Tree::Node(
"child1".to_string(),
vec![
Tree::Leaf(vec!["gc1".to_string()]),
Tree::Leaf(vec!["gc2".to_string()]),
],
),
Tree::Node(
"child2".to_string(),
vec![Tree::Leaf(vec!["gc3".to_string()])],
),
],
);
assert_eq!(tree2.width(), 2);
let single_leaf = Tree::new_leaf("leaf");
assert_eq!(single_leaf.width(), 0);
}
#[test]
fn test_node_count() {
let tree = Tree::Node(
"root".to_string(),
vec![Tree::Leaf(vec!["item".to_string()])],
);
assert_eq!(tree.node_count(), 1);
let tree2 = Tree::Node(
"root".to_string(),
vec![Tree::Node(
"child".to_string(),
vec![Tree::Leaf(vec!["leaf".to_string()])],
)],
);
assert_eq!(tree2.node_count(), 2);
let leaf = Tree::new_leaf("leaf");
assert_eq!(leaf.node_count(), 0);
}
#[test]
fn test_leaf_count() {
let tree = Tree::Node(
"root".to_string(),
vec![
Tree::Leaf(vec!["a".to_string()]),
Tree::Leaf(vec!["b".to_string()]),
],
);
assert_eq!(tree.leaf_count(), 2);
let tree2 = Tree::Node(
"root".to_string(),
vec![Tree::Node(
"child".to_string(),
vec![
Tree::Leaf(vec!["a".to_string()]),
Tree::Leaf(vec!["b".to_string()]),
],
)],
);
assert_eq!(tree2.leaf_count(), 2);
let leaf = Tree::new_leaf("leaf");
assert_eq!(leaf.leaf_count(), 1);
}
#[test]
fn test_total_lines() {
let tree = Tree::Node(
"root".to_string(),
vec![
Tree::Leaf(vec!["line1".to_string(), "line2".to_string()]),
Tree::Leaf(vec!["line3".to_string()]),
],
);
assert_eq!(tree.total_lines(), 3);
let leaf = Tree::new_leaf("single");
assert_eq!(leaf.total_lines(), 1);
}
#[test]
fn test_stats() {
let tree = Tree::Node(
"root".to_string(),
vec![
Tree::Node(
"child".to_string(),
vec![Tree::Leaf(vec!["leaf".to_string()])],
),
Tree::Leaf(vec!["leaf2".to_string()]),
],
);
let stats = tree.stats();
assert_eq!(stats.depth, 2);
assert_eq!(stats.node_count, 2);
assert_eq!(stats.leaf_count, 2);
assert_eq!(stats.total_lines, 2);
assert_eq!(stats.width, 2);
}
}