File tree 4 files changed +20
-20
lines changed
4 files changed +20
-20
lines changed Original file line number Diff line number Diff line change @@ -26,27 +26,27 @@ const delete_case1 = (n) => {
26
26
const s = sibling ( n ) ;
27
27
assert ( s instanceof Node ) ;
28
28
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
+ */
41
44
n . parent . _color = RED ;
42
45
s . _color = BLACK ;
43
46
if ( n === n . parent . left ) rotate_left ( n . parent ) ;
44
47
else rotate_right ( n . parent ) ;
45
48
delete_case3 ( n ) ;
46
49
}
47
-
48
- // Otherwise, go to case 3.
49
- else delete_case2 ( n ) ;
50
50
} ;
51
51
52
52
export default delete_case1 ;
Original file line number Diff line number Diff line change @@ -46,7 +46,7 @@ const delete_case3 = (n) => {
46
46
*/
47
47
if (
48
48
// The parent color test is always true when coming from case 2
49
- n . parent . _color === RED &&
49
+ n . parent . _color !== BLACK &&
50
50
( s . left === null || s . left . _color === BLACK ) &&
51
51
( s . right === null || s . right . _color === BLACK )
52
52
) {
Original file line number Diff line number Diff line change @@ -23,13 +23,12 @@ const delete_no_child = (n) => {
23
23
assert ( n . left === null ) ;
24
24
assert ( n . right === null ) ;
25
25
26
- if ( n . _color === RED ) {
26
+ if ( n . _color !== BLACK ) {
27
+ assert ( n . _color === RED ) ;
27
28
prune ( n ) ;
28
29
return ;
29
30
}
30
31
31
- assert ( n . _color === BLACK ) ;
32
-
33
32
// Mock leaf since there is no left child
34
33
// We use key = n.key to avoid mixing types, but this property is never
35
34
// accessed.
Original file line number Diff line number Diff line change @@ -39,7 +39,8 @@ const insert_case2 = (n) => {
39
39
* - - - -
40
40
*/
41
41
42
- if ( u !== null && u . _color === RED ) {
42
+ if ( u !== null && u . _color !== BLACK ) {
43
+ assert ( u . _color === RED ) ;
43
44
n . parent . _color = BLACK ;
44
45
u . _color = BLACK ;
45
46
const g = grandparent ( n ) ;
You can’t perform that action at this time.
0 commit comments