@@ -6,12 +6,12 @@ import { BST } from "./js/BST"
6
6
import { AVL } from "./js/AVL"
7
7
import { Splay } from "./js/Splay"
8
8
9
- var vm = new Vue ( {
9
+ var tp = new Vue ( {
10
10
el : "#TreePlayground" ,
11
11
data : {
12
12
availTreeTypes : { "BinTree" : true , "BST" : true , "AVL" : true , "Splay" : true , "RedBlack" : false } ,
13
13
commonParams : {
14
- curTreeType : "BinTree " , // Important : Always use as `this.curTreeType`.
14
+ curTreeType : "BST " , // Important : Always use as `this.curTreeType`.
15
15
treeScale : 100 , // in %
16
16
interval : 500 // in ms
17
17
} ,
@@ -223,25 +223,27 @@ var vm = new Vue({
223
223
this . alertAsync ( `Step 1: Splay ${ node . data } ` , - 1 ) ;
224
224
node . active = true ;
225
225
setTimeout ( ( ) => {
226
+ // Splay RM Step 1
226
227
this . locks . rotateLock = true ;
227
228
this . splayAsync ( node , ( rootOrNull ) => {
228
229
if ( rootOrNull === undefined ) return false ;
229
230
if ( rootOrNull === null ) throw "Error in RemoveOne" ;
230
231
let v = rootOrNull ;
231
232
let tree = this . tree ;
232
233
tree . _size -- ;
233
- if ( ! v . rc || ! v . rc ) { // Splay Simple Situation
234
+ if ( ! v . rc || ! v . rc ) { // Splay RM Step 2a
234
235
if ( ! v . rc ) { if ( tree . _root = v . lc ) tree . _root . parent = null ; }
235
236
else { if ( tree . _root = v . rc ) tree . _root . parent = null ; }
236
237
this . alertAsync ( `Final: remove ${ node . data } ` , 2500 ) ;
237
238
this . update ( ) ;
238
- } else { // Splay Complex Situation
239
+ } else { // Splay RM Step 2b
239
240
node . active = false ; node . deprecated = true ;
240
241
this . locks . trvlLock = true ;
241
242
this . alertAsync ( `Step 2: Elevate Succ of ${ node . data } ` , - 1 ) ;
242
243
this . searchAsync ( v . rc , v . data , ( _ , hot ) => {
243
244
this . locks . rotateLock = true ;
244
245
this . splayAsync ( hot , ( newRoot ) => {
246
+ // Splay RM Step 3
245
247
this . alertAsync ( `Step 3: Finally remove ${ node . data } ` , 2500 ) ;
246
248
tree . reAttachAsLC ( newRoot , v . lc ) ;
247
249
this . update ( ) ;
@@ -272,11 +274,11 @@ var vm = new Vue({
272
274
node . deprecated = true ;
273
275
this . locks . trvlLock = true ; // TODO : change to srchLock
274
276
this . searchAsync ( node , succ . data , ( ) => { // assert res === true
277
+ // RM Step 2: Swap with Succ
275
278
this . alertAsync ( `Step 2: Swap with Succ` , - 1 ) ;
276
279
this . update ( ) ;
277
280
node . deprecated = true ; succ . active = true ;
278
281
setTimeout ( ( ) => {
279
- // RM Step 2: Swap
280
282
let t = node . data ; node . data = succ . data ; succ . data = t ;
281
283
node . deprecated = false ; succ . active = false ;
282
284
node . active = true ; succ . deprecated = true ;
@@ -285,8 +287,10 @@ var vm = new Vue({
285
287
setTimeout ( ( ) => {
286
288
this . tree . removeAt ( succ ) ;
287
289
this . update ( ) ;
290
+ // RM Step 4 : AVL reBalance
288
291
if ( "AVL" === this . curTreeType ) {
289
- this . alertAsync ( `Step 4: Solve AVL Unbalance` , - 1 ) ;
292
+ this . alertAsync ( `Step 4: AVL reBalance` , - 1 ) ;
293
+ if ( this . tree . _hot ) this . tree . _hot . active = true ;
290
294
setTimeout ( ( ) => {
291
295
this . locks . rotateLock = true ;
292
296
this . avlRmRotateAsync ( this . tree . _hot , ( ) => {
@@ -494,10 +498,13 @@ var vm = new Vue({
494
498
/* Dragger */
495
499
/****************************************/
496
500
onTreeMouseDown ( event ) {
501
+ if ( event . button !== 0 && event . type !== "touchstart" ) {
502
+ this . isDragging = false ; return false ;
503
+ }
497
504
console . log ( "Start dragging" )
498
- this . treeXY = [ event . target . offsetLeft , event . target . offsetTop ] ;
505
+ this . treeXY = [ this . $refs . tree . offsetLeft , this . $refs . tree . offsetTop ] ;
499
506
switch ( event . type ) {
500
- case "mousedown" : this . mouseXY = [ event . x , event . y ] ; break ;
507
+ case "mousedown" : this . mouseXY = [ event . clientX , event . clientY ] ; break ;
501
508
case "touchstart" :
502
509
this . mouseXY = [ event . touches [ 0 ] . clientX , event . touches [ 0 ] . clientY ] ;
503
510
break ;
@@ -509,7 +516,7 @@ var vm = new Vue({
509
516
if ( this . isDragging ) {
510
517
let newXY ;
511
518
switch ( event . type ) {
512
- case "mousemove" : newXY = [ event . x , event . y ] ; break ;
519
+ case "mousemove" : newXY = [ event . clientX , event . clientY ] ; break ;
513
520
case "touchmove" :
514
521
newXY = [ event . touches [ 0 ] . clientX , event . touches [ 0 ] . clientY ] ;
515
522
break ;
@@ -520,8 +527,10 @@ var vm = new Vue({
520
527
}
521
528
} ,
522
529
onTreeMouseLeave ( e ) {
523
- console . log ( "End dragging" )
524
- this . isDragging = false ;
530
+ if ( this . isDragging ) {
531
+ console . log ( "End dragging" )
532
+ this . isDragging = false ;
533
+ }
525
534
} ,
526
535
/****************************************/
527
536
/* Validators */
@@ -580,13 +589,12 @@ var vm = new Vue({
580
589
} ,
581
590
curTreeType : {
582
591
get ( ) { return this . commonParams . curTreeType ; } ,
583
- set ( newV ) { this . commonParams . curTreeType = newV ; this . init ( ) ; } // Important!!!
592
+ set ( newV ) { this . commonParams . curTreeType = newV ; this . init ( ) ; } // Important
584
593
} ,
585
594
treeScale : {
586
595
get ( ) { return this . commonParams . treeScale ; } ,
587
596
set ( newV ) { this . commonParams . treeScale = newV ; }
588
597
} ,
589
-
590
598
curTreeClass ( ) {
591
599
return this . treeClassMap [ this . curTreeType ] ;
592
600
} ,
@@ -596,7 +604,7 @@ var vm = new Vue({
596
604
} ,
597
605
showExtr ( ) {
598
606
return true ;
599
- }
607
+ } ,
600
608
} ,
601
609
watch : {
602
610
tree : {
@@ -609,14 +617,14 @@ var vm = new Vue({
609
617
handler ( ) {
610
618
localStorage . commonParams = JSON . stringify ( this . commonParams ) ;
611
619
} , deep : true
612
- }
620
+ } ,
613
621
} ,
614
622
mounted ( ) {
615
623
try { this . commonParams = JSON . parse ( localStorage . commonParams ) ; }
616
624
catch ( err ) { }
617
- if ( this . availTreeTypes [ this . curTreeType ] == undefined ) this . curTreeType = "BinTree " ;
625
+ if ( this . availTreeTypes [ this . curTreeType ] == undefined ) this . curTreeType = "BST " ;
618
626
this . init ( ) ;
619
627
} ,
620
628
} ) ;
621
629
622
- window . vm = vm ;
630
+ window . tp = tp ;
0 commit comments