@@ -42,6 +42,7 @@ var vm = new Vue({
42
42
console . log ( "Load default tree." )
43
43
this . loadSampleTree ( ) ;
44
44
}
45
+ this . reset ( ) ;
45
46
} ,
46
47
reset ( ) {
47
48
console . log ( "Reset" ) ;
@@ -62,9 +63,8 @@ var vm = new Vue({
62
63
loadFromHistory ( ) { } ,
63
64
loadSampleTree ( ) {
64
65
this . tree = this . curTreeClass . genSampleTree ( ) ;
65
- this . update ( ) ;
66
66
} ,
67
- alertAsync ( message , time = 1000 ) {
67
+ alertAsync ( message , time = 1500 ) {
68
68
this . messages . right = message ;
69
69
let tag = ++ this . alertTag ;
70
70
setTimeout ( ( e = tag ) => {
@@ -111,48 +111,36 @@ var vm = new Vue({
111
111
112
112
// Events Handlers
113
113
onIntrUpdate ( args ) { // Internal node requests for value update
114
- console . log ( "onIntrUpdate" ) ;
115
114
let node = args [ 0 ] ;
116
115
let updation = args [ 1 ] ;
116
+ let successMessage = `Change ${ node . data } to ${ updation } ` ;
117
117
if ( this . curTreeType !== "BinTree" ) {
118
118
if ( this . tree . search ( updation ) ) {
119
- alert ( "Already exists!" ) ;
120
- return false ;
121
- } else if ( BinNode . isLC ( node ) && updation > node . parent . data ||
122
- BinNode . isRC ( node ) && updation < node . parent . data ||
123
- node . lc && updation < node . lc . data ||
124
- node . rc && updation > node . rc . data ) {
125
- alert ( "Must maintain order." ) ;
119
+ this . alertAsync ( `${ updation } Exists!` ) ;
126
120
return false ;
127
121
}
122
+ if ( ! this . checkNodeOrder ( node , updation ) ) return false ;
128
123
}
129
124
node . data = updation ;
130
125
this . update ( ) ;
131
- } ,
126
+ this . messages . left = successMessage ;
127
+ } , // TODO: active newly updated node. Update before and after every action.
132
128
onExtrInsert ( args ) { // External node requests for value insertion
133
- console . log ( "onExtrInsert" ) ;
134
129
let node = args [ 0 ] ;
135
130
let insertion = args [ 1 ] ;
136
131
let curTreeType = this . curTreeType ;
137
132
138
133
if ( curTreeType === "Splay" ) {
139
- alert ( "Can't insert at external nodes in SplayTree." ) ;
134
+ this . alertAsync ( "Can't insert at external nodes in SplayTree." , 3000 ) ;
140
135
return false ;
141
136
}
142
137
if ( curTreeType !== "BinTree" ) {
143
138
if ( this . tree . search ( insertion ) ) { // Decline duplicate
144
- alert ( "Already exists!" ) ;
145
- return false ;
146
- }
147
- // pred and succ of parent
148
- let pred , succ ;
149
- if ( node . isLC === true && insertion > node . parent . data ||
150
- node . isLC === true && ( pred = node . parent . pred ( ) ) && insertion < pred . data ||
151
- node . isLC === false && insertion < node . parent . data ||
152
- node . isLC === false && ( succ = node . parent . succ ( ) ) && insertion > succ . data ) {
153
- alert ( "Must maintain order." ) ;
139
+ this . alertAsync ( `${ insertion } Exists!` ) ;
154
140
return false ;
155
141
}
142
+ // check new order
143
+ if ( ! this . checkNodeOrder ( node , insertion ) ) return false ;
156
144
}
157
145
var updateH ;
158
146
if ( curTreeType === "BinTree" || curTreeType === "BST" )
@@ -169,15 +157,28 @@ var vm = new Vue({
169
157
170
158
if ( curTreeType === "AVL" ) {
171
159
this . tree . search ( insertion ) ; // locate _hot
172
- this . tree . solveInsertUnbalance ( ) ;
160
+ this . tree . solveInsertUnbalance ( ) ; // TODO: change to async
173
161
}
174
162
this . update ( ) ;
163
+ this . messages . left = `Insert ${ insertion } ` ;
164
+ } ,
165
+ checkNodeOrder ( node , newV ) {
166
+ let pred , succ ;
167
+ let isLC = node . isLC || BinNode . isLC ( node ) ;
168
+ if ( isLC === true && newV > node . parent . data ||
169
+ isLC === true && ( pred = node . parent . pred ( ) ) && newV < pred . data ||
170
+ isLC === false && newV < node . parent . data ||
171
+ isLC === false && ( succ = node . parent . succ ( ) ) && newV > succ . data ||
172
+ node . lc && newV < node . lc . data || node . rc && newV > node . rc . data ) {
173
+ this . alertAsync ( "Must maintain order." , 2500 ) ;
174
+ return false ;
175
+ } return true ;
175
176
} ,
176
177
// Remove whole subtree
177
178
onRemoveBelow ( node ) {
178
179
this . tree . removeBelow ( node ) ;
179
180
this . update ( ) ;
180
- this . alertAsync ( `Remove Below ${ node . data } ` , 1000 ) ;
181
+ this . alertAsync ( `Remove Below ${ node . data } ` ) ;
181
182
} ,
182
183
// Remove one node
183
184
onRemoveOne ( node ) {
@@ -189,7 +190,7 @@ var vm = new Vue({
189
190
}
190
191
else if ( 0 ) { }
191
192
this . update ( ) ;
192
- this . alertAsync ( `Remove ${ node . data } ` , 1000 ) ;
193
+ this . alertAsync ( `Remove ${ node . data } ` ) ;
193
194
} ,
194
195
// Proper Rebuild
195
196
onTopBuild ( sequence ) {
@@ -198,6 +199,10 @@ var vm = new Vue({
198
199
this . tree . buildFromBinSequence ( sequence ) ;
199
200
this . update ( ) ;
200
201
this . messages . left = "真二叉树层次序列构建" ;
202
+
203
+ this . curTreeClass . checkValidity ( this . tree , ( res , message ) => {
204
+ if ( ! res ) this . alertAsync ( message , 3000 ) ;
205
+ } )
201
206
} ,
202
207
// Insert sequence
203
208
onTopInsert ( sequence ) {
@@ -229,6 +234,18 @@ var vm = new Vue({
229
234
else Math . random ( ) < 0.5 ? this . alertAsync ( "Not Found" ) : this . alertAsync ( "404" ) ;
230
235
} ) ;
231
236
} ,
237
+ onTopHelp ( message ) {
238
+ this . alertAsync ( message , 5000 ) ;
239
+ } ,
240
+ // Proper Binary Tree Sequence
241
+ onTopProper ( ) {
242
+ let sequence = BinTree . properTraversal ( this . tree . root ( ) ) ;
243
+ for ( let i = 0 ; i < sequence . length ; i ++ ) sequence [ i ] = sequence [ i ] ? sequence [ i ] . data : null ;
244
+ let last = sequence . length - 1 ;
245
+ while ( sequence [ last ] === null ) last -- ;
246
+ sequence . splice ( last + 1 ) ;
247
+ this . topSequence = sequence ;
248
+ } ,
232
249
searchAsync ( node , num , callback ) {
233
250
if ( ! this . trvlParams . lock || ! node ) {
234
251
this . trvlParams . lock = false ;
@@ -293,7 +310,7 @@ var vm = new Vue({
293
310
} ,
294
311
strToArr ( str ) {
295
312
str = str . trim ( ) ;
296
- if ( str === "" ) return false ;
313
+ if ( str === "" ) return null ;
297
314
let arr = str . split ( / , | , / ) ;
298
315
for ( let i = 0 ; i < arr . length ; i ++ ) {
299
316
arr [ i ] = this . assertNumber ( arr [ i ] ) ;
@@ -306,7 +323,6 @@ var vm = new Vue({
306
323
get ( ) { return this . trees [ this . curTreeType ] ; } ,
307
324
set ( newTree ) {
308
325
this . trees [ this . curTreeType ] = newTree ;
309
- this . update ( ) ;
310
326
}
311
327
} ,
312
328
curTreeType : {
0 commit comments