solved Task 2

This commit is contained in:
schrom01 2022-10-25 15:23:27 +02:00
parent 78f5531067
commit 2c27dd7629
1 changed files with 16 additions and 5 deletions

View File

@ -18,7 +18,15 @@ public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T>
@Override
protected int calcSize(TreeNode<T> 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<T extends Comparable<T>> extends SortedBinaryTree<T>
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;