Based on the background of a scripting language with some C, trying to “learn”, Rust encourages me to question my competence. I am trying to figure out how to change its pointer and am trying to do this.
Besides copying from additional libs, I cannot determine the recursion that I need in the binary tree. In particular, I do not know how to replace pointer branches. If I can trick and use a temporary vector with a linked list to return a new list, or add a new Cons (value, ~ Cons) to the header, the branches hit me.
enum NaiveTreeNode {
NNil,
NNode(~NaiveTreeNode, ~NaiveTreeNode, int, char)
// left right key val
}
impl NaiveTreeNode {
fn eq(first_node: &NaiveTreeNode, second_node: &NaiveTreeNode) -> bool {
match (first_node, second_node) {
(&NNil, &NNil) => true,
( &NNode( ~ref left_lval, ~ref left_rval, left_leafkey, left_leafval ),
&NNode( ~ref right_lval, ~ref right_rval, right_leafkey, right_leafval )
) if left_leafkey == right_leafkey && left_leafval == right_leafval => {
NaiveTreeNode::eq(left_lval, right_lval) && NaiveTreeNode::eq(left_rval, right_rval)
},
_ => false
}
}
fn add_branch(&mut self, node_to_add: ~NaiveTreeNode) {
match (self, node_to_add) {
(&NaiveTreeNode(~NNil, ~ref r_branch, leaf_key, leaf_val), ~NaiveTreeNode(_, _, new_node_key, _) )
if leaf_key > new_node_key => self = &NaiveTreeNode(node_to_add, *r_branch, leaf_key, leaf_val),
(&NaiveTreeNode(~ref l_branch, ~NNil, leaf_key, leaf_val), ~NaiveTreeNode(_, _, new_node_key, _))
if leaf_key < new_node_key => self = &NaiveTreeNode(*l_branch, node_to_add, leaf_key, leaf_val),
(&NaiveTreeNode(~ref l_branch, _, leaf_key, _), ~NaiveTreeNode(_, _, new_node_key, _))
if leaf_key > new_node_key => self.add_branch(l_branch, node_to_add),
(&NaiveTreeNode(_, ~ref r_branch, leaf_key, _), ~NaiveTreeNode(_, _, new_node_key, _))
if leaf_key < new_node_key => self.add_branch(l_branch, node_to_add),
(_, ~NNil) => fail!("NNil branch. failing"),
(&NNil, _) => fail!("NNil trunk. failing"),
_ => fail!("something is wrong. failing.")
};
}
}
11 errors are thrown in this compiler, and when I print it, it looks like a pseudo-code. I am upset because I feel good implementing the tree with pointers to C.
, , - , , ? , , . , .
, , . Treemap, , , , , - , , !