Tree identical
The tree is_dentical
function checks if the current tree is identical to another tree that is
being passed in as input.
If both trees root's values match - then it proceeds to check whether or not left and right subtree-s are also identical or not. This is done recursively.
Following is the definition:
//Is this tree is identical to other tree?
pub fn is_identical(&self, other: &Self) -> bool {
match self.0 {
Some(ref this) => match other {
Tree(Some(ref that)) => {
if this.borrow().key == that.borrow().key {
let this_left = &this.borrow().left;
let that_left = &that.borrow().left;
let this_right = &this.borrow().right;
let that_right = &that.borrow().right;
let left_matched = match this_left {
Some(ref this_tree) => match that_left {
Some(ref that_tree) => {
return Self::is_identical(
&this_tree.borrow(),
&that_tree.borrow(),
);
}
None => false,
},
None => that_left.is_none(),
};
let right_matched = match this_right {
Some(ref this_tree) => match that_right {
Some(ref that_tree) => {
return Self::is_identical(
&this_tree.borrow(),
&that_tree.borrow(),
);
}
None => false,
},
None => that_right.is_none(),
};
left_matched && right_matched
} else {
false
}
}
Tree(None) => false,
},
None => match other {
Tree(Some(_)) => false,
Tree(None) => true,
},
}
}