Skip to content

Commit 84afb51

Browse files
committed
save the parents
1 parent c4c96bc commit 84afb51

File tree

2 files changed

+124
-44
lines changed

2 files changed

+124
-44
lines changed

scripts/pass-last-defined.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const x = {
2+
foo: {
3+
foo: {
4+
foo: 5
5+
}
6+
}
7+
};
8+
9+
10+
console.log(
11+
x.foo?.foo?.foo?.foo?.foo?.foo
12+
)
13+
14+
console.log(
15+
16+
)

src/sorted-queue.ts

+108-44
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ class SortedQueue<V, K = any> {
8484

8585
}
8686

87-
remove(node: SortedQueueNode<V, K>) : SortedQueueNode<V, K>{
87+
remove(node: SortedQueueNode<V, K>): SortedQueueNode<V, K> {
8888
// this can remove in O(log_2(n))
8989

90-
if(!(node && 'val' in node)){
90+
if (!node) {
91+
return null;
92+
}
93+
94+
if (!(node && 'val' in node)) {
9195
throw new Error('node is not defined, or wrong type.')
9296
}
9397

@@ -102,8 +106,8 @@ class SortedQueue<V, K = any> {
102106
return parent;
103107
};
104108

105-
if(!parent){
106-
if(this.rootNode !== node){
109+
if (!parent) {
110+
if (this.rootNode !== node) {
107111
throw new Error('root node should be parent.')
108112
}
109113
}
@@ -112,12 +116,14 @@ class SortedQueue<V, K = any> {
112116

113117
if (!node.left && !node.right) {
114118

119+
throw 'neither!'
120+
115121
if (!parent) {
116122
// root node...
117123
this.head = null;
118124
this.tail = null;
119125
this.rootNode = null;
120-
return parent;
126+
return makeRet();
121127
}
122128

123129
if (parent.left === node) {
@@ -133,19 +139,24 @@ class SortedQueue<V, K = any> {
133139
this.tail = parent;
134140
}
135141
}
136-
return parent;
142+
return makeRet();
137143
}
138144

139145
if (!node.left) {
140146

147+
throw 'left'
148+
141149
if (isTail) {
142150
throw new Error('should not be tail if node.right is defined.');
143151
}
144152

145-
if(isHead){
153+
if (isHead) {
146154
this.head = node.right;
147155
}
148156

157+
// assign even if parent is null
158+
node.right.parent = parent;
159+
149160
if (!parent) {
150161
this.rootNode = node.right;
151162
return parent;
@@ -159,19 +170,24 @@ class SortedQueue<V, K = any> {
159170
throw new Error('neither left or right hmmm')
160171
}
161172

162-
return parent;
173+
return makeRet();
163174
}
164175

165176
if (!node.right) {
166177

178+
throw 'right'
179+
167180
if (isHead) {
168181
throw new Error('if left is defined, node should not be head.')
169182
}
170183

171-
if(isTail){
184+
if (isTail) {
172185
this.tail = node.left;
173186
}
174187

188+
// assign even if parent is null
189+
node.left.parent = parent;
190+
175191
if (!parent) {
176192
this.rootNode = node.left;
177193
return parent;
@@ -185,52 +201,78 @@ class SortedQueue<V, K = any> {
185201
throw new Error('neither left nor right hmmm.')
186202
}
187203

188-
return parent;
204+
return makeRet();
189205
}
190206

191-
192-
if(!(node.left && node.right)){
207+
if (!(node.left && node.right)) {
193208
throw new Error('both left and right should be defined.')
194209
}
195210

196-
if(!node.left.right){
197-
throw 'foo111'
211+
if (!node.left.right) {
212+
throw 'boof right'
198213
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();
200225
}
201226

202-
if(!node.right.left){
203-
throw 'bar222'
227+
if (!node.right.left) {
228+
// throw 'boof left'
204229
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();
206243
}
207244

208245
const rightMost = this.findLargestGivenVal(node.left);
209246
const leftMost = this.findSmallestGivenVal(node.right);
210247

211-
if(!(rightMost && leftMost)){
248+
if (!(rightMost && leftMost)) {
212249
throw new Error('both of these should be defined!')
213250
}
214251

215252

216-
if(true){
253+
if (true) {
217254

218255
rightMost.parent.right = rightMost.left;
256+
257+
if (rightMost.left) {
258+
rightMost.left.parent = rightMost.parent
259+
}
260+
219261
rightMost.left = node.left;
220262
rightMost.right = node.right;
221263
rightMost.parent = parent;
222264

223-
if(!parent){
265+
if (!parent) {
224266
this.rootNode = rightMost;
225-
return parent;
267+
return makeRet();
226268
}
227269

228-
if(parent.left === node){
270+
if (parent.left === node) {
229271
parent.left = rightMost;
230-
}
231-
232-
if(parent.right === node){
272+
} else if (parent.right === node) {
233273
parent.right = rightMost;
274+
} else {
275+
throw new Error('this is unexpected.')
234276
}
235277

236278
} else {
@@ -240,7 +282,7 @@ class SortedQueue<V, K = any> {
240282
}
241283

242284

243-
return parent;
285+
return makeRet();
244286

245287

246288
}
@@ -326,20 +368,28 @@ class SortedQueue<V, K = any> {
326368

327369
}
328370

329-
logInOrder(node: SortedQueueNode<any, any>) {
371+
logInOrder(node: SortedQueueNode<any, any>, count = {val: 0}) {
330372

331373
if (node === null) {
332374
return;
333375
}
334376

335377
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);
337383
}
338384

339-
console.log(node.val);
385+
console.log(count.val++, node.val);
340386

341387
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);
343393
}
344394
}
345395

@@ -379,7 +429,7 @@ class SortedQueue<V, K = any> {
379429

380430
}
381431

382-
findSmallestValFromRoot() {
432+
findSmallestValFromRoot(): SortedQueueNode<V, K> {
383433
let currentNode = this.rootNode;
384434

385435
while (currentNode.left) {
@@ -465,9 +515,9 @@ class SortedQueue<V, K = any> {
465515
if (n.parent.right && n.parent.right === n) {
466516
n = n.parent;
467517
} 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+
// }
471521
return n.parent;
472522
}
473523

@@ -872,20 +922,34 @@ for (const v of vals) {
872922
// sq.findNextLargest(sq.findNextLargest(sq.findNextLargest(sq.findNextLargest()))).val
873923
// );
874924

925+
console.log('///////');
926+
sq.logInOrder(sq.rootNode);
927+
console.log('////////');
928+
875929
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);
876933
console.log({x});
877934
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);
881935

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('////////')
883947

884948
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);
889953
let prev = 0;
890954
while (next) {
891955
next = sq.findNextLargest(next);
@@ -897,7 +961,7 @@ while (next) {
897961
if (next.val <= prev) {
898962
throw 'smaller!'
899963
}
900-
prev = next.val;
964+
prev = <number>next.val;
901965
if (false) {
902966
break;
903967
}

0 commit comments

Comments
 (0)