Skip to content

Commit 0ad926b

Browse files
style: fix Checkstyle violations in AVLTree package
1 parent da84c5f commit 0ad926b

File tree

3 files changed

+36
-53
lines changed

3 files changed

+36
-53
lines changed

src/test/java/com/thealgorithms/tree/AVLtree/AVLDelete.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
11
package com.thealgorithms.tree.AVLtree;
22

3-
4-
53
public class AVLDelete extends AVLInsert {
64

75
Node minValueNode(Node node) {
86
Node current = node;
9-
while (current.left != null)
7+
while (current.left != null) {
108
current = current.left;
9+
}
1110
return current;
1211
}
1312

1413
Node deleteNode(Node root, int key) {
15-
if (root == null)
14+
if (root == null) {
1615
return root;
16+
}
1717

18-
if (key < root.key)
18+
if (key < root.key) {
1919
root.left = deleteNode(root.left, key);
20-
else if (key > root.key)
20+
} else if (key > root.key) {
2121
root.right = deleteNode(root.right, key);
22-
else {
22+
} else {
2323
if ((root.left == null) || (root.right == null)) {
24-
Node temp = null;
25-
if (root.left != null)
26-
temp = root.left;
27-
else
28-
temp = root.right;
29-
24+
Node temp = (root.left != null) ? root.left : root.right;
3025
if (temp == null) {
3126
root = null;
3227
} else {
@@ -39,23 +34,30 @@ else if (key > root.key)
3934
}
4035
}
4136

42-
if (root == null)
37+
if (root == null) {
4338
return root;
39+
}
4440

4541
root.height = Math.max(height(root.left), height(root.right)) + 1;
4642
int balance = getBalance(root);
4743

48-
if (balance > 1 && getBalance(root.left) >= 0)
44+
// Left Left Case
45+
if (balance > 1 && getBalance(root.left) >= 0) {
4946
return rightRotate(root);
47+
}
5048

49+
// Left Right Case
5150
if (balance > 1 && getBalance(root.left) < 0) {
5251
root.left = leftRotate(root.left);
5352
return rightRotate(root);
5453
}
5554

56-
if (balance < -1 && getBalance(root.right) <= 0)
55+
// Right Right Case
56+
if (balance < -1 && getBalance(root.right) <= 0) {
5757
return leftRotate(root);
58+
}
5859

60+
// Right Left Case
5961
if (balance < -1 && getBalance(root.right) > 0) {
6062
root.right = rightRotate(root.right);
6163
return leftRotate(root);
@@ -76,13 +78,12 @@ public static void main(String[] args) {
7678
tree.root = tree.insert(tree.root, 1);
7779
tree.root = tree.insert(tree.root, 2);
7880

79-
System.out.println("Preorder before deletion:");
81+
System.out.println("Preorder traversal before deletion:");
8082
tree.preOrder(tree.root);
8183

8284
tree.root = tree.deleteNode(tree.root, 10);
8385

84-
System.out.println("\nPreorder after deletion:");
86+
System.out.println("\nPreorder traversal after deletion:");
8587
tree.preOrder(tree.root);
8688
}
8789
}
88-

src/test/java/com/thealgorithms/tree/AVLtree/CheckBalance.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33

44

5+
;
6+
57
public class CheckBalance extends AVLInsert {
68

79
boolean isBalanced(Node node) {
8-
if (node == null)
10+
if (node == null) {
911
return true;
12+
}
1013

1114
int balance = getBalance(node);
12-
if (Math.abs(balance) > 1)
15+
if (Math.abs(balance) > 1) {
1316
return false;
17+
}
1418

1519
return isBalanced(node.left) && isBalanced(node.right);
1620
}
@@ -23,8 +27,8 @@ public static void main(String[] args) {
2327

2428
System.out.println("Is AVL Tree balanced? " + tree.isBalanced(tree.root));
2529

26-
tree.root.left.left = new Node(5); // Manually unbalance it
30+
// Manually unbalance the tree
31+
tree.root.left.left = new Node(5);
2732
System.out.println("Is AVL Tree balanced after modification? " + tree.isBalanced(tree.root));
2833
}
2934
}
30-
Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,16 @@
11
package com.thealgorithms.tree.AVLtree;
22

3-
4-
53
public class Rotations extends AVLInsert {
64

7-
public Node rightRotate(Node y) {
8-
Node x = y.left;
9-
Node T2 = x.right;
10-
x.right = y;
11-
y.left = T2;
12-
y.height = Math.max(height(y.left), height(y.right)) + 1;
13-
x.height = Math.max(height(x.left), height(x.right)) + 1;
14-
return x;
15-
}
16-
17-
public Node leftRotate(Node x) {
18-
Node y = x.right;
19-
Node T2 = y.left;
20-
y.left = x;
21-
x.right = T2;
22-
x.height = Math.max(height(x.left), height(x.right)) + 1;
23-
y.height = Math.max(height(y.left), height(y.right)) + 1;
24-
return y;
25-
}
26-
275
public static void main(String[] args) {
28-
Rotations r = new Rotations();
29-
Node root = new Node(30);
30-
root.left = new Node(20);
31-
root.left.left = new Node(10);
6+
Rotations tree = new Rotations();
7+
tree.root = tree.insert(tree.root, 30);
8+
tree.root = tree.insert(tree.root, 20);
9+
tree.root = tree.insert(tree.root, 10);
3210

33-
System.out.println("Performing right rotation on 30...");
34-
root = r.rightRotate(root);
35-
System.out.println("New root after rotation: " + root.key);
11+
System.out.println("Performing right rotation...");
12+
tree.root = tree.rightRotate(tree.root);
13+
14+
System.out.println("New root after rotation: " + tree.root.key);
3615
}
3716
}
38-

0 commit comments

Comments
 (0)