build_tree/
build_tree.rs

1use bin_tree::{Node, IteratorEx};
2
3#[derive(Debug)]
4struct NodeContent {
5    value: usize,
6    children: Option<(NodeRef, NodeRef)>,
7}
8
9type NodeRef = Box<NodeContent>;
10
11fn leaf(value: usize) -> NodeRef {
12    Box::new(NodeContent {
13        value,
14        children: None,
15    })
16}
17
18impl Node for NodeRef {
19    fn new_parent(self, right: Self) -> Self {
20        let value = self.value + right.value;
21        Box::new(NodeContent {
22            value,
23            children: Some((self, right)),
24        })
25    }
26
27    fn new_parent_from_single(self) -> Self {
28        self
29    }
30}
31
32fn main() {
33    let root = (0..11).map(leaf).build_tree().unwrap();
34    assert_eq!(55, root.value);
35    println!("root: {:?}", root);
36    println!("root.children: {:?}", root.children);
37}