@@ -7,7 +7,7 @@ import Leaf from '../types/Leaf.js';
77import replace_node from './replace_node.js' ;
88import delete_case2 from './delete_case2.js' ;
99
10- import delete_mocked_leaf from './delete_mocked_leaf .js' ;
10+ import prune_subtree from './prune_subtree .js' ;
1111
1212/**
1313 * Delete a node <code>n</code> that has at most a single non-leaf child.
@@ -29,32 +29,41 @@ const delete_one_child = (n) => {
2929 // properties
3030 assert ( n . right === null ) ;
3131
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+
3248 // Mock leaf if there is no left child
3349 const child = n . left === null ? new Leaf ( null ) : n . left ;
3450
35- // TODO skip creating mocked leaf if n._color === RED
36-
3751 // Replace n with its left child
3852 replace_node ( n , child ) ;
3953
4054 // If n is black, deleting it reduces the black-height of every path going
4155 // 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 ) ;
5563 }
5664
57- if ( child instanceof Leaf ) delete_mocked_leaf ( child ) ;
65+ // Delete mocked leaf
66+ if ( child instanceof Leaf ) prune_subtree ( child ) ;
5867} ;
5968
6069export default delete_one_child ;
0 commit comments