untested INSERT LETS GO

This commit is contained in:
LinlyBoi
2023-05-18 09:03:57 +03:00
parent 45fe7a254f
commit e095c0961f

View File

@@ -1,31 +1,58 @@
struct calcNode { use std::io::Error;
#[derive(Clone)]
struct CalcNode {
item: Item, item: Item,
left: Option<Box<calcNode>>, left: Option<Box<CalcNode>>,
right: Option<Box<calcNode>>, right: Option<Box<CalcNode>>,
} }
impl calcNode { impl CalcNode {
pub fn new(item: Item, left: Option<Box<calcNode>>, right: Option<Box<calcNode>>) -> Self { pub fn new(item: Item, left: Option<Box<CalcNode>>, right: Option<Box<CalcNode>>) -> Self {
Self { item, left, right } Self { item, left, right }
} }
} }
#[derive(Clone)]
pub enum Item { pub enum Item {
Num(i32), Num(i32),
Oper(Operation), Oper(Operation),
} }
#[derive(Clone)]
pub enum Operation { pub enum Operation {
Add, Add,
Sub, Sub,
Div, Div,
Mult, Mult,
} }
pub struct calcTree { pub struct CalcTree {
root: calcNode, root: CalcNode,
} }
impl calcTree { impl CalcTree {
pub fn new(item: Item) -> Self { pub fn new(item: Item) -> Self {
let root = calcNode::new(item, None, None); let root = CalcNode::new(item, None, None);
Self { root } Self { root }
} }
pub fn insert(&mut self, item: Item) {
use Item::*;
if let Oper(_) = item {
match &self.root.item {
Num(_) => self.root = CalcNode::new(item, Some(Box::new(self.root.clone())), None),
Oper(_) => panic!("fuck off"),
}
} else {
match &self.root.item {
Num(_) => panic!("fuck off"),
Oper(_) => {
if let None = self.root.right {
self.root.right = Some(Box::new(CalcNode::new(item, None, None)));
} else if let None = self.root.left {
self.root.left = Some(Box::new(CalcNode::new(item, None, None)));
} else {
self.insert(item)
}
}
}
}
}
} }