Pārlūkot izejas kodu

test(node): add test checking balance condition

Danilo Gómez 1 gadu atpakaļ
vecāks
revīzija
4902d33dfc
1 mainītis faili ar 19 papildinājumiem un 0 dzēšanām
  1. 19 0
      src/node.rs

+ 19 - 0
src/node.rs

@@ -122,4 +122,23 @@ mod tests {
         values.sort();
         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);
+    }
 }