1-
2- import { Node } from "./Node.js" ;
3- import { RED , BLACK } from '../index.js' ;
4- import { predecessor } from '../index.js' ;
5- import { insert , insert_case2 } from '../index.js' ;
6- import { delete_one_child } from '../index.js' ;
7- import { search } from '../index.js' ;
8- import { inordertraversal , rangetraversal } from '../index.js' ;
1+ import { Node } from './Node.js' ;
2+ import {
3+ RED ,
4+ BLACK ,
5+ predecessor ,
6+ insert ,
7+ insert_case2 ,
8+ delete_one_child ,
9+ search ,
10+ inordertraversal ,
11+ rangetraversal ,
12+ } from '../index.js' ;
913
1014/**
1115 * A RedBlackTree with key-only nodes.
1216 *
1317 */
1418export class RedBlackTree {
15-
1619 /**
1720 * Constructs a new empty red-black tree.
1821 *
1922 * @param {Function } compare - The comparison function for node keys.
2023 * @returns {RedBlackTree }
2124 */
22- constructor ( compare ) {
23-
24- this . compare = compare ;
25- this . root = null ;
26-
25+ constructor ( compare ) {
26+ this . compare = compare ;
27+ this . root = null ;
2728 }
2829
2930 /**
3031 * Adds a key to the tree.
3132 *
3233 * @param {Key } key - The key to add.
3334 */
34- add ( key ) {
35- if ( this . root === null ) {
36- this . root = new Node ( BLACK , key ) ;
37- }
38- else {
39- const node = new Node ( RED , key ) ;
40- insert ( this . compare , this . root , node ) ;
41- insert_case2 ( node ) ;
35+ add ( key ) {
36+ if ( this . root === null ) {
37+ this . root = new Node ( BLACK , key ) ;
38+ } else {
39+ const node = new Node ( RED , key ) ;
40+ insert ( this . compare , this . root , node ) ;
41+ insert_case2 ( node ) ;
4242 }
4343 }
4444
@@ -50,9 +50,9 @@ export class RedBlackTree {
5050 * @param {Key } key - The input key.
5151 * @returns {Node }
5252 */
53- _search ( key ) {
54- if ( this . root === null ) return null ;
55- return search ( this . compare , this . root , key ) ;
53+ _search ( key ) {
54+ if ( this . root === null ) return null ;
55+ return search ( this . compare , this . root , key ) ;
5656 }
5757
5858 /**
@@ -63,9 +63,9 @@ export class RedBlackTree {
6363 * @param {Key } key - The input key.
6464 * @returns {Key }
6565 */
66- get ( key ) {
67- const node = this . _search ( key ) ;
68- return node === null ? null : node . key ;
66+ get ( key ) {
67+ const node = this . _search ( key ) ;
68+ return node === null ? null : node . key ;
6969 }
7070
7171 /**
@@ -75,47 +75,39 @@ export class RedBlackTree {
7575 * @param {Key } key - The input key.
7676 * @returns {Boolean }
7777 */
78- has ( key ) {
79- return this . _search ( key ) !== null ;
78+ has ( key ) {
79+ return this . _search ( key ) !== null ;
8080 }
8181
8282 /**
8383 * Deletes the input node from the tree.
8484 *
8585 * @param {Node } node - The input node to delete.
8686 */
87- _delete ( node ) {
88-
89- if ( ! node . left . isleaf ( ) ) {
90- // replace node's key with predecessor's key
91- const pred = predecessor ( node ) ;
92- node . key = pred . key ;
93- // delete predecessor node
87+ _delete ( node ) {
88+ if ( ! node . left . isleaf ( ) ) {
89+ // Replace node's key with predecessor's key
90+ const pred = predecessor ( node ) ;
91+ node . key = pred . key ;
92+ // Delete predecessor node
9493 // note: this node can only have one non-leaf child
9594 // because the tree is a red-black tree
96- delete_one_child ( pred ) ;
97- }
98-
99- else if ( ! node . right . isleaf ( ) ) {
100- // replace node's key with successor's key
95+ delete_one_child ( pred ) ;
96+ } else if ( ! node . right . isleaf ( ) ) {
97+ // Replace node's key with successor's key
10198 // If there is no left child, then there can only be one right
10299 // child.
103- const succ = node . right ;
104- node . key = succ . key ;
105- // delete successor node
100+ const succ = node . right ;
101+ node . key = succ . key ;
102+ // Delete successor node
106103 // note: this node can only have one non-leaf child
107104 // because the tree is a red-black tree
108- delete_one_child ( succ ) ;
109- }
110-
111- else if ( node === this . root ) {
112- this . root = null ;
113- }
114-
115- else {
116- delete_one_child ( node ) ;
105+ delete_one_child ( succ ) ;
106+ } else if ( node === this . root ) {
107+ this . root = null ;
108+ } else {
109+ delete_one_child ( node ) ;
117110 }
118-
119111 }
120112
121113 /**
@@ -127,14 +119,12 @@ export class RedBlackTree {
127119 * @param {Key } key - The input key.
128120 * @returns {Boolean } - Whether the key existed in the tree before removal.
129121 */
130- remove ( key ) {
131-
132- const node = this . _search ( key ) ;
133- if ( node === null ) return false ;
134-
135- this . _delete ( node ) ;
136- return true ;
122+ remove ( key ) {
123+ const node = this . _search ( key ) ;
124+ if ( node === null ) return false ;
137125
126+ this . _delete ( node ) ;
127+ return true ;
138128 }
139129
140130 /**
@@ -144,30 +134,25 @@ export class RedBlackTree {
144134 * @param {Key } right - The right bound of the interval.
145135 * @returns {Iterator }
146136 */
147- * range ( left , right ) {
148-
149- if ( this . root !== null ) yield * rangetraversal ( this . compare , this . root , left , right ) ;
150-
137+ * range ( left , right ) {
138+ if ( this . root !== null )
139+ yield * rangetraversal ( this . compare , this . root , left , right ) ;
151140 }
152141
153142 /**
154143 * Returns an in order iterator over the keys of the tree.
155144 *
156145 * @returns {Iterator }
157146 */
158- * items ( ) {
159-
160- if ( this . root !== null ) yield * inordertraversal ( this . root ) ;
161-
147+ * items ( ) {
148+ if ( this . root !== null ) yield * inordertraversal ( this . root ) ;
162149 }
163150
164151 /**
165152 * Same as {@link RedBlackTree#items}.
166153 */
167- [ Symbol . iterator ] ( ) {
168-
169- return this . items ( ) ;
170-
154+ [ Symbol . iterator ] ( ) {
155+ return this . items ( ) ;
171156 }
172157
173158 /**
@@ -177,14 +162,11 @@ export class RedBlackTree {
177162 * @param {Iterbale } iterable - The input iterable.
178163 * @returns {RedBlackTree }
179164 */
180- static from ( compare , iterable ) {
165+ static from ( compare , iterable ) {
166+ const tree = new RedBlackTree ( compare ) ;
181167
182- const tree = new RedBlackTree ( compare ) ;
183-
184- for ( const element of iterable ) tree . add ( element ) ;
185-
186- return tree ;
168+ for ( const element of iterable ) tree . add ( element ) ;
187169
170+ return tree ;
188171 }
189-
190172}
0 commit comments