Skip to content

Commit 6275064

Browse files
Added AVL tree implementation with detailed comments
1 parent 646b998 commit 6275064

File tree

4 files changed

+20
-16
lines changed

4 files changed

+20
-16
lines changed

src/main/java/com/thealgorithms/tree/AVL.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ private static class Node {
1414

1515
Node(int key) {
1616
this.key = key;
17-
left = right = null;
1817
this.height = 1;
18+
this.left = null;
19+
this.right = null;
1920
}
2021
}
2122

@@ -170,10 +171,10 @@ private Node findMinNode(Node node) {
170171

171172
private Node rightRotate(Node y) {
172173
Node x = y.left;
173-
Node T2 = x.right;
174+
Node t2 = x.right;
174175

175176
x.right = y;
176-
y.left = T2;
177+
y.left = t2;
177178

178179
updateHeight(y);
179180
updateHeight(x);
@@ -183,10 +184,10 @@ private Node rightRotate(Node y) {
183184

184185
private Node leftRotate(Node x) {
185186
Node y = x.right;
186-
Node T2 = y.left;
187+
Node t2 = y.left;
187188

188189
y.left = x;
189-
x.right = T2;
190+
x.right = t2;
190191

191192
updateHeight(x);
192193
updateHeight(y);

src/main/java/com/thealgorithms/tree/BST.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ public class BST {
88

99
private static class Node {
1010
int key;
11-
Node left, right;
11+
Node left;
12+
Node right;
1213

1314
Node(int key) {
1415
this.key = key;

src/test/java/com/thealgorithms/tree/AVLTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ public class AVLTest {
99
private static class Node {
1010
int key;
1111
int height;
12-
Node left, right;
12+
Node left;
13+
Node right;
1314

1415
Node(int key) {
1516
this.key = key;
16-
this.height = 1; // new node is initially added at leaf
17+
this.height = 1;
1718
this.left = null;
1819
this.right = null;
1920
}
@@ -168,10 +169,10 @@ private Node findMinNode(Node node) {
168169

169170
private Node rightRotate(Node y) {
170171
Node x = y.left;
171-
Node T2 = x.right;
172+
Node t2 = x.right;
172173

173174
x.right = y;
174-
y.left = T2;
175+
y.left = t2;
175176

176177
updateHeight(y);
177178
updateHeight(x);
@@ -181,10 +182,10 @@ private Node rightRotate(Node y) {
181182

182183
private Node leftRotate(Node x) {
183184
Node y = x.right;
184-
Node T2 = y.left;
185+
Node t2 = y.left;
185186

186187
y.left = x;
187-
x.right = T2;
188+
x.right = t2;
188189

189190
updateHeight(x);
190191
updateHeight(y);

src/test/java/com/thealgorithms/tree/BSTTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ public class BSTTest {
88

99
private static class Node {
1010
int key;
11-
Node left, right;
11+
Node left;
12+
Node right;
1213

1314
Node(int key) {
1415
this.key = key;
15-
left = right = null;
16-
}
17-
}
16+
this.left = null;
17+
this.right = null;
18+
} }
1819

1920
private Node root;
2021

0 commit comments

Comments
 (0)