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' ;
9
13
10
14
/**
11
15
* A RedBlackTree with key-only nodes.
12
16
*
13
17
*/
14
18
export class RedBlackTree {
15
-
16
19
/**
17
20
* Constructs a new empty red-black tree.
18
21
*
19
22
* @param {Function } compare - The comparison function for node keys.
20
23
* @returns {RedBlackTree }
21
24
*/
22
- constructor ( compare ) {
23
-
24
- this . compare = compare ;
25
- this . root = null ;
26
-
25
+ constructor ( compare ) {
26
+ this . compare = compare ;
27
+ this . root = null ;
27
28
}
28
29
29
30
/**
30
31
* Adds a key to the tree.
31
32
*
32
33
* @param {Key } key - The key to add.
33
34
*/
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 ) ;
42
42
}
43
43
}
44
44
@@ -50,9 +50,9 @@ export class RedBlackTree {
50
50
* @param {Key } key - The input key.
51
51
* @returns {Node }
52
52
*/
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 ) ;
56
56
}
57
57
58
58
/**
@@ -63,9 +63,9 @@ export class RedBlackTree {
63
63
* @param {Key } key - The input key.
64
64
* @returns {Key }
65
65
*/
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 ;
69
69
}
70
70
71
71
/**
@@ -75,47 +75,39 @@ export class RedBlackTree {
75
75
* @param {Key } key - The input key.
76
76
* @returns {Boolean }
77
77
*/
78
- has ( key ) {
79
- return this . _search ( key ) !== null ;
78
+ has ( key ) {
79
+ return this . _search ( key ) !== null ;
80
80
}
81
81
82
82
/**
83
83
* Deletes the input node from the tree.
84
84
*
85
85
* @param {Node } node - The input node to delete.
86
86
*/
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
94
93
// note: this node can only have one non-leaf child
95
94
// 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
101
98
// If there is no left child, then there can only be one right
102
99
// 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
106
103
// note: this node can only have one non-leaf child
107
104
// 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 ) ;
117
110
}
118
-
119
111
}
120
112
121
113
/**
@@ -127,14 +119,12 @@ export class RedBlackTree {
127
119
* @param {Key } key - The input key.
128
120
* @returns {Boolean } - Whether the key existed in the tree before removal.
129
121
*/
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 ;
137
125
126
+ this . _delete ( node ) ;
127
+ return true ;
138
128
}
139
129
140
130
/**
@@ -144,30 +134,25 @@ export class RedBlackTree {
144
134
* @param {Key } right - The right bound of the interval.
145
135
* @returns {Iterator }
146
136
*/
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 ) ;
151
140
}
152
141
153
142
/**
154
143
* Returns an in order iterator over the keys of the tree.
155
144
*
156
145
* @returns {Iterator }
157
146
*/
158
- * items ( ) {
159
-
160
- if ( this . root !== null ) yield * inordertraversal ( this . root ) ;
161
-
147
+ * items ( ) {
148
+ if ( this . root !== null ) yield * inordertraversal ( this . root ) ;
162
149
}
163
150
164
151
/**
165
152
* Same as {@link RedBlackTree#items}.
166
153
*/
167
- [ Symbol . iterator ] ( ) {
168
-
169
- return this . items ( ) ;
170
-
154
+ [ Symbol . iterator ] ( ) {
155
+ return this . items ( ) ;
171
156
}
172
157
173
158
/**
@@ -177,14 +162,11 @@ export class RedBlackTree {
177
162
* @param {Iterbale } iterable - The input iterable.
178
163
* @returns {RedBlackTree }
179
164
*/
180
- static from ( compare , iterable ) {
165
+ static from ( compare , iterable ) {
166
+ const tree = new RedBlackTree ( compare ) ;
181
167
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 ) ;
187
169
170
+ return tree ;
188
171
}
189
-
190
172
}
0 commit comments