Skip to content

Commit d4573c4

Browse files
♻️ refactor: Compare to 0 when possible.
This fixes #106.
1 parent 936c1f8 commit d4573c4

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

src/deletion/delete_case1.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,27 @@ const delete_case1 = (n) => {
2626
const s = sibling(n);
2727
assert(s instanceof Node);
2828

29-
/**
30-
* If n's sibling is red, prepare for and go to case 4.
31-
*
32-
* B B
33-
* / \ / \
34-
* >B R R B
35-
* / \ / \ --> / \ / \
36-
* - - B B >B B = =
37-
* / \ / \ / \ / \
38-
* = = = = - - = =
39-
*/
40-
if (s._color === RED) {
29+
if (s._color === BLACK) {
30+
// If n's sibling is BLACK, go to case 3.
31+
delete_case2(n);
32+
} else {
33+
/**
34+
* Otherwise, prepare for and go to case 4.
35+
*
36+
* B B
37+
* / \ / \
38+
* >B R R B
39+
* / \ / \ --> / \ / \
40+
* - - B B >B B = =
41+
* / \ / \ / \ / \
42+
* = = = = - - = =
43+
*/
4144
n.parent._color = RED;
4245
s._color = BLACK;
4346
if (n === n.parent.left) rotate_left(n.parent);
4447
else rotate_right(n.parent);
4548
delete_case3(n);
4649
}
47-
48-
// Otherwise, go to case 3.
49-
else delete_case2(n);
5050
};
5151

5252
export default delete_case1;

src/deletion/delete_case3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const delete_case3 = (n) => {
4646
*/
4747
if (
4848
// The parent color test is always true when coming from case 2
49-
n.parent._color === RED &&
49+
n.parent._color !== BLACK &&
5050
(s.left === null || s.left._color === BLACK) &&
5151
(s.right === null || s.right._color === BLACK)
5252
) {

src/deletion/delete_no_child.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ const delete_no_child = (n) => {
2323
assert(n.left === null);
2424
assert(n.right === null);
2525

26-
if (n._color === RED) {
26+
if (n._color !== BLACK) {
27+
assert(n._color === RED);
2728
prune(n);
2829
return;
2930
}
3031

31-
assert(n._color === BLACK);
32-
3332
// Mock leaf since there is no left child
3433
// We use key = n.key to avoid mixing types, but this property is never
3534
// accessed.

src/insertion/insert_case2.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ const insert_case2 = (n) => {
3939
* - - - -
4040
*/
4141

42-
if (u !== null && u._color === RED) {
42+
if (u !== null && u._color !== BLACK) {
43+
assert(u._color === RED);
4344
n.parent._color = BLACK;
4445
u._color = BLACK;
4546
const g = grandparent(n);

0 commit comments

Comments
 (0)