@@ -4,7 +4,6 @@ import RED from '../color/RED.js';
4
4
import Node from '../types/Node.js' ;
5
5
6
6
import replace_node from './replace_node.js' ;
7
- import delete_case2 from './delete_case2.js' ;
8
7
9
8
/**
10
9
* Delete a node <code>n</code> with one non-leaf left child and one leaf right
@@ -15,37 +14,27 @@ import delete_case2 from './delete_case2.js';
15
14
* - n is not the root
16
15
* - n's only non-leaf child is n's left child.
17
16
* - hence, n's right child is a leaf
17
+ * - hence, n's left child is RED
18
+ * - hence, n is BLACK
18
19
*
19
20
* @param {Node } n - The node to delete.
20
21
*/
21
22
const delete_one_child = ( n ) => {
22
23
assert ( n instanceof Node ) ;
24
+ assert ( n . _color === BLACK ) ;
23
25
assert ( n . parent !== null ) ;
24
26
assert ( n . left instanceof Node ) ;
27
+ assert ( n . left . _color === RED ) ;
25
28
assert ( n . right === null ) ;
26
29
27
30
const child = n . left ;
28
-
29
- // Replace n with its only child
31
+ // If n is black, deleting it reduces the black-height of every path going
32
+ // through it by 1.
33
+ // We can easily fix this when its only child is an
34
+ // internal RED node: change the color of the child to black and
35
+ // replace n with it.
30
36
replace_node ( n , child ) ;
31
-
32
- if ( n . _color === BLACK ) {
33
- // If n is black, deleting it reduces the black-height of every path going
34
- // through it by 1.
35
- // We can easily fix this when its only child is an
36
- // internal red node: change the color of the child to black and
37
- // replace n with it.
38
- if ( child . _color === RED ) child . _color = BLACK ;
39
- // Otherwise, there are more things to fix.
40
- else {
41
- delete_case2 ( child ) ;
42
- }
43
- } else {
44
- assert ( n . _color === RED ) ;
45
- // If n is red then its child can only be black. Replacing n with its
46
- // child suffices. This has already been done above.
47
- assert ( child . _color === BLACK ) ;
48
- }
37
+ child . _color = BLACK ;
49
38
} ;
50
39
51
40
export default delete_one_child ;
0 commit comments