Skip to content

Commit 1b488c4

Browse files
♻️ refactor: Assert input children are black in insert case analysis.
1 parent 5b951bb commit 1b488c4

File tree

5 files changed

+16
-0
lines changed

5 files changed

+16
-0
lines changed

src/insertion/insert_case1.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import insert_case2 from './insert_case2.js';
77
/**
88
* Preconditions:
99
* - n is red.
10+
* - n's children are BLACK
1011
*
1112
* @param {Node} n - The input node.
1213
*/
1314
const insert_case1 = (n) => {
1415
assert(n instanceof Node);
1516
assert(n._color === RED);
17+
assert(n.left._color === BLACK);
18+
assert(n.right._color === BLACK);
1619
/**
1720
* If n is the root of the tree, paint it black and we are done.
1821
*

src/insertion/insert_case2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import insert_case3 from './insert_case3.js';
77
/**
88
* Preconditions:
99
* - n is red.
10+
* - n's children are BLACK
1011
* - n is not the root of the tree.
1112
*
1213
* @param {Node} n - The input node.
1314
*/
1415
const insert_case2 = (n) => {
1516
assert(n instanceof Node);
1617
assert(n._color === RED);
18+
assert(n.left._color === BLACK);
19+
assert(n.right._color === BLACK);
1720
assert(n.parent !== null);
1821

1922
/**

src/insertion/insert_case3.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import insert_case4 from './insert_case4.js';
1010
/**
1111
* Preconditions:
1212
* - n is red.
13+
* - n's children are BLACK
1314
* - n is not the root of the tree.
1415
* - n's parent is red.
1516
*
@@ -18,6 +19,8 @@ import insert_case4 from './insert_case4.js';
1819
const insert_case3 = (n) => {
1920
assert(n instanceof Node);
2021
assert(n._color === RED);
22+
assert(n.left._color === BLACK);
23+
assert(n.right._color === BLACK);
2124
assert(n.parent !== null);
2225
assert(n.parent._color === RED);
2326
const u = uncle(n);

src/insertion/insert_case4.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import assert from 'assert';
22
import Node from '../adt/Node.js';
3+
import BLACK from '../color/BLACK.js';
34
import RED from '../color/RED.js';
45
import rotate_left from '../rotate/rotate_left.js';
56
import rotate_right from '../rotate/rotate_right.js';
@@ -9,6 +10,7 @@ import insert_case5 from './insert_case5.js';
910
/**
1011
* Preconditions:
1112
* - n is red.
13+
* - n's children are BLACK
1214
* - n is not the root of the tree.
1315
* - n's parent is red.
1416
* - n's uncle is black.
@@ -20,6 +22,8 @@ import insert_case5 from './insert_case5.js';
2022
const insert_case4 = (n) => {
2123
assert(n instanceof Node);
2224
assert(n._color === RED);
25+
assert(n.left._color === BLACK);
26+
assert(n.right._color === BLACK);
2327
assert(n.parent !== null);
2428
assert(n.parent._color === RED);
2529
const g = grandparent(n);

src/insertion/insert_case5.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import grandparent from '../family/grandparent.js';
99
/**
1010
* Preconditions:
1111
* - n is red.
12+
* - n's children are BLACK
1213
* - n is not the root of the tree.
1314
* - n's parent is red.
1415
* - n's uncle is black.
@@ -19,6 +20,8 @@ import grandparent from '../family/grandparent.js';
1920
const insert_case5 = (n) => {
2021
assert(n instanceof Node);
2122
assert(n._color === RED);
23+
assert(n.left._color === BLACK);
24+
assert(n.right._color === BLACK);
2225
assert(n.parent !== null);
2326
assert(n.parent._color === RED);
2427
const g = grandparent(n);

0 commit comments

Comments
 (0)