Skip to content

Commit 10c9286

Browse files
make-github-pseudonymous-againAurélien Ooms
authored and
Aurélien Ooms
committed
♻️ refactor: Remove usage of class Leaf.
This avoids using multiple Hidden Classes and enables inline caching. Fixes #107.
1 parent 90fe493 commit 10c9286

11 files changed

+20
-45
lines changed

src/deletion/delete_case2.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from 'assert';
22
import BLACK from '../color/BLACK.js';
33
import RED from '../color/RED.js';
44
import Node from '../types/Node.js';
5-
import Leaf from '../types/Leaf.js';
65
import rotate_left from '../rotate/rotate_left.js';
76
import rotate_right from '../rotate/rotate_right.js';
87
import 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
*/
2221
const 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

src/deletion/delete_case3.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from 'assert';
22
import BLACK from '../color/BLACK.js';
33
import RED from '../color/RED.js';
44
import Node from '../types/Node.js';
5-
import Leaf from '../types/Leaf.js';
65
import sibling from '../family/sibling.js';
76

87
import 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
*/
2120
const 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);

src/deletion/delete_case4.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from 'assert';
22
import BLACK from '../color/BLACK.js';
33
import RED from '../color/RED.js';
44
import Node from '../types/Node.js';
5-
import Leaf from '../types/Leaf.js';
65
import sibling from '../family/sibling.js';
76

87
import 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
*/
2120
const 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);

src/deletion/delete_case5.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from 'assert';
22
import BLACK from '../color/BLACK.js';
33
import RED from '../color/RED.js';
44
import Node from '../types/Node.js';
5-
import Leaf from '../types/Leaf.js';
65
import rotate_left from '../rotate/rotate_left.js';
76
import rotate_right from '../rotate/rotate_right.js';
87
import 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
*/
2322
const 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);

src/deletion/delete_case6.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from 'assert';
22
import BLACK from '../color/BLACK.js';
33
import RED from '../color/RED.js';
44
import Node from '../types/Node.js';
5-
import Leaf from '../types/Leaf.js';
65
import rotate_left from '../rotate/rotate_left.js';
76
import rotate_right from '../rotate/rotate_right.js';
87
import 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
*/
2221
const 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);

src/deletion/delete_no_child.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import assert from 'assert';
22
import BLACK from '../color/BLACK.js';
33
import RED from '../color/RED.js';
44
import Node from '../types/Node.js';
5-
import Leaf from '../types/Leaf.js';
65

76
import replace_node from './replace_node.js';
87
import 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

src/deletion/prune.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import assert from 'assert';
22
import 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
*/
109
const 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;

src/deletion/replace_node.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import assert from 'assert';
22
import 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
*/
1110
const 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

src/family/sibling.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import assert from 'assert';
22
import 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
*/
1110
const 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

src/index.js

-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ export {default as rotate_right} from './rotate/rotate_right.js';
2828
export {default as search} from './search/search.js';
2929
export {default as inordertraversal} from './traversal/inordertraversal.js';
3030
export {default as rangetraversal} from './traversal/rangetraversal.js';
31-
export {default as Leaf} from './types/Leaf.js';
3231
export {default as Node} from './types/Node.js';
3332
export {default as RedBlackTree} from './types/RedBlackTree.js';

src/types/Leaf.js

-17
This file was deleted.

0 commit comments

Comments
 (0)