1
1
import Vue from "../node_modules/vue/dist/vue.min"
2
2
import "./components/components"
3
+ import { BinNode } from "./js/BinNode"
4
+ import { BinTree } from "./js/BinTree"
5
+ import { BST } from "./js/BST"
6
+ import { AVL } from "./js/AVL"
7
+ import { Splay } from "./js/Splay"
3
8
4
9
var vm = new Vue ( {
5
10
el : "#TreePlayground" ,
@@ -23,7 +28,7 @@ var vm = new Vue({
23
28
} ,
24
29
locks : { // TODO : seperate trvlLock and searchLock. this can wait.
25
30
trvlLock : false ,
26
- splayLock : false
31
+ rotateLock : false
27
32
} ,
28
33
topSequence : [ ] ,
29
34
BSTParams : {
@@ -213,29 +218,29 @@ var vm = new Vue({
213
218
// Remove one node
214
219
onRemoveOne ( node ) {
215
220
if ( this . isAnyLocked ( ) ) return false ;
216
- this . messages . left = `Remove ${ node . data } ` ;
221
+ this . showMessage ( `Remove ${ node . data } ` ) ;
217
222
if ( "Splay" === this . curTreeType ) { // Exception : Deal with Splay
218
223
this . alertAsync ( `Step 1: Splay ${ node . data } ` , - 1 ) ;
219
224
node . active = true ;
220
225
setTimeout ( ( ) => {
221
- this . locks . splayLock = true ;
226
+ this . locks . rotateLock = true ;
222
227
this . splayAsync ( node , ( rootOrNull ) => {
223
228
if ( rootOrNull === undefined ) return false ;
224
229
if ( rootOrNull === null ) throw "Error in RemoveOne" ;
225
230
let v = rootOrNull ;
226
231
let tree = this . tree ;
227
232
tree . _size -- ;
228
- if ( ! v . rc || ! v . rc ) {
233
+ if ( ! v . rc || ! v . rc ) { // Splay Simple Situation
229
234
if ( ! v . rc ) { if ( tree . _root = v . lc ) tree . _root . parent = null ; }
230
235
else { if ( tree . _root = v . rc ) tree . _root . parent = null ; }
231
236
this . alertAsync ( `Final: remove ${ node . data } ` , 2500 ) ;
232
237
this . update ( ) ;
233
- } else {
238
+ } else { // Splay Complex Situation
234
239
node . active = false ; node . deprecated = true ;
235
240
this . locks . trvlLock = true ;
236
241
this . alertAsync ( `Step 2: Elevate Succ of ${ node . data } ` , - 1 ) ;
237
242
this . searchAsync ( v . rc , v . data , ( _ , hot ) => {
238
- this . locks . splayLock = true ;
243
+ this . locks . rotateLock = true ;
239
244
this . splayAsync ( hot , ( newRoot ) => {
240
245
this . alertAsync ( `Step 3: Finally remove ${ node . data } ` , 2500 ) ;
241
246
tree . reAttachAsLC ( newRoot , v . lc ) ;
@@ -246,13 +251,78 @@ var vm = new Vue({
246
251
} )
247
252
} , this . commonParams . interval ) ;
248
253
} else { // Deal with other trees
249
- this . tree . removeAt ( node ) ;
250
- this . tree . _size -- ;
251
- if ( "AVL" === this . curTreeType ) // BugFixed0305 : _hot already at position after removeAt
252
- this . tree . solveRemoveUnbalance ( ) ;
253
- this . update ( ) ;
254
+ if ( ! node . lc || ! node . rc ) { // Other Trees: Simple Situation
255
+ this . tree . removeAt ( node ) ; this . tree . _size -- ;
256
+ this . alertAsync ( `${ node . data } Removed.` , 2500 ) ;
257
+ this . update ( ) ;
258
+ if ( "AVL" === this . curTreeType ) {
259
+ this . alertAsync ( `${ node . data } Removed, solve AVL Unbalance` , - 1 ) ;
260
+ setTimeout ( ( ) => {
261
+ this . locks . rotateLock = true ;
262
+ this . avlRmRotateAsync ( this . tree . _hot , ( ) => {
263
+ this . alertAsync ( `AVL Balanced again.` ) ;
264
+ this . update ( ) ;
265
+ } ) ;
266
+ } , this . commonParams . interval ) ;
267
+ }
268
+ } else { // Other Trees: Complex situation
269
+ // RM Step 1: Find Succ
270
+ this . alertAsync ( `Step 1: Find Succ` , - 1 ) ;
271
+ let succ = node . succ ( ) ;
272
+ node . deprecated = true ;
273
+ this . locks . trvlLock = true ; // TODO : change to srchLock
274
+ this . searchAsync ( node , succ . data , ( ) => { // assert res === true
275
+ this . alertAsync ( `Step 2: Swap with Succ` , - 1 ) ;
276
+ this . update ( ) ;
277
+ node . deprecated = true ; succ . active = true ;
278
+ setTimeout ( ( ) => {
279
+ // RM Step 2: Swap
280
+ let t = node . data ; node . data = succ . data ; succ . data = t ;
281
+ node . deprecated = false ; succ . active = false ;
282
+ node . active = true ; succ . deprecated = true ;
283
+ // RM Step 3: Remove
284
+ this . alertAsync ( `Step 3: Remove ${ t } ` , 2500 ) ;
285
+ setTimeout ( ( ) => {
286
+ this . tree . removeAt ( succ ) ;
287
+ this . update ( ) ;
288
+ if ( "AVL" === this . curTreeType ) {
289
+ this . alertAsync ( `Step 4: Solve AVL Unbalance` , - 1 ) ;
290
+ setTimeout ( ( ) => {
291
+ this . locks . rotateLock = true ;
292
+ this . avlRmRotateAsync ( this . tree . _hot , ( ) => {
293
+ this . alertAsync ( `AVL Balanced again.` ) ;
294
+ this . update ( ) ;
295
+ } ) ;
296
+ } , this . commonParams . interval ) ;
297
+ }
298
+ } , this . commonParams . interval ) ;
299
+ } , this . commonParams . interval ) ;
300
+ } )
301
+ }
254
302
}
255
303
} ,
304
+ // Async version of AVL.solveRemoveUnbalance
305
+ avlRmRotateAsync ( node , callback ) { // Important: SET rotateLock BEFORE START
306
+ if ( ! node || ! this . locks . rotateLock || "AVL" !== this . curTreeType ) {
307
+ this . locks . rotateLock = false ;
308
+ if ( typeof callback == "function" ) callback ( ) ;
309
+ return ;
310
+ }
311
+ node . active = true ;
312
+ setTimeout ( ( ) => {
313
+ let interval = this . commonParams . interval ;
314
+ if ( ! AVL . avlBalanced ( node ) )
315
+ this . tree . rotateAt ( BinNode . tallerChild ( BinNode . tallerChild ( node ) ) ) ;
316
+ else interval = 0 ;
317
+ this . tree . update_height ( node ) ;
318
+ this . update ( ) ;
319
+ node . active = true ;
320
+ setTimeout ( ( ) => {
321
+ node . active = false ;
322
+ this . avlRmRotateAsync ( node . parent , callback ) ;
323
+ } , interval ) ;
324
+ } , this . commonParams . interval )
325
+ } ,
256
326
// Proper Rebuild
257
327
onTopBuild ( sequence ) {
258
328
if ( this . curTreeType !== "BinTree" )
@@ -289,10 +359,10 @@ var vm = new Vue({
289
359
this . alertAsync ( nodeOrHot ? `Step 2: Splay at ${ nodeOrHot . data } ` : "" , - 1 ) ;
290
360
// Wait & Splay & Insert in callback
291
361
setTimeout ( ( ) => {
292
- this . locks . splayLock = true ;
362
+ this . locks . rotateLock = true ;
293
363
this . splayAsync ( nodeOrHot , ( rootOrNull ) => {
294
364
if ( ! res ) {
295
- if ( rootOrNull === undefined ) return false ; // `splayLock ` has been reset.
365
+ if ( rootOrNull === undefined ) return false ; // `rotateLock ` has been reset.
296
366
this . alertAsync ( `Final: ${ num } Inserted` , 2500 ) ;
297
367
if ( rootOrNull === null ) recentNode = this . tree . insertAsRoot ( num ) ;
298
368
else recentNode = this . tree . insertSplitRoot ( num ) ; // Splay ONLY!!!
@@ -343,14 +413,14 @@ var vm = new Vue({
343
413
if ( this . curTreeType === "Splay" ) { // Exception & Important : Splay
344
414
this . alertAsync ( nodeOrHot ? `Splay at ${ nodeOrHot . data } ` : "" , 2000 ) ;
345
415
setTimeout ( ( ) => {
346
- this . locks . splayLock = true ;
416
+ this . locks . rotateLock = true ;
347
417
this . splayAsync ( nodeOrHot ) ;
348
418
} , this . commonParams . interval ) ;
349
419
}
350
420
} ) ;
351
421
} ,
352
- // Search Async & Recur. Callback: (true, target) if found else (false, _hot)
353
- searchAsync ( node , num , callback ) {
422
+ // Search Async & Recur. Callback: (true, target) if found else (false, _hot)
423
+ searchAsync ( node , num , callback ) { // Important: SET LOCK BEFORE START!
354
424
if ( ! this . locks . trvlLock || ! node ) {
355
425
this . locks . trvlLock = false ;
356
426
if ( typeof callback === "function" ) callback ( false , this . tree . _hot ) ;
@@ -374,13 +444,13 @@ var vm = new Vue({
374
444
}
375
445
} ,
376
446
// Splay Async & Recur. Callback: (null) if !v, (undefined) if locked, (_root) if success
377
- splayAsync ( v , callback ) {
447
+ splayAsync ( v , callback ) { // Important: SET `rotateLock` BEFORE START!
378
448
if ( ! v ) {
379
- this . locks . splayLock = false ;
449
+ this . locks . rotateLock = false ;
380
450
if ( typeof callback === "function" ) callback ( null ) ;
381
451
return false ;
382
452
}
383
- if ( ! this . locks . splayLock ) {
453
+ if ( ! this . locks . rotateLock ) {
384
454
if ( typeof callback === "function" ) callback ( undefined ) ;
385
455
return false ;
386
456
}
@@ -395,7 +465,7 @@ var vm = new Vue({
395
465
this . tree . _root = v ;
396
466
this . update ( ) ;
397
467
v . active = true ;
398
- this . locks . splayLock = false ;
468
+ this . locks . rotateLock = false ;
399
469
setTimeout ( ( ) => {
400
470
if ( typeof callback === "function" ) callback ( v ) ;
401
471
} , this . commonParams . interval ) ;
0 commit comments