@@ -38,6 +38,10 @@ BST.prototype.insert = function(value) {
38
38
BST . prototype . delete = function ( value ) {
39
39
let current = this . search ( value )
40
40
41
+ if ( ! current ) {
42
+ return null
43
+ }
44
+
41
45
if ( ! current . left ) {
42
46
this . transplant ( current , current . right )
43
47
} else if ( ! current . right ) {
@@ -54,6 +58,8 @@ BST.prototype.delete = function(value) {
54
58
y . left = current . left
55
59
y . left . parent = y
56
60
}
61
+
62
+ return current
57
63
}
58
64
59
65
BST . prototype . transplant = function ( u , v ) {
@@ -154,28 +160,116 @@ BST.prototype.inorderTreeWalk = function(node, callback) {
154
160
155
161
const test = require ( 'tape' )
156
162
157
- test ( 'BST' , assert => {
163
+ test ( 'BST insert' , assert => {
164
+ let bst = new BST ( )
165
+ bst . insert ( 12 )
166
+ bst . insert ( 4 )
167
+ bst . insert ( 8 )
168
+ bst . insert ( 15 )
169
+ assert . deepEqual ( bst . length ( ) , 4 , 'length should be 4' )
170
+ assert . end ( )
171
+ } )
172
+
173
+ test ( 'BST delete' , assert => {
174
+ let bst = new BST ( )
175
+ bst . insert ( 12 )
176
+ bst . insert ( 4 )
177
+ bst . insert ( 8 )
178
+ bst . insert ( 15 )
179
+ assert . deepEqual ( bst . delete ( 2 ) , null , 'should return null' )
180
+ assert . deepEqual ( bst . delete ( 8 ) . key , 8 , 'should return 8' )
181
+ assert . deepEqual ( bst . length ( ) , 3 , 'length should be 3' )
182
+ assert . end ( )
183
+ } )
184
+
185
+ test ( 'BST transplant' , assert => {
186
+ let bst = new BST ( ) ,
187
+ el1 ,
188
+ el2
189
+ bst . insert ( 12 )
190
+ bst . insert ( 4 )
191
+ bst . insert ( 8 )
192
+ bst . insert ( 15 )
193
+ el1 = bst . search ( 4 )
194
+ el2 = bst . search ( 8 )
195
+ assert . deepEqual ( el2 . parent . key , 4 , '8 parent ́s should be 4' )
196
+ bst . transplant ( el1 , el2 )
197
+ assert . deepEqual ( el2 . parent . key , 12 , '8 parent ́s should be 12' )
198
+ assert . end ( )
199
+ } )
200
+
201
+ test ( 'BST search' , assert => {
202
+ let bst = new BST ( )
203
+ bst . insert ( 12 )
204
+ bst . insert ( 4 )
205
+ bst . insert ( 8 )
206
+ bst . insert ( 15 )
207
+ assert . deepEqual ( bst . search ( 8 ) . key , 8 , 'should find element with key 8' )
208
+ assert . deepEqual ( bst . search ( 2 ) , null , 'should return null if not found' )
209
+ assert . end ( )
210
+ } )
211
+
212
+ test ( 'BST min' , assert => {
213
+ let bst = new BST ( )
214
+ bst . insert ( 12 )
215
+ bst . insert ( 4 )
216
+ bst . insert ( 8 )
217
+ bst . insert ( 2 )
218
+ assert . deepEqual ( bst . min ( bst . root ) . key , 2 , 'should return min element' )
219
+ assert . end ( )
220
+ } )
221
+
222
+ test ( 'BST max' , assert => {
223
+ let bst = new BST ( )
224
+ bst . insert ( 12 )
225
+ bst . insert ( 4 )
226
+ bst . insert ( 8 )
227
+ bst . insert ( 15 )
228
+ assert . deepEqual ( bst . max ( bst . root ) . key , 15 , 'should return max element' )
229
+ assert . end ( )
230
+ } )
231
+
232
+ test ( 'BST sucessor' , assert => {
233
+ let bst = new BST ( )
234
+ bst . insert ( 12 )
235
+ bst . insert ( 4 )
236
+ bst . insert ( 8 )
237
+ bst . insert ( 15 )
238
+ assert . deepEqual ( bst . sucessor ( 12 ) . key , 15 , 'should return 15, sucessor for 12' )
239
+ assert . deepEqual ( bst . sucessor ( 15 ) , null , 'should return null if not found' )
240
+ assert . end ( )
241
+ } )
242
+
243
+ test ( 'BST predecessor' , assert => {
244
+ let bst = new BST ( )
245
+ bst . insert ( 12 )
246
+ bst . insert ( 4 )
247
+ bst . insert ( 8 )
248
+ bst . insert ( 15 )
249
+ assert . deepEqual ( bst . predecessor ( 12 ) . key , 8 , 'should return 8, predecessor for 12' )
250
+ assert . deepEqual ( bst . predecessor ( 4 ) , null , 'should return null if not found' )
251
+ assert . end ( )
252
+ } )
253
+
254
+ test ( 'BST length' , assert => {
158
255
let bst = new BST ( )
159
256
bst . insert ( 12 )
160
257
bst . insert ( 4 )
161
258
bst . insert ( 8 )
162
259
bst . insert ( 15 )
163
- assert . deepEqual ( bst . search ( 15 ) . key , 15 )
164
- assert . deepEqual ( bst . min ( bst . root ) . key , 4 )
165
- assert . deepEqual ( bst . max ( bst . root ) . key , 15 )
166
- assert . deepEqual ( bst . toArray ( ) , [ 4 , 8 , 12 , 15 ] )
167
- assert . deepEqual ( bst . length ( ) , 4 )
168
- assert . deepEqual ( bst . root . left . parent . key , 12 )
169
- assert . deepEqual ( bst . sucessor ( 12 ) . key , 15 )
170
- assert . deepEqual ( bst . sucessor ( 8 ) . key , 12 )
171
- assert . deepEqual ( bst . sucessor ( 15 ) , null )
172
- assert . deepEqual ( bst . predecessor ( 12 ) . key , 8 )
173
- assert . deepEqual ( bst . predecessor ( 8 ) . key , 4 )
174
- assert . deepEqual ( bst . predecessor ( 4 ) , null )
175
- bst . delete ( 12 )
176
- assert . deepEqual ( bst . search ( 12 ) , null )
177
- assert . deepEqual ( bst . toArray ( ) , [ 4 , 8 , 15 ] )
178
- assert . deepEqual ( bst . root . left . parent . key , 15 )
179
- assert . deepEqual ( bst . length ( ) , 3 )
260
+ assert . deepEqual ( bst . length ( ) , 4 , 'should return length 4' )
261
+ bst . insert ( 21 )
262
+ bst . insert ( 13 )
263
+ assert . deepEqual ( bst . length ( ) , 6 , 'should return length 6' )
264
+ assert . end ( )
265
+ } )
266
+
267
+ test ( 'BST toArray' , assert => {
268
+ let bst = new BST ( )
269
+ bst . insert ( 4 )
270
+ bst . insert ( 8 )
271
+ bst . insert ( 15 )
272
+ assert . deepEqual ( bst . toArray ( ) , [ 4 , 8 , 15 ] , 'should return array representation of bst' )
273
+ assert . deepEqual ( Array . isArray ( bst . toArray ( ) ) , true , 'should return an array' )
180
274
assert . end ( )
181
275
} )
0 commit comments