File tree Expand file tree Collapse file tree 11 files changed +20
-45
lines changed Expand file tree Collapse file tree 11 files changed +20
-45
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ 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' ;
65import rotate_left from '../rotate/rotate_left.js' ;
76import rotate_right from '../rotate/rotate_right.js' ;
87import sibling from '../family/sibling.js' ;
@@ -17,10 +16,10 @@ import delete_case4 from './delete_case4.js';
1716 * - all other root-leaf paths have a black height of b
1817 * - n is not the root
1918 *
20- * @param {Node|Leaf } n - The input node.
19+ * @param {Node } n - The input node.
2120 */
2221const delete_case2 = ( n ) => {
23- assert ( n instanceof Node || n instanceof Leaf ) ;
22+ assert ( n instanceof Node ) ;
2423 assert ( n . _color === BLACK ) ;
2524 assert ( n . parent !== null ) ;
2625
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ 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' ;
65import sibling from '../family/sibling.js' ;
76
87import delete_case1 from './delete_case1.js' ;
@@ -16,10 +15,10 @@ import delete_case4 from './delete_case4.js';
1615 * - n is not the root
1716 * - n's sibling is black
1817 *
19- * @param {Node|Leaf } n - The input node.
18+ * @param {Node } n - The input node.
2019 */
2120const delete_case3 = ( n ) => {
22- assert ( n instanceof Node || n instanceof Leaf ) ;
21+ assert ( n instanceof Node ) ;
2322 assert ( n . _color === BLACK ) ;
2423 assert ( n . parent !== null ) ;
2524 const s = sibling ( n ) ;
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ 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' ;
65import sibling from '../family/sibling.js' ;
76
87import delete_case5 from './delete_case5.js' ;
@@ -16,10 +15,10 @@ import delete_case5 from './delete_case5.js';
1615 * - n's sibling is black
1716 * - n's parent and n's sibling's children cannot all be black
1817 *
19- * @param {Node|Leaf } n - The input node.
18+ * @param {Node } n - The input node.
2019 */
2120const delete_case4 = ( n ) => {
22- assert ( n instanceof Node || n instanceof Leaf ) ;
21+ assert ( n instanceof Node ) ;
2322 assert ( n . _color === BLACK ) ;
2423 assert ( n . parent !== null ) ;
2524 const s = sibling ( n ) ;
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ 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' ;
65import rotate_left from '../rotate/rotate_left.js' ;
76import rotate_right from '../rotate/rotate_right.js' ;
87import sibling from '../family/sibling.js' ;
@@ -18,10 +17,10 @@ import delete_case6 from './delete_case6.js';
1817 * - n's sibling is black
1918 * - at least one of n's sibling's children is red
2019 *
21- * @param {Node|Leaf } n - The input node.
20+ * @param {Node } n - The input node.
2221 */
2322const delete_case5 = ( n ) => {
24- assert ( n instanceof Node || n instanceof Leaf ) ;
23+ assert ( n instanceof Node ) ;
2524 assert ( n . _color === BLACK ) ;
2625 assert ( n . parent !== null ) ;
2726 const s = sibling ( n ) ;
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ 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' ;
65import rotate_left from '../rotate/rotate_left.js' ;
76import rotate_right from '../rotate/rotate_right.js' ;
87import sibling from '../family/sibling.js' ;
@@ -17,10 +16,10 @@ import sibling from '../family/sibling.js';
1716 * - if n is a left child, the right child of n's sibling is red
1817 * - if n is a right child, the left child of n's sibling is red
1918 *
20- * @param {Node|Leaf } n - The input node.
19+ * @param {Node } n - The input node.
2120 */
2221const delete_case6 = ( n ) => {
23- assert ( n instanceof Node || n instanceof Leaf ) ;
22+ assert ( n instanceof Node ) ;
2423 assert ( n . _color === BLACK ) ;
2524 assert ( n . parent !== null ) ;
2625 const s = sibling ( n ) ;
Original file line number Diff line number Diff line change @@ -2,7 +2,6 @@ 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' ;
@@ -32,13 +31,15 @@ const delete_no_child = (n) => {
3231 assert ( n . _color === BLACK ) ;
3332
3433 // Mock leaf since there is no left child
35- const leaf = new Leaf ( null ) ;
34+ // We use key = n.key to avoid mixing types, but this property is never
35+ // accessed.
36+ const leaf = new Node ( BLACK , n . key ) ;
3637
3738 // Replace n with the mocked leaf
3839 replace_node ( n , leaf ) ;
3940
4041 // If n is black, deleting it reduces the black-height of every path going
41- // through it by 1. Leaf is black, so there are more things to fix.
42+ // through it by 1. The leaf is black, so there are more things to fix.
4243 delete_case2 ( leaf ) ;
4344
4445 // Delete mocked leaf
Original file line number Diff line number Diff line change 11import assert from 'assert' ;
22import Node from '../types/Node.js' ;
3- import Leaf from '../types/Leaf.js' ;
43
54/**
65 * Prune subtree rooted at input node.
76 *
8- * @param {Node|Leaf } root - The leaf to delete .
7+ * @param {Node } root - The root of the subtree to prune .
98 */
109const prune = ( root ) => {
11- assert ( root instanceof Node || root instanceof Leaf ) ;
10+ assert ( root instanceof Node ) ;
1211 assert ( root . parent !== null ) ;
1312
1413 if ( root === root . parent . left ) root . parent . left = null ;
Original file line number Diff line number Diff line change 11import assert from 'assert' ;
22import Node from '../types/Node.js' ;
3- import Leaf from '../types/Leaf.js' ;
43
54/**
65 * Replaces node <code>A</code> by node <code>B</code>.
76 *
87 * @param {Node } A - The node to replace.
9- * @param {Node|Leaf } B - The replacement node.
8+ * @param {Node } B - The replacement node.
109 */
1110const replace_node = ( A , B ) => {
1211 assert ( A instanceof Node ) ;
13- assert ( B instanceof Node || B instanceof Leaf ) ;
12+ assert ( B instanceof Node ) ;
1413 // We never apply delete_one_child or delete_no_child on the root
1514 assert ( A . parent !== null ) ;
1615
Original file line number Diff line number Diff line change 11import assert from 'assert' ;
22import Node from '../types/Node.js' ;
3- import Leaf from '../types/Leaf.js' ;
43
54/**
65 * Computes the sibling of the input node.
76 *
8- * @param {Node|Leaf } node - The input node.
7+ * @param {Node } node - The input node.
98 * @returns {Node }
109 */
1110const sibling = ( node ) => {
12- assert ( node instanceof Node || node instanceof Leaf ) ;
11+ assert ( node instanceof Node ) ;
1312 // We only use this function when node HAS a non-leaf sibling.
1413 assert ( node . parent !== null ) ;
1514
Original file line number Diff line number Diff line change @@ -28,6 +28,5 @@ export {default as rotate_right} from './rotate/rotate_right.js';
2828export { default as search } from './search/search.js' ;
2929export { default as inordertraversal } from './traversal/inordertraversal.js' ;
3030export { default as rangetraversal } from './traversal/rangetraversal.js' ;
31- export { default as Leaf } from './types/Leaf.js' ;
3231export { default as Node } from './types/Node.js' ;
3332export { default as RedBlackTree } from './types/RedBlackTree.js' ;
You can’t perform that action at this time.
0 commit comments