@@ -208,7 +208,7 @@ class SortedQueue<V, K = any> {
208208
209209 }
210210
211- findSmallestVal ( ) {
211+ findSmallestValFromRoot ( ) {
212212 let currentNode = this . rootNode ;
213213 if ( ! currentNode ) {
214214 throw new Error ( 'missing root node?' )
@@ -228,10 +228,15 @@ class SortedQueue<V, K = any> {
228228 while ( node . left ) {
229229 node = node . left
230230 }
231+
232+ if ( ! node ) {
233+ throw new Error ( 'missing left node?' )
234+ }
235+
231236 return node ;
232237 }
233238
234- findNextLargestOld ( node : SortedQueueNode < any , any > = this . findSmallestVal ( ) ) {
239+ findNextLargestOld ( node : SortedQueueNode < any , any > = this . findSmallestValFromRoot ( ) ) {
235240
236241 if ( ! node ) {
237242 throw new Error ( 'no current node.' ) ;
@@ -258,7 +263,7 @@ class SortedQueue<V, K = any> {
258263 return node ;
259264 }
260265
261- findNextLargest ( node : SortedQueueNode < any , any > = this . findSmallestVal ( ) ) : SortedQueueNode < any , any > {
266+ findNextLargest ( node : SortedQueueNode < any , any > = this . findSmallestValFromRoot ( ) ) : SortedQueueNode < any , any > {
262267
263268 if ( ! node ) {
264269 throw new Error ( 'no current node.' ) ;
@@ -268,21 +273,41 @@ class SortedQueue<V, K = any> {
268273 return this . findSmallestGivenVal ( node . right ) ;
269274 }
270275
271- while ( node . parent ) {
276+ if ( ! node . parent ) {
277+ return null ;
278+ }
279+
280+ if ( node . parent . left === node ) {
281+ return node . parent ;
282+ }
272283
273- if ( node . parent . right === node ) {
274- node = node . parent ;
275- continue ;
276- }
284+ let n = node . parent ;
277285
278- if ( node . parent . right && node . parent . right !== node ) {
279- return this . findSmallestGivenVal ( node . parent . right ) ;
286+ // if (n.parent && n.parent.right && n.parent.right !== n) {
287+ // return this.findSmallestGivenVal(n.parent.right);
288+ // } else {
289+ // return n.parent;
290+ // }
291+
292+
293+ while ( n . parent ) {
294+ if ( n . parent . right && n . parent . right === n ) {
295+ n = n . parent ;
296+ } else {
297+ if ( n . parent . left !== n ) {
298+ throw new Error ( 'oddd' ) ;
299+ }
300+ return n . parent ;
280301 }
281302
282- node = node . parent ;
303+ // if(!n.parent){
304+ // return this.findSmallestGivenVal(n);
305+ // }
283306 }
284307
285- return node . right ;
308+ return null ;
309+ // return this.findSmallestGivenVal(n);
310+ // return this.findSmallestGivenVal(n.parent || n);
286311 }
287312
288313 iterator ( ) {
@@ -438,19 +463,19 @@ const getNode = <V, K>(v: number, diff: number, count: number): SortedQueueNode<
438463 // console.log(v, count);
439464 return new SortedQueueNode < V , K > (
440465 { val : v as any , key : v as any } ,
441- count > 14 ? emptyNodeSymbol : getNode ( v - diff , diff / 2 , count + 1 ) ,
442- count > 14 ? emptyNodeSymbol : getNode ( v + diff , diff / 2 , count + 1 ) ,
466+ count > 9 ? emptyNodeSymbol : getNode ( v - diff , diff / 2 , count + 1 ) ,
467+ count > 9 ? emptyNodeSymbol : getNode ( v + diff , diff / 2 , count + 1 ) ,
443468 ) ;
444469}
445470
446471console . time ( 'foo' )
447- const rootNode = getNode ( 0.5 , 0.25 , 1 ) ;
448- ( rootNode as any ) . isRoot = true ;
472+ const rootNode3 = getNode ( 0.5 , 0.25 , 1 ) ;
473+ ( rootNode3 as any ) . isRoot = true ;
449474console . timeEnd ( 'foo' ) ;
450475// process.exit(0);
451476
452477
453- const rootNode2 = new SortedQueueNode < number , number > (
478+ const rootNode = new SortedQueueNode < number , number > (
454479 { val : 0.5 } ,
455480
456481 new SortedQueueNode < number , number > (
@@ -468,9 +493,23 @@ const rootNode2 = new SortedQueueNode<number, number>(
468493 { val : 0.375 } ,
469494 new SortedQueueNode < number , number > (
470495 { val : 0.3125 } ,
496+ new SortedQueueNode < number , number > (
497+ { val : 0.3025 } ,
498+ new SortedQueueNode < number , number > (
499+ { val : 0.3005 } ,
500+ ) ,
501+ ) ,
471502 ) ,
472503 new SortedQueueNode < number , number > (
473504 { val : 0.4375 } ,
505+ emptyNodeSymbol ,
506+ new SortedQueueNode < number , number > (
507+ { val : 0.4385 } ,
508+ emptyNodeSymbol ,
509+ new SortedQueueNode < number , number > (
510+ { val : 0.4395 } ,
511+ ) ,
512+ ) ,
474513 ) ,
475514 ) ,
476515 ) ,
@@ -509,7 +548,7 @@ const vals = [];
509548
510549console . time ( 'start' ) ;
511550
512- for ( let i = 0 ; i < 1000 ; i ++ ) {
551+ for ( let i = 0 ; i < 5 ; i ++ ) {
513552 const r = Math . random ( ) ;
514553 // console.time(String(r));
515554 sq . insert ( r ) ;
@@ -592,7 +631,6 @@ const runLog3 = () => {
592631 // console.log('val:', z);
593632 } ) ;
594633 console . timeEnd ( 'bar3' )
595-
596634 console . log ( { count} ) ;
597635}
598636
@@ -631,41 +669,52 @@ const doRecurse = <K, V>(n: SortedQueueNode<V, K>, count: number) => {
631669 }
632670} ;
633671
634- doRecurse ( rootNode , 0 ) ;
672+ // doRecurse(rootNode, 0);
635673// console.log(sq);
636674
637675
638- // for (const v of vals) {
639- // console.log(sq.find(v).numOfSearches);
640- // }
676+ for ( const v of vals ) {
677+ console . log ( sq . find ( v ) . numOfSearches ) ;
678+ }
641679//
642- console . log ( sq . find ( 0.5 ) . numOfSearches ) ;
643- console . log ( sq . find ( 0.25 ) . numOfSearches ) ;
644- console . log ( sq . find ( 0.375 ) . numOfSearches ) ;
645- console . log ( sq . find ( 0.8125 ) . numOfSearches ) ;
646-
647- //TODO: fix this method
648- console . log ( sq . findNextBiggest ( 0.11 ) . currentNode . val ) ;
649-
650- console . log ( sq . findNextLargest ( ) . val ) ;
651-
652- console . log (
653- 'zzz:' ,
654- sq . findNextLargest ( sq . findNextLargest ( sq . findNextLargest ( ) ) ) . val
655- ) ;
656-
657- console . log (
658- 'zzz:' ,
659- sq . findNextLargest ( sq . findNextLargest ( sq . findNextLargest ( sq . findNextLargest ( ) ) ) ) . val
660- ) ;
680+ // console.log(sq.find(0.5).numOfSearches);
681+ // console.log(sq.find(0.25).numOfSearches);
682+ // console.log(sq.find(0.375).numOfSearches);
683+ // console.log(sq.find(0.8125).numOfSearches);
684+ //
685+ // // TODO: fix this method
686+ // console.log(sq.findNextBiggest(0.11).currentNode.val);
687+ //
688+ // console.log(sq.findNextLargest().val);
689+ //
690+ // console.log(
691+ // 'zzz:',
692+ // sq.findNextLargest(sq.findNextLargest(sq.findNextLargest())).val
693+ // );
694+ //
695+ // console.log(
696+ // 'zzz:',
697+ // sq.findNextLargest(sq.findNextLargest(sq.findNextLargest(sq.findNextLargest()))).val
698+ // );
661699
662700let count = 0 ;
701+ let smallest = sq . findSmallestValFromRoot ( ) ;
702+ console . log ( smallest . val ) ;
663703let next = sq . findNextLargest ( ) ;
664- while ( true ) {
704+ console . log ( next . val ) ;
705+ let prev = 0 ;
706+ while ( next ) {
665707 next = sq . findNextLargest ( next ) ;
666- console . log ( count , next && next . val || 'fo' ) ;
667- if ( count ++ > 55 ) {
668- console . error ( 'breaking?' , count ) ;
708+ if ( ! next ) {
709+ continue ;
710+ }
711+ console . log ( count ++ , next && next . val ) ;
712+ // console.error('breaking?', count);
713+ if ( next . val <= prev ) {
714+ throw 'smaller!'
715+ }
716+ prev = next . val ;
717+ if ( false ) {
669718 break ;
670719 }
671720}
0 commit comments