@@ -84,10 +84,14 @@ class SortedQueue<V, K = any> {
84
84
85
85
}
86
86
87
- remove ( node : SortedQueueNode < V , K > ) : SortedQueueNode < V , K > {
87
+ remove ( node : SortedQueueNode < V , K > ) : SortedQueueNode < V , K > {
88
88
// this can remove in O(log_2(n))
89
89
90
- if ( ! ( node && 'val' in node ) ) {
90
+ if ( ! node ) {
91
+ return null ;
92
+ }
93
+
94
+ if ( ! ( node && 'val' in node ) ) {
91
95
throw new Error ( 'node is not defined, or wrong type.' )
92
96
}
93
97
@@ -102,8 +106,8 @@ class SortedQueue<V, K = any> {
102
106
return parent ;
103
107
} ;
104
108
105
- if ( ! parent ) {
106
- if ( this . rootNode !== node ) {
109
+ if ( ! parent ) {
110
+ if ( this . rootNode !== node ) {
107
111
throw new Error ( 'root node should be parent.' )
108
112
}
109
113
}
@@ -112,12 +116,14 @@ class SortedQueue<V, K = any> {
112
116
113
117
if ( ! node . left && ! node . right ) {
114
118
119
+ throw 'neither!'
120
+
115
121
if ( ! parent ) {
116
122
// root node...
117
123
this . head = null ;
118
124
this . tail = null ;
119
125
this . rootNode = null ;
120
- return parent ;
126
+ return makeRet ( ) ;
121
127
}
122
128
123
129
if ( parent . left === node ) {
@@ -133,19 +139,24 @@ class SortedQueue<V, K = any> {
133
139
this . tail = parent ;
134
140
}
135
141
}
136
- return parent ;
142
+ return makeRet ( ) ;
137
143
}
138
144
139
145
if ( ! node . left ) {
140
146
147
+ throw 'left'
148
+
141
149
if ( isTail ) {
142
150
throw new Error ( 'should not be tail if node.right is defined.' ) ;
143
151
}
144
152
145
- if ( isHead ) {
153
+ if ( isHead ) {
146
154
this . head = node . right ;
147
155
}
148
156
157
+ // assign even if parent is null
158
+ node . right . parent = parent ;
159
+
149
160
if ( ! parent ) {
150
161
this . rootNode = node . right ;
151
162
return parent ;
@@ -159,19 +170,24 @@ class SortedQueue<V, K = any> {
159
170
throw new Error ( 'neither left or right hmmm' )
160
171
}
161
172
162
- return parent ;
173
+ return makeRet ( ) ;
163
174
}
164
175
165
176
if ( ! node . right ) {
166
177
178
+ throw 'right'
179
+
167
180
if ( isHead ) {
168
181
throw new Error ( 'if left is defined, node should not be head.' )
169
182
}
170
183
171
- if ( isTail ) {
184
+ if ( isTail ) {
172
185
this . tail = node . left ;
173
186
}
174
187
188
+ // assign even if parent is null
189
+ node . left . parent = parent ;
190
+
175
191
if ( ! parent ) {
176
192
this . rootNode = node . left ;
177
193
return parent ;
@@ -185,52 +201,78 @@ class SortedQueue<V, K = any> {
185
201
throw new Error ( 'neither left nor right hmmm.' )
186
202
}
187
203
188
- return parent ;
204
+ return makeRet ( ) ;
189
205
}
190
206
191
-
192
- if ( ! ( node . left && node . right ) ) {
207
+ if ( ! ( node . left && node . right ) ) {
193
208
throw new Error ( 'both left and right should be defined.' )
194
209
}
195
210
196
- if ( ! node . left . right ) {
197
- throw 'foo111 '
211
+ if ( ! node . left . right ) {
212
+ throw 'boof right '
198
213
node . left . right = node . right ;
199
- return parent ;
214
+ node . left . parent = parent ; // ok if parent is null
215
+ if ( node . parent ) {
216
+ if ( node . parent . right === node ) {
217
+ node . parent . right = node . left ;
218
+ } else if ( node . parent . left === node ) {
219
+ node . parent . left = node . left ;
220
+ } else {
221
+ throw new Error ( 'missing children.' )
222
+ }
223
+ }
224
+ return makeRet ( ) ;
200
225
}
201
226
202
- if ( ! node . right . left ) {
203
- throw 'bar222 '
227
+ if ( ! node . right . left ) {
228
+ // throw 'boof left '
204
229
node . right . left = node . left ;
205
- return parent ;
230
+ node . right . parent = parent ; // ok if parent is null
231
+
232
+ if ( node . parent ) {
233
+ if ( node . parent . right === node ) {
234
+ node . parent . right = node . right ;
235
+ } else if ( node . parent . right === node ) {
236
+ node . parent . right = node . right ;
237
+ } else {
238
+ throw new Error ( 'missing children.' ) ;
239
+ }
240
+ }
241
+
242
+ return makeRet ( ) ;
206
243
}
207
244
208
245
const rightMost = this . findLargestGivenVal ( node . left ) ;
209
246
const leftMost = this . findSmallestGivenVal ( node . right ) ;
210
247
211
- if ( ! ( rightMost && leftMost ) ) {
248
+ if ( ! ( rightMost && leftMost ) ) {
212
249
throw new Error ( 'both of these should be defined!' )
213
250
}
214
251
215
252
216
- if ( true ) {
253
+ if ( true ) {
217
254
218
255
rightMost . parent . right = rightMost . left ;
256
+
257
+ if ( rightMost . left ) {
258
+ rightMost . left . parent = rightMost . parent
259
+ }
260
+
219
261
rightMost . left = node . left ;
220
262
rightMost . right = node . right ;
221
263
rightMost . parent = parent ;
222
264
223
- if ( ! parent ) {
265
+ if ( ! parent ) {
224
266
this . rootNode = rightMost ;
225
- return parent ;
267
+ return makeRet ( ) ;
226
268
}
227
269
228
- if ( parent . left === node ) {
270
+ if ( parent . left === node ) {
229
271
parent . left = rightMost ;
230
- }
231
-
232
- if ( parent . right === node ) {
272
+ } else if ( parent . right === node ) {
233
273
parent . right = rightMost ;
274
+ } else {
275
+ throw new Error ( 'this is unexpected.' )
234
276
}
235
277
236
278
} else {
@@ -240,7 +282,7 @@ class SortedQueue<V, K = any> {
240
282
}
241
283
242
284
243
- return parent ;
285
+ return makeRet ( ) ;
244
286
245
287
246
288
}
@@ -326,20 +368,28 @@ class SortedQueue<V, K = any> {
326
368
327
369
}
328
370
329
- logInOrder ( node : SortedQueueNode < any , any > ) {
371
+ logInOrder ( node : SortedQueueNode < any , any > , count = { val : 0 } ) {
330
372
331
373
if ( node === null ) {
332
374
return ;
333
375
}
334
376
335
377
if ( node . left ) {
336
- this . logInOrder ( node . left ) ;
378
+ if ( node . left . parent !== node ) {
379
+ node . left . parent = node ;
380
+ // throw new Error('this is wrong / left.');
381
+ }
382
+ this . logInOrder ( node . left , count ) ;
337
383
}
338
384
339
- console . log ( node . val ) ;
385
+ console . log ( count . val ++ , node . val ) ;
340
386
341
387
if ( node . right ) {
342
- this . logInOrder ( node . right ) ;
388
+ if ( node . right . parent !== node ) {
389
+ node . right . parent = node ;
390
+ // throw new Error('this is wrong / right.');
391
+ }
392
+ this . logInOrder ( node . right , count ) ;
343
393
}
344
394
}
345
395
@@ -379,7 +429,7 @@ class SortedQueue<V, K = any> {
379
429
380
430
}
381
431
382
- findSmallestValFromRoot ( ) {
432
+ findSmallestValFromRoot ( ) : SortedQueueNode < V , K > {
383
433
let currentNode = this . rootNode ;
384
434
385
435
while ( currentNode . left ) {
@@ -465,9 +515,9 @@ class SortedQueue<V, K = any> {
465
515
if ( n . parent . right && n . parent . right === n ) {
466
516
n = n . parent ;
467
517
} else {
468
- if ( n . parent . left !== n ) {
469
- throw new Error ( 'oddd' ) ;
470
- }
518
+ // if (n.parent.left !== n) {
519
+ // throw new Error('oddd');
520
+ // }
471
521
return n . parent ;
472
522
}
473
523
@@ -872,20 +922,34 @@ for (const v of vals) {
872
922
// sq.findNextLargest(sq.findNextLargest(sq.findNextLargest(sq.findNextLargest()))).val
873
923
// );
874
924
925
+ console . log ( '///////' ) ;
926
+ sq . logInOrder ( sq . rootNode ) ;
927
+ console . log ( '////////' ) ;
928
+
875
929
const x = sq . remove ( sq . rootNode ) ;
930
+ const x2 = sq . remove ( sq . rootNode ) ;
931
+ const x3 = sq . remove ( sq . rootNode . right ?. left ) ;
932
+ const x4 = sq . remove ( sq . rootNode . left ?. right ) ;
876
933
console . log ( { x} ) ;
877
934
console . log ( 'new root-node val:' , sq . rootNode . val ) ;
878
- console . log ( 'new root-node left val:' , sq . rootNode . left . val ) ;
879
- console . log ( 'new root-node right val:' , sq . rootNode . right . val ) ;
880
- sq . logInOrder ( sq . rootNode ) ;
881
935
882
- throw 'bar'
936
+ if ( sq . rootNode . left ) {
937
+ console . log ( 'new root-node left val:' , sq . rootNode . left . val ) ;
938
+ }
939
+
940
+ if ( sq . rootNode . right ) {
941
+ console . log ( 'new root-node right val:' , sq . rootNode . right . val ) ;
942
+ }
943
+
944
+ console . log ( '///////' )
945
+ sq . logInOrder ( sq . rootNode ) ;
946
+ console . log ( '////////' )
883
947
884
948
let count = 0 ;
885
- let smallest = sq . findSmallestValFromRoot ( ) ;
886
- console . log ( smallest . val ) ;
887
- let next = sq . findNextLargest ( ) ;
888
- console . log ( next . val ) ;
949
+ let next = sq . findSmallestValFromRoot ( ) ;
950
+ // console.log(smallest.val);
951
+ // let next = sq.findNextLargest();
952
+ // console.log(next.val);
889
953
let prev = 0 ;
890
954
while ( next ) {
891
955
next = sq . findNextLargest ( next ) ;
@@ -897,7 +961,7 @@ while (next) {
897
961
if ( next . val <= prev ) {
898
962
throw 'smaller!'
899
963
}
900
- prev = next . val ;
964
+ prev = < number > next . val ;
901
965
if ( false ) {
902
966
break ;
903
967
}
0 commit comments