From dbf0186898cf9a383bb20121e8c782a708a1ae37 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 25 Oct 2022 19:45:21 +0200 Subject: [PATCH] solved Task 5 --- src/ch/zhaw/ads/AVLSearchTree.java | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) 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; } }