From 676e934a80f961a1a32869c80793fb7b60a78e0a Mon Sep 17 00:00:00 2001 From: Calvin Josenhans <60663079+HalflingHelper@users.noreply.github.com> Date: Fri, 25 Oct 2024 20:11:14 -0400 Subject: [PATCH] Update StudentTest.java Better validate avl --- test/StudentTest.java | 62 +++++++++---------------------------------- 1 file changed, 12 insertions(+), 50 deletions(-) diff --git a/test/StudentTest.java b/test/StudentTest.java index ca226b5..3e39741 100644 --- a/test/StudentTest.java +++ b/test/StudentTest.java @@ -5,8 +5,6 @@ import java.util.*; import java.util.Random; -import static java.lang.Math.log; - public class StudentTest { @Test @@ -31,7 +29,7 @@ public void insertSmallBST() { } } - /** + /** * TODO: Test cases */ @Test @@ -41,56 +39,20 @@ public void test() { } /* - * Helper Methods to Validate Properties of - * BST and AVL Tree. + Helper Methods to Validate AVL Property */ - - // Check that the tree is ordered - public static void - validate_BST_property(OrderedSet tree) { - validate_BST_property(tree.root()); - } - // Check that the subtree satisfies the BST Property. - // Returns the min and max keys in the subtree of n. - public static Pair - validate_BST_property(Location n) { - if (n == null) { - return null; - } else { - Pair left_range = - validate_BST_property(n.left_child()); - Pair right_range = - validate_BST_property(n.right_child()); - if (left_range == null) { - left_range = new Pair<>(n.get(), n.get()); - } else { - assertTrue(left_range.getValue() < n.get()); - } - if (right_range == null) { - right_range = new Pair<>(n.get(), n.get()); - } else { - assertTrue(n.get() < right_range.getKey()); - } - return combine(left_range, right_range); - } + private static int calc_height(BinarySearchTree.Node n) { + if (n == null) return -1; + else return 1 + Math.max(calc_height(n.left), calc_height(n.right)); } - - // Check that the tree is an AVL tree. - public static void validate_AVL_property(OrderedSet tree) { - validate_AVL_property(tree.root()); + private static boolean validate_height(BinarySearchTree.Node n) { + if (n == null) return true; + if (Math.abs(calc_height(n.left) - calc_height(n.right)) > 1) return false; + return validate_height(n.left) && validate_height(n.right); } - // Checks that the subtree rooted at location n is and AVL tree - // and returns the height of this subtree. - public static int validate_AVL_property(Location n) { - if (n == null) { - return -1; - } else { - int h1, h2; - h1 = validate_AVL_property(n.left_child()); - h2 = validate_AVL_property(n.right_child()); - assertTrue(Math.abs(h2 - h1) < 2); - return 1 + max(h1, h2); - } + + private static void validate_height(BinarySearchTree avl) { + assertTrue(validate_height((BinarySearchTree.Node) avl.root)); } }