Skip to content

Commit 328cf12

Browse files
committed
feat: add tests for binary search trees
1 parent 85a55da commit 328cf12

File tree

2 files changed

+99
-7
lines changed

2 files changed

+99
-7
lines changed

Data-Structures/Tree/BinarySearchTree.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ const Node = (function Node() {
3636
visit(output = (value) => console.log(value)) {
3737
// Recursively go left
3838
if (this.left !== null) {
39-
this.left.visit()
39+
this.left.visit(output)
4040
}
4141
// Print out value
4242
output(this.value)
4343
// Recursively go right
4444
if (this.right !== null) {
45-
this.right.visit()
45+
this.right.visit(output)
4646
}
4747
}
4848

@@ -116,20 +116,23 @@ const Tree = (function () {
116116
}
117117

118118
// Inorder traversal
119-
traverse() {
119+
traverse(output = (value) => console.log(value)) {
120120
if (!this.root) {
121121
// No nodes are there in the tree till now
122122
return
123123
}
124-
this.root.visit()
124+
this.root.visit(output)
125125
}
126126

127127
// Start by searching the root
128128
search(val) {
129-
const found = this.root.search(val)
130-
if (found !== null) {
131-
return found.value
129+
if (this.root) {
130+
const found = this.root.search(val)
131+
if (found !== null) {
132+
return found.value
133+
}
132134
}
135+
133136
// not found
134137
return null
135138
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Tree } from '../BinarySearchTree.js'
2+
3+
describe('Binary Search Tree', () => {
4+
let tree
5+
6+
beforeEach(() => {
7+
tree = new Tree()
8+
})
9+
10+
test('should add values to the tree', () => {
11+
tree.addValue(10)
12+
tree.addValue(5)
13+
tree.addValue(15)
14+
15+
expect(tree.search(10)).toBe(10)
16+
expect(tree.search(5)).toBe(5)
17+
expect(tree.search(15)).toBe(15)
18+
})
19+
20+
test('should perform in-order traversal', () => {
21+
const values = []
22+
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+
tree.traverse(output)
31+
expect(values).toEqual([3, 5, 8, 10, 15])
32+
})
33+
34+
test('should remove leaf nodes correctly', () => {
35+
tree.addValue(10)
36+
tree.addValue(5)
37+
tree.addValue(15)
38+
39+
tree.removeValue(5)
40+
expect(tree.search(5)).toBeNull()
41+
})
42+
43+
test('should remove nodes with one child correctly', () => {
44+
tree.addValue(10)
45+
tree.addValue(5)
46+
tree.addValue(15)
47+
tree.addValue(12)
48+
49+
tree.removeValue(15)
50+
expect(tree.search(15)).toBeNull()
51+
expect(tree.search(12)).toBe(12)
52+
})
53+
54+
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+
tree.addValue(18)
60+
61+
tree.removeValue(15)
62+
expect(tree.search(15)).toBeNull()
63+
expect(tree.search(12)).toBe(12)
64+
expect(tree.search(18)).toBe(18)
65+
})
66+
67+
test('should return null for non-existent values', () => {
68+
tree.addValue(10)
69+
tree.addValue(5)
70+
tree.addValue(15)
71+
72+
expect(tree.search(20)).toBeNull()
73+
expect(tree.search(0)).toBeNull()
74+
})
75+
76+
test('should handle removal of root node correctly', () => {
77+
tree.addValue(10)
78+
tree.addValue(5)
79+
tree.addValue(15)
80+
81+
tree.removeValue(10)
82+
expect(tree.search(10)).toBeNull()
83+
})
84+
85+
test('should handle empty tree gracefully', () => {
86+
expect(tree.search(10)).toBeNull()
87+
tree.removeValue(10) // Should not throw
88+
})
89+
})

0 commit comments

Comments
 (0)