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
use std::{fmt::Display, mem::swap};

use crate::DisplayableNode;

pub struct BinaryTree<D> {
    data: D,
    children: [Option<Box<BinaryTree<D>>>; 2],
}
#[macro_export]
macro_rules! bt {
    (
 $value:expr,
 $left:expr,
 $right:expr
 ) => {{
        BinaryTree::assemble(($value), Some($left), Some($right))
    }};

    (
 $value:expr,
 ,
 $right:expr
 ) => {{
        BinaryTree::assemble(($value), None, Some($right))
    }};

    (
 $value:expr,
 $left:expr,

 ) => {{
        BinaryTree::assemble(($value), Some($left), None)
    }};

    (
 $value:expr
 ) => {{
        BinaryTree::new(($value))
    }};
}

impl<D> BinaryTree<D> {
    pub fn new(data: D) -> Self {
        Self {
            data,
            children: [None, None],
        }
    }

    pub fn assemble(data: D, x: Option<BinaryTree<D>>, y: Option<BinaryTree<D>>) -> Self {
        let x = x.map(|x| Box::new(x));
        let y = y.map(|y| Box::new(y));
        Self {
            data,
            children: [x, y],
        }
    }

    pub fn children(&self) -> (Option<&BinaryTree<D>>, Option<&BinaryTree<D>>) {
        let [x, y] = &self.children;
        let x = x.as_ref().map(|boxed_x| boxed_x.as_ref());
        let y = y.as_ref().map(|boxed_y| boxed_y.as_ref());
        (x, y)
    }

    pub fn x_ref(&self) -> Option<&BinaryTree<D>> {
        self.children[0].as_ref().map(|boxed| boxed.as_ref())
    }

    pub fn y_ref(&self) -> Option<&BinaryTree<D>> {
        self.children[1].as_ref().map(|boxed| boxed.as_ref())
    }

    pub fn x_mut(&mut self) -> Option<&mut BinaryTree<D>> {
        self.children[0].as_mut().map(|boxed| boxed.as_mut())
    }

    pub fn y_mut(&mut self) -> Option<&mut BinaryTree<D>> {
        self.children[1].as_mut().map(|boxed| boxed.as_mut())
    }

    pub fn set_x(&mut self, x: BinaryTree<D>) -> Option<BinaryTree<D>> {
        let mut outer = Some(Box::new(x));
        let outer_ref = &mut outer;
        let inner_ref = &mut self.children[0];
        swap(outer_ref, inner_ref);
        outer.map(|boxed| *boxed)
    }

    pub fn set_y(&mut self, y: BinaryTree<D>) -> Option<BinaryTree<D>> {
        let mut outer = Some(Box::new(y));
        let outer_ref = &mut outer;
        let inner_ref = &mut self.children[1];
        swap(outer_ref, inner_ref);
        outer.map(|boxed| *boxed)
    }

    pub fn data(&self) -> &D {
        &self.data
    }

    pub fn data_put(&mut self) -> &mut D {
        &mut self.data
    }
}

impl<D: Display> DisplayableNode for BinaryTree<D> {
    fn label(&self) -> Option<String> {
        let data = self.data();
        Some(format!("{data}"))
    }

    fn children(&self) -> Vec<&Self> {
        match self.children() {
            (Some(left), Some(right)) => vec![left, right],
            (Some(left), None) => vec![left],
            (None, Some(right)) => vec![right],
            (None, None) => vec![],
        }
    }
}

#[test]
fn it_works() {
    let tree = bt!(
        1,
        bt!(
            2,
            ,
            bt!(5)
        ),
        bt!(3, bt!(4, bt!(7),), bt!(6))
    );

    println!("{}", tree.display());
}