Skip to content

Commit 5b951bb

Browse files
💥 refactor!: Simplify uncle computation.
We allow to return a leaf since the only place we use it is insert_case3 where this allows to skip an unnecessary null check. BREAKING CHANGE: This breaks dependents relying on previous behaviour.
1 parent ac66199 commit 5b951bb

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

src/family/uncle.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
import assert from 'assert';
22
import Node from '../adt/Node.js';
3+
import Leaf from '../adt/Leaf.js';
34
import grandparent from './grandparent.js';
45

56
/**
67
* Computes the uncle of the input node when the grandparent is guaranteed to
78
* exist.
89
*
910
* @param {Node} node - The input node.
10-
* @returns {Node}
11+
* @returns {Node|Leaf}
1112
*/
1213
const uncle = (node) => {
1314
assert(node instanceof Node);
1415
const g = grandparent(node);
1516
assert(g !== null);
16-
if (node.parent === g.left) {
17-
if (g.right.isLeaf()) return null;
18-
assert(g.right instanceof Node);
19-
return g.right;
20-
}
21-
22-
if (g.left.isLeaf()) return null;
23-
assert(g.left instanceof Node);
24-
return g.left;
17+
const u = node.parent === g.left ? g.right : g.left;
18+
assert(u instanceof Node || u instanceof Leaf);
19+
return u;
2520
};
2621

2722
export default uncle;

src/insertion/insert_case3.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const insert_case3 = (n) => {
3636
* - - - -
3737
*/
3838

39-
if (u !== null && u._color === RED) {
39+
if (u._color === RED) {
4040
n.parent._color = BLACK;
4141
u._color = BLACK;
4242
const g = grandparent(n);

0 commit comments

Comments
 (0)