diff --git a/src/ch/zhaw/ads/AVLSearchTree.java b/src/ch/zhaw/ads/AVLSearchTree.java index c25511c..824f1f9 100644 --- a/src/ch/zhaw/ads/AVLSearchTree.java +++ b/src/ch/zhaw/ads/AVLSearchTree.java @@ -8,9 +8,12 @@ package ch.zhaw.ads; */ public class AVLSearchTree> extends SortedBinaryTree { private boolean balanced(TreeNode node) { - if(Math.abs(calcHeight(node.right) - calcHeight(node.left)) > 1 - || (node.right != null && !balanced(node.right)) - || (node.left != null && !balanced(node.left))){ + if(node == null){ + return true; + } else if (Math.abs(calcHeight(node.right) - calcHeight(node.left)) > 1 +// || (node.right != null && !balanced(node.right)) +// || (node.left != null && !balanced(node.left)) + ){ return false; } else { return true; @@ -23,15 +26,11 @@ public class AVLSearchTree> extends SortedBinaryTree @Override protected int calcSize(TreeNode p) { - // Implement (6.2) - int sizeLeft = 0, sizeRight = 0; - if(p.left != null){ - sizeLeft = calcSize(p.left); + if (p == null) { + return 0; + } else { + return p.values.size() + calcSize(p.left) + calcSize(p.right); } - if(p.right != null) { - sizeRight = calcSize(p.right); - } - return sizeLeft + sizeRight + 1; } /** @@ -140,7 +139,10 @@ public class AVLSearchTree> extends SortedBinaryTree // search right node.right = removeAt(node.right, x); } - // TODO Implement (6.5) + // Implement (6.5) + if(!balanced(node)){ + node = balance(node); + } return node; } }