Skip to content

Commit 3dc424d

Browse files
🚴 perf(delete_one_child): Skip case 1 since n cannot be the root.
1 parent d145aa2 commit 3dc424d

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/deletion/delete_one_child.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ import Node from '../types/Node.js';
55
import Leaf from '../types/Leaf.js';
66

77
import replace_node from './replace_node.js';
8-
import delete_case1 from './delete_case1.js';
8+
import delete_case2 from './delete_case2.js';
99

1010
/**
1111
* Delete a node <code>n</code> that has at most a single non-leaf child.
1212
*
1313
* Precondition:
1414
* - n has at most one non-leaf child.
15+
* - n is not the root
1516
* - if n has a non-leaf child, then it is its left child.
1617
* - hence, n's right child is a leaf
1718
*
1819
* @param {Node} n - The node to delete.
1920
*/
2021
const delete_one_child = (n) => {
2122
assert(n instanceof Node);
23+
assert(n.parent !== null);
2224
// Precondition: n's right child is a leaf.
2325
// The right child of n is always a LEAF because either n is a subtree
2426
// predecessor or it is the only child of its parent by the red-black tree
@@ -39,7 +41,7 @@ const delete_one_child = (n) => {
3941
if (child._color === RED) child._color = BLACK;
4042
// Otherwise, there are more things to fix.
4143
else {
42-
delete_case1(child);
44+
delete_case2(child);
4345
}
4446
} else {
4547
// If n is red then its child can only be black. Replacing n with its

0 commit comments

Comments
 (0)