Commit eaada400 authored by Isaac Xu's avatar Isaac Xu
Browse files

simplified

parent 4e3efe45
......@@ -6,24 +6,12 @@ public class VerifyAVL {
return true;
}
boolean isBst = verifyBst(root, null, null);
//int hDiff = verifyBalance(root);
//boolean isBalanced = true;
//if (hDiff > 1) {
//isBalanced = false;
//}
//if (root.height != Math.max(root.right.height, root.left.height) + 1) {
//return false;
//}
boolean isBalanced = verifyBalance(root);
if (!isBst || !isBalanced) {
return false;
}
return true;
return verifyBstAndBalance(root, null, null);
}
/* TODO: Add private methods if you want (recommended) */
private static boolean verifyBst(AVLNode root, Integer low, Integer high) {
private static boolean verifyBstAndBalance(AVLNode root, Integer low, Integer high) {
if (root == null) {
return true;
}
......@@ -31,18 +19,11 @@ public class VerifyAVL {
if ((low != null && root.key <= low) || (high != null && root.key >= high)) {
return false;
}
// The left and right subtree must also be valid.
return verifyBst(root.right, root.key, high) && verifyBst(root.left, low, root.key);
}
private static boolean verifyBalance(AVLNode root) {
if (root == null) {
return true;
}
if (!verifyNode(root)) {
return false;
}
return verifyBalance(root.left) && verifyBalance(root.right);
// The left and right subtree must also be valid.
return verifyBstAndBalance(root.right, root.key, high) && verifyBstAndBalance(root.left, low, root.key);
}
private static boolean verifyNode(AVLNode root) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment