1
- import Vue from "../node_modules/vue/dist/vue.min "
1
+ import Vue from "../node_modules/vue/dist/vue"
2
2
import IntrBinnode from "./components/binnode.vue"
3
3
import ExtrBinnode from "./components/extr-binnode.vue"
4
4
import TopBinnode from "./components/top-binnode.vue"
5
- import { BinNode } from "./js/BinNode"
5
+ import { BinNode , RBColor , TreeUtil } from "./js/BinNode"
6
6
import { BinTree } from "./js/BinTree"
7
7
import { BST } from "./js/BST"
8
8
import { AVL } from "./js/AVL"
9
9
import { Splay } from "./js/Splay"
10
+ import { RedBlack } from "./js/RedBlack"
10
11
11
12
var tp = new Vue ( {
12
13
el : "#TreePlayground" ,
13
14
data : {
14
- availTreeTypes : { "BinTree" : true , "BST" : true , "AVL" : true , "Splay" : true , "RedBlack" : false } ,
15
+ availTreeTypes : { "BinTree" : true , "BST" : true , "AVL" : true , "Splay" : true , "RedBlack" : true } ,
15
16
commonParams : {
16
17
curTreeType : "BST" , // Important : Always use as `this.curTreeType`.
17
18
treeScale : 100 , // in %
@@ -20,7 +21,7 @@ var tp = new Vue({
20
21
messages : {
21
22
left : "" , right : ""
22
23
} ,
23
- treeClassMap : { "BinTree" : BinTree , "BST" : BST , "AVL" : AVL , "Splay" : Splay } ,
24
+ treeClassMap : { "BinTree" : BinTree , "BST" : BST , "AVL" : AVL , "Splay" : Splay , "RedBlack" : RedBlack } ,
24
25
trees : { "BinTree" : null , "BST" : null , "AVL" : null , "Splay" : null , "RedBlack" : null } ,
25
26
structInfo : {
26
27
nodes : [ ] ,
@@ -228,13 +229,17 @@ var tp = new Vue({
228
229
onRemoveOne ( node ) {
229
230
if ( this . isAnyLocked ( ) ) return false ;
230
231
this . showMessage ( `Remove ${ node . data } ` ) ;
231
- if ( "Splay" === this . curTreeType ) { // Exception : Deal with Splay
232
+ if ( "RedBlack" === this . curTreeType ) { // TODO: No Vigor to write async version anymore using callback
233
+ this . tree . remove ( node . data ) ; // Maybe change everything to await promise in the future.
234
+ this . update ( ) ;
235
+ }
236
+ else if ( "Splay" === this . curTreeType ) { // Exception : Deal with Splay
232
237
this . alertAsync ( `Step 1: Splay ${ node . data } ` , - 1 ) ;
233
238
node . active = true ;
234
239
setTimeout ( ( ) => {
235
240
// Splay RM Step 1
236
241
this . locks . rotateLock = true ;
237
- this . splayAsync ( node , ( rootOrNull ) => {
242
+ this . _splayAsync ( node , ( rootOrNull ) => {
238
243
if ( rootOrNull === undefined ) return false ;
239
244
if ( rootOrNull === null ) throw "Error in RemoveOne" ;
240
245
let v = rootOrNull ;
@@ -249,9 +254,9 @@ var tp = new Vue({
249
254
node . active = false ; node . deprecated = true ;
250
255
this . locks . trvlLock = true ;
251
256
this . alertAsync ( `Step 2: Elevate Succ of ${ node . data } ` , - 1 ) ;
252
- this . searchAsync ( v . rc , v . data , ( _ , hot ) => {
257
+ this . _searchAsync ( v . rc , v . data , ( _ , hot ) => {
253
258
this . locks . rotateLock = true ;
254
- this . splayAsync ( hot , ( newRoot ) => {
259
+ this . _splayAsync ( hot , ( newRoot ) => {
255
260
// Splay RM Step 3
256
261
this . alertAsync ( `Step 3: Finally remove ${ node . data } ` , 2500 ) ;
257
262
tree . reAttachAsLC ( newRoot , v . lc ) ;
@@ -282,7 +287,7 @@ var tp = new Vue({
282
287
let succ = node . succ ( ) ;
283
288
node . deprecated = true ;
284
289
this . locks . trvlLock = true ; // TODO : change to srchLock
285
- this . searchAsync ( node , succ . data , ( ) => { // assert res === true
290
+ this . _searchAsync ( node , succ . data , ( ) => { // assert res === true
286
291
// RM Step 2: Swap with Succ
287
292
this . alertAsync ( `Step 2: Swap with Succ` , - 1 ) ;
288
293
this . update ( ) ;
@@ -325,9 +330,9 @@ var tp = new Vue({
325
330
setTimeout ( ( ) => {
326
331
let interval = this . commonParams . interval ;
327
332
if ( ! AVL . avlBalanced ( node ) )
328
- this . tree . rotateAt ( BinNode . tallerChild ( BinNode . tallerChild ( node ) ) ) ;
333
+ this . tree . rotateAt ( TreeUtil . tallerChild ( TreeUtil . tallerChild ( node ) ) ) ;
329
334
else interval = 0 ;
330
- this . tree . update_height ( node ) ;
335
+ this . tree . updateHeight ( node ) ;
331
336
this . update ( ) ;
332
337
node . active = true ;
333
338
setTimeout ( ( ) => {
@@ -365,15 +370,15 @@ var tp = new Vue({
365
370
this . alertAsync ( `Step 1: Search ${ num } ` , - 1 ) ;
366
371
this . locks . trvlLock = true ;
367
372
this . tree . _hot = null ; // Important: reset _hot before search
368
- this . searchAsync ( this . tree . root ( ) , num , ( res , nodeOrHot ) => {
373
+ this . _searchAsync ( this . tree . root ( ) , num , ( res , nodeOrHot ) => {
369
374
let recentNode = null ;
370
375
// Exception : Deal with Splay
371
376
if ( "Splay" === this . curTreeType ) { // Caution & Important & TODO : May need change
372
377
this . alertAsync ( nodeOrHot ? `Step 2: Splay at ${ nodeOrHot . data } ` : "" , - 1 ) ;
373
378
// Wait & Splay & Insert in callback
374
379
setTimeout ( ( ) => {
375
380
this . locks . rotateLock = true ;
376
- this . splayAsync ( nodeOrHot , ( rootOrNull ) => {
381
+ this . _splayAsync ( nodeOrHot , ( rootOrNull ) => {
377
382
if ( ! res ) {
378
383
if ( rootOrNull === undefined ) return false ; // `rotateLock` has been reset.
379
384
this . alertAsync ( `Final: ${ num } Inserted` , 2500 ) ;
@@ -405,7 +410,7 @@ var tp = new Vue({
405
410
this . update ( ) ;
406
411
if ( this . topSequence . length === 0 ) {
407
412
recentNode . active = true ; // Caution: Mark recent active
408
- this . locks . trvlLock = false ; return false ;
413
+ this . locks . trvlLock = false ; return true ;
409
414
} else this . insertSequnceAsync ( ) ;
410
415
} , this . commonParams . interval ) ;
411
416
/* ----------------------------------------------------------------------------------------------------- */
@@ -420,20 +425,20 @@ var tp = new Vue({
420
425
this . messages . left = `Search ${ num } ` ;
421
426
422
427
this . tree . _hot = null ; // Important: reset _hot before search
423
- this . searchAsync ( this . tree . root ( ) , num , ( res , nodeOrHot ) => {
428
+ this . _searchAsync ( this . tree . root ( ) , num , ( res , nodeOrHot ) => {
424
429
if ( res ) this . alertAsync ( `${ num } Found` ) ;
425
430
else Math . random ( ) < 0.5 ? this . alertAsync ( `${ num } Not Found` ) : this . alertAsync ( `${ num } 404` ) ;
426
431
if ( this . curTreeType === "Splay" ) { // Exception & Important : Splay
427
432
this . alertAsync ( nodeOrHot ? `Splay at ${ nodeOrHot . data } ` : "" , 2000 ) ;
428
433
setTimeout ( ( ) => {
429
434
this . locks . rotateLock = true ;
430
- this . splayAsync ( nodeOrHot ) ;
435
+ this . _splayAsync ( nodeOrHot ) ;
431
436
} , this . commonParams . interval ) ;
432
437
}
433
438
} ) ;
434
439
} ,
435
440
// Search Async & Recur. Callback: (true, target) if found else (false, _hot)
436
- searchAsync ( node , num , callback ) { // Important: SET LOCK BEFORE START!
441
+ _searchAsync ( node , num , callback ) { // Important: SET LOCK BEFORE START!
437
442
if ( ! this . locks . trvlLock || ! node ) {
438
443
this . locks . trvlLock = false ;
439
444
if ( typeof callback === "function" ) callback ( false , this . tree . _hot ) ;
@@ -452,12 +457,12 @@ var tp = new Vue({
452
457
node . visited = true ;
453
458
if ( num < node . data ) node = node . lc ;
454
459
else node = node . rc ;
455
- this . searchAsync ( node , num , callback ) ;
460
+ this . _searchAsync ( node , num , callback ) ;
456
461
} , this . commonParams . interval ) ;
457
462
}
458
463
} ,
459
464
// Splay Async & Recur. Callback: (null) if !v, (undefined) if locked, (_root) if success
460
- splayAsync ( v , callback ) { // Important: SET `rotateLock` BEFORE START!
465
+ _splayAsync ( v , callback ) { // Important: SET `rotateLock` BEFORE START!
461
466
if ( ! v ) {
462
467
this . locks . rotateLock = false ;
463
468
if ( typeof callback === "function" ) callback ( null ) ;
@@ -486,7 +491,7 @@ var tp = new Vue({
486
491
this . update ( ) ;
487
492
v . active = true ;
488
493
setTimeout ( ( ) => {
489
- this . splayAsync ( v , callback ) ;
494
+ this . _splayAsync ( v , callback ) ;
490
495
} , this . commonParams . interval ) ;
491
496
}
492
497
} ,
@@ -503,7 +508,6 @@ var tp = new Vue({
503
508
sequence . splice ( last + 1 ) ;
504
509
this . topSequence = sequence ;
505
510
} ,
506
-
507
511
/****************************************/
508
512
/* Dragger */
509
513
/****************************************/
@@ -572,7 +576,7 @@ var tp = new Vue({
572
576
} ,
573
577
checkNodeOrder ( node , newV ) {
574
578
let pred , succ ;
575
- let isLC = node . isLC || BinNode . isLC ( node ) ;
579
+ let isLC = node . isLC || TreeUtil . isLC ( node ) ;
576
580
if ( node . lc === undefined ) { // External nodes
577
581
if ( isLC === true && newV > node . parent . data ||
578
582
isLC === true && ( pred = node . parent . pred ( ) ) && newV < pred . data ||
@@ -590,6 +594,14 @@ var tp = new Vue({
590
594
}
591
595
return true ;
592
596
} ,
597
+
598
+ /****************************************/
599
+ /* Others */
600
+ /****************************************/
601
+ nodeColorClass ( color ) {
602
+ if ( this . curTreeType === "RedBlack" ) return color == RBColor . Red ? "red-node" : "black-node" ;
603
+ return "normal-color-node" ;
604
+ } ,
593
605
} ,
594
606
computed : {
595
607
tree : {
@@ -613,17 +625,14 @@ var tp = new Vue({
613
625
let scale = this . treeScale / 100 ;
614
626
return `transform:scale(${ scale } )` ;
615
627
} ,
616
- showExtr ( ) {
617
- return true ;
618
- } ,
619
628
} ,
620
629
watch : {
621
- tree : {
622
- handler ( ) {
623
- console . log ( "Detect Change in tree." ) ;
624
- } ,
625
- deep : true ,
626
- } ,
630
+ // tree: {
631
+ // handler() {
632
+ // console.log("Detect Change in tree.");
633
+ // },
634
+ // deep: true,
635
+ // },
627
636
commonParams : {
628
637
handler ( ) {
629
638
localStorage . commonParams = JSON . stringify ( this . commonParams ) ;
0 commit comments