From 2c27dd762944362092573c4a6b7aa1a22109f108 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Tue, 25 Oct 2022 15:23:27 +0200 Subject: [PATCH] solved Task 2 --- src/ch/zhaw/ads/AVLSearchTree.java | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/ch/zhaw/ads/AVLSearchTree.java b/src/ch/zhaw/ads/AVLSearchTree.java index ec63de5..6a6441f 100644 --- a/src/ch/zhaw/ads/AVLSearchTree.java +++ b/src/ch/zhaw/ads/AVLSearchTree.java @@ -18,7 +18,15 @@ public class AVLSearchTree> extends SortedBinaryTree @Override protected int calcSize(TreeNode p) { - // TODO Implement (6.2) + // Implement (6.2) + int sizeLeft = 0, sizeRight = 0; + if(p.left != null){ + sizeLeft = calcSize(p.left); + } + if(p.right != null) { + sizeRight = calcSize(p.right); + } + return sizeLeft + sizeRight + 1; } /** @@ -41,15 +49,18 @@ public class AVLSearchTree> extends SortedBinaryTree return null; } else if (height(p.left) - height(p.right) == 2) { if (height(p.left.left) >= height(p.left.right)) { - // TODO Implement (6.2) + // Implement (6.2) + p = rotateR(p); } else { - // TODO Implement (6.2) + p = rotateLR(p); } } else if (height(p.right) - height(p.left) == 2) { if (height(p.right.right) >= height(p.right.left)) { - // TODO Implement (6.2) + // Implement (6.2) + p = rotateL(p); } else { - // TODO Implement (6.2) + // Implement (6.2) + p = rotateRL(p); } } p.height = Math.max(height(p.left), height(p.right)) + 1;