|
@@ -122,4 +122,23 @@ mod tests {
|
|
values.sort();
|
|
values.sort();
|
|
assert_eq!(items, values);
|
|
assert_eq!(items, values);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ #[test]
|
|
|
|
+ fn is_balanced() {
|
|
|
|
+ let values = vec![5, 3, 7, 1, 4, 6, 9];
|
|
|
|
+ let mut root = Node::new(values[0]);
|
|
|
|
+ for v in values[1..].iter() {
|
|
|
|
+ root.insert(*v);
|
|
|
|
+ }
|
|
|
|
+ fn traverse(node: &Node<i32>) {
|
|
|
|
+ assert!((node.balance_factor()).abs() <= 1);
|
|
|
|
+ if let Some(ref left) = node.left {
|
|
|
|
+ traverse(left);
|
|
|
|
+ }
|
|
|
|
+ if let Some(ref right) = node.right {
|
|
|
|
+ traverse(right);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ traverse(&root);
|
|
|
|
+ }
|
|
}
|
|
}
|