forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.test.js
66 lines (53 loc) · 1.57 KB
/
BinarySearchTree.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Tree } from '../BinarySearchTree.js'
describe('Binary Search Tree', () => {
let tree
beforeEach(() => {
tree = new Tree()
tree.addValue(10)
tree.addValue(5)
tree.addValue(15)
tree.addValue(3)
tree.addValue(8)
})
test('should add values to the tree', () => {
tree.addValue(12)
expect(tree.search(12)).toBe(12)
expect(tree.search(5)).toBe(5)
expect(tree.search(15)).toBe(15)
})
test('should perform in-order traversal', () => {
const values = []
const output = (val) => values.push(val)
tree.traverse(output)
expect(values).toEqual([3, 5, 8, 10, 15])
})
test('should remove leaf nodes correctly', () => {
tree.removeValue(5)
expect(tree.search(5)).toBeNull()
})
test('should remove nodes with one child correctly', () => {
tree.addValue(12)
tree.removeValue(15)
expect(tree.search(15)).toBeNull()
expect(tree.search(12)).toBe(12)
})
test('should remove nodes with two children correctly', () => {
tree.addValue(18)
tree.removeValue(15)
expect(tree.search(15)).toBeNull()
expect(tree.search(18)).toBe(18)
})
test('should return null for non-existent values', () => {
expect(tree.search(20)).toBeNull()
expect(tree.search(0)).toBeNull()
})
test('should handle removal of root node correctly', () => {
tree.removeValue(10)
expect(tree.search(10)).toBeNull()
})
test('should handle empty tree gracefully', () => {
const newTree = new Tree()
newTree.removeValue(22) // Should not throw
expect(newTree.search(22)).toBeNull()
})
})