Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c513cd

Browse files
split tests
1 parent 0ce38b6 commit 5c513cd

File tree

1 file changed

+112
-18
lines changed

1 file changed

+112
-18
lines changed

‎data-structures/binary-search-tree.js

Lines changed: 112 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ BST.prototype.insert = function(value) {
3838
BST.prototype.delete = function(value) {
3939
let current = this.search(value)
4040

41+
if (!current) {
42+
return null
43+
}
44+
4145
if (!current.left) {
4246
this.transplant(current, current.right)
4347
} else if (!current.right) {
@@ -54,6 +58,8 @@ BST.prototype.delete = function(value) {
5458
y.left = current.left
5559
y.left.parent = y
5660
}
61+
62+
return current
5763
}
5864

5965
BST.prototype.transplant = function(u, v) {
@@ -154,28 +160,116 @@ BST.prototype.inorderTreeWalk = function(node, callback) {
154160

155161
const test = require('tape')
156162

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 => {
158255
let bst = new BST()
159256
bst.insert(12)
160257
bst.insert(4)
161258
bst.insert(8)
162259
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')
180274
assert.end()
181275
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /