1
+ import assert from 'assert' ;
1
2
import { Node } from './Node.js' ;
2
3
import { BLACK , RED } from '../color/index.js' ;
3
4
import { predecessor } from '../family/predecessor.js' ;
@@ -15,10 +16,11 @@ export class RedBlackTree {
15
16
* Constructs a new empty red-black tree.
16
17
*
17
18
* @param {Function } compare - The comparison function for node keys.
18
- * @returns {RedBlackTree }
19
19
*/
20
20
constructor ( compare ) {
21
+ /** @member {Function} The comparison function for node keys. */
21
22
this . compare = compare ;
23
+ /** @member {Node} The root of the tree. */
22
24
this . root = null ;
23
25
}
24
26
@@ -34,7 +36,7 @@ export class RedBlackTree {
34
36
/**
35
37
* Adds a key to the tree.
36
38
*
37
- * @param {Key } key - The key to add.
39
+ * @param {any } key - The key to add.
38
40
*/
39
41
add ( key ) {
40
42
if ( this . root === null ) {
@@ -51,7 +53,7 @@ export class RedBlackTree {
51
53
* Returns the first node whose key equals the input key.
52
54
* If no such node exists, returns <code>null</code>.
53
55
*
54
- * @param {Key } key - The input key.
56
+ * @param {any } key - The input key.
55
57
* @returns {Node }
56
58
*/
57
59
_search ( key ) {
@@ -64,8 +66,8 @@ export class RedBlackTree {
64
66
* in this way (with {@link RedBlackTree#_search}. If no such key exists
65
67
* in the tree, returns <code>null</code>.
66
68
*
67
- * @param {Key } key - The input key.
68
- * @returns {Key }
69
+ * @param {any } key - The input key.
70
+ * @returns {any }
69
71
*/
70
72
get ( key ) {
71
73
const node = this . _search ( key ) ;
@@ -76,7 +78,7 @@ export class RedBlackTree {
76
78
* Returns <code>true</code> if and only if the tree contains the input
77
79
* key.
78
80
*
79
- * @param {Key } key - The input key.
81
+ * @param {any } key - The input key.
80
82
* @returns {Boolean }
81
83
*/
82
84
has ( key ) {
@@ -102,6 +104,7 @@ export class RedBlackTree {
102
104
// If there is no left child, then there can only be one right
103
105
// child.
104
106
const succ = node . right ;
107
+ assert ( succ instanceof Node ) ;
105
108
node . key = succ . key ;
106
109
// Delete successor node
107
110
// note: this node can only have one non-leaf child
@@ -120,7 +123,7 @@ export class RedBlackTree {
120
123
* (with {@link RedBlackTree#_delete}). If such a node is found and deleted
121
124
* then return <code>true</code>. Return <code>false</code> otherwise.
122
125
*
123
- * @param {Key } key - The input key.
126
+ * @param {any } key - The input key.
124
127
* @returns {Boolean } - Whether the key existed in the tree before removal.
125
128
*/
126
129
remove ( key ) {
@@ -134,9 +137,9 @@ export class RedBlackTree {
134
137
/**
135
138
* Returns an in order iterator over the keys of the tree that lie in the
136
139
* interval [left, right[.
137
- * @param {Key } left - The left bound of the interval.
138
- * @param {Key } right - The right bound of the interval.
139
- * @returns {Iterator }
140
+ * @param {any } left - The left bound of the interval.
141
+ * @param {any } right - The right bound of the interval.
142
+ * @returns {IterableIterator }
140
143
*/
141
144
* range ( left , right ) {
142
145
if ( this . root !== null )
@@ -146,7 +149,7 @@ export class RedBlackTree {
146
149
/**
147
150
* Returns an in order iterator over the keys of the tree.
148
151
*
149
- * @returns {Iterator }
152
+ * @returns {IterableIterator }
150
153
*/
151
154
* items ( ) {
152
155
if ( this . root !== null ) yield * inordertraversal ( this . root ) ;
@@ -163,7 +166,7 @@ export class RedBlackTree {
163
166
* Constructs a red-black tree from an input iterable.
164
167
*
165
168
* @param {Function } compare - The comparison function to use.
166
- * @param {Iterbale } iterable - The input iterable.
169
+ * @param {Iterable } iterable - The input iterable.
167
170
* @returns {RedBlackTree }
168
171
*/
169
172
static from ( compare , iterable ) {
0 commit comments