1+ import assert from 'assert' ;
12import { Node } from './Node.js' ;
23import { BLACK , RED } from '../color/index.js' ;
34import { predecessor } from '../family/predecessor.js' ;
@@ -15,10 +16,11 @@ export class RedBlackTree {
1516 * Constructs a new empty red-black tree.
1617 *
1718 * @param {Function } compare - The comparison function for node keys.
18- * @returns {RedBlackTree }
1919 */
2020 constructor ( compare ) {
21+ /** @member {Function} The comparison function for node keys. */
2122 this . compare = compare ;
23+ /** @member {Node} The root of the tree. */
2224 this . root = null ;
2325 }
2426
@@ -34,7 +36,7 @@ export class RedBlackTree {
3436 /**
3537 * Adds a key to the tree.
3638 *
37- * @param {Key } key - The key to add.
39+ * @param {any } key - The key to add.
3840 */
3941 add ( key ) {
4042 if ( this . root === null ) {
@@ -51,7 +53,7 @@ export class RedBlackTree {
5153 * Returns the first node whose key equals the input key.
5254 * If no such node exists, returns <code>null</code>.
5355 *
54- * @param {Key } key - The input key.
56+ * @param {any } key - The input key.
5557 * @returns {Node }
5658 */
5759 _search ( key ) {
@@ -64,8 +66,8 @@ export class RedBlackTree {
6466 * in this way (with {@link RedBlackTree#_search}. If no such key exists
6567 * in the tree, returns <code>null</code>.
6668 *
67- * @param {Key } key - The input key.
68- * @returns {Key }
69+ * @param {any } key - The input key.
70+ * @returns {any }
6971 */
7072 get ( key ) {
7173 const node = this . _search ( key ) ;
@@ -76,7 +78,7 @@ export class RedBlackTree {
7678 * Returns <code>true</code> if and only if the tree contains the input
7779 * key.
7880 *
79- * @param {Key } key - The input key.
81+ * @param {any } key - The input key.
8082 * @returns {Boolean }
8183 */
8284 has ( key ) {
@@ -102,6 +104,7 @@ export class RedBlackTree {
102104 // If there is no left child, then there can only be one right
103105 // child.
104106 const succ = node . right ;
107+ assert ( succ instanceof Node ) ;
105108 node . key = succ . key ;
106109 // Delete successor node
107110 // note: this node can only have one non-leaf child
@@ -120,7 +123,7 @@ export class RedBlackTree {
120123 * (with {@link RedBlackTree#_delete}). If such a node is found and deleted
121124 * then return <code>true</code>. Return <code>false</code> otherwise.
122125 *
123- * @param {Key } key - The input key.
126+ * @param {any } key - The input key.
124127 * @returns {Boolean } - Whether the key existed in the tree before removal.
125128 */
126129 remove ( key ) {
@@ -134,9 +137,9 @@ export class RedBlackTree {
134137 /**
135138 * Returns an in order iterator over the keys of the tree that lie in the
136139 * 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 }
140143 */
141144 * range ( left , right ) {
142145 if ( this . root !== null )
@@ -146,7 +149,7 @@ export class RedBlackTree {
146149 /**
147150 * Returns an in order iterator over the keys of the tree.
148151 *
149- * @returns {Iterator }
152+ * @returns {IterableIterator }
150153 */
151154 * items ( ) {
152155 if ( this . root !== null ) yield * inordertraversal ( this . root ) ;
@@ -163,7 +166,7 @@ export class RedBlackTree {
163166 * Constructs a red-black tree from an input iterable.
164167 *
165168 * @param {Function } compare - The comparison function to use.
166- * @param {Iterbale } iterable - The input iterable.
169+ * @param {Iterable } iterable - The input iterable.
167170 * @returns {RedBlackTree }
168171 */
169172 static from ( compare , iterable ) {
0 commit comments