Skip to content

Commit ecfd1f0

Browse files
committed
Prefer casting over unnecessary if conditions
1 parent 819ac7b commit ecfd1f0

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/avlTree.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,24 +90,24 @@ export class AvlTree<K, V> implements AvlTreeApi<K, V> {
9090
root.height = Math.max(root.leftHeight, root.rightHeight) + 1;
9191
const balanceState = this._getBalanceState(root);
9292

93-
if (root.left && balanceState === BalanceState.UNBALANCED_LEFT) {
94-
if (this._compare(key, root.left.key) < 0) {
93+
if (balanceState === BalanceState.UNBALANCED_LEFT) {
94+
if (this._compare(key, (<Node<K, V>>root.left).key) < 0) {
9595
// Left left case
9696
root = root.rotateRight();
9797
} else {
9898
// Left right case
99-
root.left = root.left.rotateLeft();
99+
root.left = (<Node<K, V>>root.left).rotateLeft();
100100
return root.rotateRight();
101101
}
102102
}
103103

104-
if (root.right && balanceState === BalanceState.UNBALANCED_RIGHT) {
105-
if (this._compare(key, root.right.key) > 0) {
104+
if (balanceState === BalanceState.UNBALANCED_RIGHT) {
105+
if (this._compare(key, (<Node<K, V>>root.right).key) > 0) {
106106
// Right right case
107107
root = root.rotateLeft();
108108
} else {
109109
// Right left case
110-
root.right = root.right.rotateRight();
110+
root.right = (<Node<K, V>>root.right).rotateRight();
111111
return root.rotateLeft();
112112
}
113113
}
@@ -168,27 +168,27 @@ export class AvlTree<K, V> implements AvlTreeApi<K, V> {
168168
root.height = Math.max(root.leftHeight, root.rightHeight) + 1;
169169
const balanceState = this._getBalanceState(root);
170170

171-
if (root.left && balanceState === BalanceState.UNBALANCED_LEFT) {
171+
if (balanceState === BalanceState.UNBALANCED_LEFT) {
172172
// Left left case
173-
if (this._getBalanceState(root.left) === BalanceState.BALANCED ||
174-
this._getBalanceState(root.left) === BalanceState.SLIGHTLY_UNBALANCED_LEFT) {
173+
if (this._getBalanceState((<Node<K, V>>root.left)) === BalanceState.BALANCED ||
174+
this._getBalanceState((<Node<K, V>>root.left)) === BalanceState.SLIGHTLY_UNBALANCED_LEFT) {
175175
return root.rotateRight();
176176
}
177177
// Left right case
178178
// this._getBalanceState(root.left) === BalanceState.SLIGHTLY_UNBALANCED_RIGHT
179-
root.left = root.left.rotateLeft();
179+
root.left = (<Node<K, V>>root.left).rotateLeft();
180180
return root.rotateRight();
181181
}
182182

183-
if (root.right && balanceState === BalanceState.UNBALANCED_RIGHT) {
183+
if (balanceState === BalanceState.UNBALANCED_RIGHT) {
184184
// Right right case
185-
if (this._getBalanceState(root.right) === BalanceState.BALANCED ||
186-
this._getBalanceState(root.right) === BalanceState.SLIGHTLY_UNBALANCED_RIGHT) {
185+
if (this._getBalanceState((<Node<K, V>>root.right)) === BalanceState.BALANCED ||
186+
this._getBalanceState((<Node<K, V>>root.right)) === BalanceState.SLIGHTLY_UNBALANCED_RIGHT) {
187187
return root.rotateLeft();
188188
}
189189
// Right left case
190190
// this._getBalanceState(root.right) === BalanceState.SLIGHTLY_UNBALANCED_LEFT
191-
root.right = root.right.rotateRight();
191+
root.right = (<Node<K, V>>root.right).rotateRight();
192192
return root.rotateLeft();
193193
}
194194

0 commit comments

Comments
 (0)