@@ -7,7 +7,7 @@ import Leaf from '../types/Leaf.js';
7
7
import replace_node from './replace_node.js' ;
8
8
import delete_case2 from './delete_case2.js' ;
9
9
10
- import delete_mocked_leaf from './delete_mocked_leaf .js' ;
10
+ import prune_subtree from './prune_subtree .js' ;
11
11
12
12
/**
13
13
* Delete a node <code>n</code> that has at most a single non-leaf child.
@@ -29,32 +29,41 @@ const delete_one_child = (n) => {
29
29
// properties
30
30
assert ( n . right === null ) ;
31
31
32
+ if ( n . _color === RED ) {
33
+ // If n is red then its child can only be black. Replacing n with its
34
+ // child suffices.
35
+ const child = n . left ;
36
+ if ( child === null ) {
37
+ prune_subtree ( n ) ;
38
+ } else {
39
+ assert ( child . _color === BLACK ) ;
40
+ replace_node ( n , child ) ;
41
+ }
42
+
43
+ return ;
44
+ }
45
+
46
+ assert ( n . _color === BLACK ) ;
47
+
32
48
// Mock leaf if there is no left child
33
49
const child = n . left === null ? new Leaf ( null ) : n . left ;
34
50
35
- // TODO skip creating mocked leaf if n._color === RED
36
-
37
51
// Replace n with its left child
38
52
replace_node ( n , child ) ;
39
53
40
54
// If n is black, deleting it reduces the black-height of every path going
41
55
// through it by 1.
42
- if ( n . _color === BLACK ) {
43
- // We can easily fix this when its left child is an
44
- // internal red node: change the color of the left child to black and
45
- // replace n with it.
46
- if ( child . _color === RED ) child . _color = BLACK ;
47
- // Otherwise, there are more things to fix.
48
- else {
49
- delete_case2 ( child ) ;
50
- }
51
- } else {
52
- // If n is red then its child can only be black. Replacing n with its
53
- // child suffices. This is a NO-OP.
54
- assert ( child . _color === BLACK ) ;
56
+ // We can easily fix this when its left child is an
57
+ // internal red node: change the color of the left child to black and
58
+ // replace n with it.
59
+ if ( child . _color === RED ) child . _color = BLACK ;
60
+ // Otherwise, there are more things to fix.
61
+ else {
62
+ delete_case2 ( child ) ;
55
63
}
56
64
57
- if ( child instanceof Leaf ) delete_mocked_leaf ( child ) ;
65
+ // Delete mocked leaf
66
+ if ( child instanceof Leaf ) prune_subtree ( child ) ;
58
67
} ;
59
68
60
69
export default delete_one_child ;
0 commit comments