@@ -5,85 +5,61 @@ describe('Binary Search Tree', () => {
5
5
6
6
beforeEach(() => {
7
7
tree = new Tree()
8
- })
9
-
10
- test('should add values to the tree', () => {
11
8
tree.addValue(10)
12
9
tree.addValue(5)
13
10
tree.addValue(15)
11
+ tree.addValue(3)
12
+ tree.addValue(8)
13
+ })
14
14
15
- expect(tree.search(10)).toBe(10)
15
+ test('should add values to the tree', () => {
16
+ tree.addValue(12)
17
+
18
+ expect(tree.search(12)).toBe(12)
16
19
expect(tree.search(5)).toBe(5)
17
20
expect(tree.search(15)).toBe(15)
18
21
})
19
22
20
23
test('should perform in-order traversal', () => {
21
24
const values = []
22
25
const output = (val) => values.push(val)
23
-
24
- tree.addValue(10)
25
- tree.addValue(5)
26
- tree.addValue(15)
27
- tree.addValue(3)
28
- tree.addValue(8)
29
-
30
26
tree.traverse(output)
31
27
expect(values).toEqual([3, 5, 8, 10, 15])
32
28
})
33
29
34
30
test('should remove leaf nodes correctly', () => {
35
- tree.addValue(10)
36
- tree.addValue(5)
37
- tree.addValue(15)
38
-
39
31
tree.removeValue(5)
40
32
expect(tree.search(5)).toBeNull()
41
33
})
42
34
43
35
test('should remove nodes with one child correctly', () => {
44
- tree.addValue(10)
45
- tree.addValue(5)
46
- tree.addValue(15)
47
36
tree.addValue(12)
48
-
49
37
tree.removeValue(15)
38
+
50
39
expect(tree.search(15)).toBeNull()
51
40
expect(tree.search(12)).toBe(12)
52
41
})
53
42
54
43
test('should remove nodes with two children correctly', () => {
55
- tree.addValue(10)
56
- tree.addValue(5)
57
- tree.addValue(15)
58
- tree.addValue(12)
59
44
tree.addValue(18)
60
-
61
45
tree.removeValue(15)
46
+
62
47
expect(tree.search(15)).toBeNull()
63
- expect(tree.search(12)).toBe(12)
64
48
expect(tree.search(18)).toBe(18)
65
49
})
66
50
67
51
test('should return null for non-existent values', () => {
68
- tree.addValue(10)
69
- tree.addValue(5)
70
- tree.addValue(15)
71
-
72
52
expect(tree.search(20)).toBeNull()
73
53
expect(tree.search(0)).toBeNull()
74
54
})
75
55
76
56
test('should handle removal of root node correctly', () => {
77
- tree.addValue(10)
78
- tree.addValue(5)
79
- tree.addValue(15)
80
-
81
57
tree.removeValue(10)
82
58
expect(tree.search(10)).toBeNull()
83
59
})
84
60
85
61
test('should handle empty tree gracefully', () => {
86
- expect(tree.search(10 )).toBeNull()
87
- tree.removeValue(10 ) // Should not throw
62
+ expect(tree.search(22 )).toBeNull()
63
+ tree.removeValue(22 ) // Should not throw
88
64
})
89
65
})
0 commit comments