Skip to content

Commit 5756bc9

Browse files
author
Justin Wetherell
committed
Renamed a variable to better represent what is happening in the balance after insert method
1 parent ba855ff commit 5756bc9

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/com/jwetherell/algorithms/data_structures/AVLTree.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,18 @@ protected Node<T> addValue(T id) {
8181
private void balanceAfterInsert(AVLNode<T> node) {
8282
int balanceFactor = node.getBalanceFactor();
8383
if (balanceFactor > 1 || balanceFactor < -1) {
84-
AVLNode<T> parent = (AVLNode<T>) node.parent;
84+
AVLNode<T> child = null;
8585
Balance balance = null;
8686
if (balanceFactor < 0) {
87-
parent = (AVLNode<T>) node.lesser;
88-
balanceFactor = parent.getBalanceFactor();
87+
child = (AVLNode<T>) node.lesser;
88+
balanceFactor = child.getBalanceFactor();
8989
if (balanceFactor < 0)
9090
balance = Balance.LEFT_LEFT;
9191
else
9292
balance = Balance.LEFT_RIGHT;
9393
} else {
94-
parent = (AVLNode<T>) node.greater;
95-
balanceFactor = parent.getBalanceFactor();
94+
child = (AVLNode<T>) node.greater;
95+
balanceFactor = child.getBalanceFactor();
9696
if (balanceFactor < 0)
9797
balance = Balance.RIGHT_LEFT;
9898
else
@@ -101,11 +101,11 @@ private void balanceAfterInsert(AVLNode<T> node) {
101101

102102
if (balance == Balance.LEFT_RIGHT) {
103103
// Left-Right (Left rotation, right rotation)
104-
rotateLeft(parent);
104+
rotateLeft(child);
105105
rotateRight(node);
106106
} else if (balance == Balance.RIGHT_LEFT) {
107107
// Right-Left (Right rotation, left rotation)
108-
rotateRight(parent);
108+
rotateRight(child);
109109
rotateLeft(node);
110110
} else if (balance == Balance.LEFT_LEFT) {
111111
// Left-Left (Right rotation)
@@ -115,8 +115,8 @@ private void balanceAfterInsert(AVLNode<T> node) {
115115
rotateLeft(node);
116116
}
117117

118+
child.updateHeight();
118119
node.updateHeight();
119-
parent.updateHeight();
120120
}
121121
}
122122

0 commit comments

Comments
 (0)