@@ -4,7 +4,6 @@ import RED from '../color/RED.js';
44import Node from '../types/Node.js' ;
55
66import replace_node from './replace_node.js' ;
7- import delete_case2 from './delete_case2.js' ;
87
98/**
109 * 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';
1514 * - n is not the root
1615 * - n's only non-leaf child is n's left child.
1716 * - hence, n's right child is a leaf
17+ * - hence, n's left child is RED
18+ * - hence, n is BLACK
1819 *
1920 * @param {Node } n - The node to delete.
2021 */
2122const delete_one_child = ( n ) => {
2223 assert ( n instanceof Node ) ;
24+ assert ( n . _color === BLACK ) ;
2325 assert ( n . parent !== null ) ;
2426 assert ( n . left instanceof Node ) ;
27+ assert ( n . left . _color === RED ) ;
2528 assert ( n . right === null ) ;
2629
2730 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.
3036 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 ;
4938} ;
5039
5140export default delete_one_child ;
0 commit comments