Browse Source

test(node): add test checking balance condition

Danilo Gómez 1 year ago
parent
commit
4902d33dfc
1 changed files with 19 additions and 0 deletions
  1. 19 0
      src/node.rs

+ 19 - 0
src/node.rs

@@ -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);
+    }
 }
 }