@@ -2,68 +2,50 @@ import assert from 'assert';
22import BLACK from '../color/BLACK.js' ;
33import RED from '../color/RED.js' ;
44import Node from '../types/Node.js' ;
5- import Leaf from '../types/Leaf.js' ;
65
76import replace_node from './replace_node.js' ;
87import delete_case2 from './delete_case2.js' ;
98
10- import prune_subtree from './prune_subtree.js' ;
11-
129/**
13- * Delete a node <code>n</code> that has at most a single non-leaf child.
10+ * Delete a node <code>n</code> with one non-leaf left child and one leaf right
11+ * child.
1412 *
1513 * Precondition:
16- * - n has at most one non-leaf child.
14+ * - n has exactly one non-leaf child.
1715 * - n is not the root
18- * - if n has a non-leaf child, then it is its left child.
16+ * - n's only non-leaf child is n's left child.
1917 * - hence, n's right child is a leaf
2018 *
2119 * @param {Node } n - The node to delete.
2220 */
2321const delete_one_child = ( n ) => {
2422 assert ( n instanceof Node ) ;
2523 assert ( n . parent !== null ) ;
26- // Precondition: n's right child is a leaf.
27- // The right child of n is always a LEAF because either n is a subtree
28- // predecessor or it is the only child of its parent by the red-black tree
29- // properties
24+ assert ( n . left instanceof Node ) ;
3025 assert ( n . right === null ) ;
3126
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-
48- // Mock leaf if there is no left child
49- const child = n . left === null ? new Leaf ( null ) : n . left ;
27+ const child = n . left ;
5028
51- // Replace n with its left child
29+ // Replace n with its only child
5230 replace_node ( n , child ) ;
5331
54- // If n is black, deleting it reduces the black-height of every path going
55- // through it by 1.
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 ) ;
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 ) ;
6348 }
64-
65- // Delete mocked leaf
66- if ( child instanceof Leaf ) prune_subtree ( child ) ;
6749} ;
6850
6951export default delete_one_child ;
0 commit comments