solved Task 2
This commit is contained in:
parent
78f5531067
commit
2c27dd7629
|
@ -18,7 +18,15 @@ public class AVLSearchTree<T extends Comparable<T>> extends SortedBinaryTree<T>
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int calcSize(TreeNode<T> p) {
|
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;
|
return null;
|
||||||
} else if (height(p.left) - height(p.right) == 2) {
|
} else if (height(p.left) - height(p.right) == 2) {
|
||||||
if (height(p.left.left) >= height(p.left.right)) {
|
if (height(p.left.left) >= height(p.left.right)) {
|
||||||
// TODO Implement (6.2)
|
// Implement (6.2)
|
||||||
|
p = rotateR(p);
|
||||||
} else {
|
} else {
|
||||||
// TODO Implement (6.2)
|
p = rotateLR(p);
|
||||||
}
|
}
|
||||||
} else if (height(p.right) - height(p.left) == 2) {
|
} else if (height(p.right) - height(p.left) == 2) {
|
||||||
if (height(p.right.right) >= height(p.right.left)) {
|
if (height(p.right.right) >= height(p.right.left)) {
|
||||||
// TODO Implement (6.2)
|
// Implement (6.2)
|
||||||
|
p = rotateL(p);
|
||||||
} else {
|
} else {
|
||||||
// TODO Implement (6.2)
|
// Implement (6.2)
|
||||||
|
p = rotateRL(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.height = Math.max(height(p.left), height(p.right)) + 1;
|
p.height = Math.max(height(p.left), height(p.right)) + 1;
|
||||||
|
|
Loading…
Reference in New Issue