Skip to content

Commit af6209c

Browse files
committed
feat: update unit tests for Red-Black Tree, Trie, and Union-Find data structures
1 parent 50e7d6e commit af6209c

File tree

3 files changed

+0
-37
lines changed

3 files changed

+0
-37
lines changed

data-structures/red-black-tree/redBlackTree.test.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,44 @@ describe('RedBlackTree', () => {
66
beforeEach(() => {
77
tree = new RedBlackTree<number>();
88
});
9-
109
test('should insert elements and maintain order', () => {
1110
tree.insert(10);
1211
tree.insert(20);
1312
tree.insert(15);
14-
1513
const result = tree.inOrderTraversal();
1614
expect(result).toEqual([10, 15, 20]);
1715
});
18-
1916
test('should search for existing elements', () => {
2017
tree.insert(10);
2118
tree.insert(20);
2219
tree.insert(15);
23-
2420
expect(tree.search(10)).toBe(true);
2521
expect(tree.search(20)).toBe(true);
2622
expect(tree.search(15)).toBe(true);
2723
});
28-
2924
test('should return false for non-existing elements', () => {
3025
tree.insert(10);
3126
tree.insert(20);
3227

3328
expect(tree.search(5)).toBe(false);
3429
expect(tree.search(15)).toBe(false);
3530
});
36-
3731
test('should handle duplicate inserts gracefully', () => {
3832
tree.insert(10);
3933
tree.insert(10);
4034
tree.insert(10);
41-
4235
const result = tree.inOrderTraversal();
4336
expect(result).toEqual([10]);
4437
});
45-
4638
test('should correctly balance the tree after inserts', () => {
4739
tree.insert(10);
4840
tree.insert(20);
4941
tree.insert(30);
5042
tree.insert(40);
5143
tree.insert(50);
52-
5344
const result = tree.inOrderTraversal();
5445
expect(result).toEqual([10, 20, 30, 40, 50]);
5546
});
56-
5747
test('should handle mixed inserts and maintain order', () => {
5848
tree.insert(50);
5949
tree.insert(30);
@@ -62,11 +52,9 @@ describe('RedBlackTree', () => {
6252
tree.insert(40);
6353
tree.insert(60);
6454
tree.insert(80);
65-
6655
const result = tree.inOrderTraversal();
6756
expect(result).toEqual([20, 30, 40, 50, 60, 70, 80]);
6857
});
69-
7058
test('should support custom comparator', () => {
7159
const stringTree = new RedBlackTree<string>((a, b) => a.localeCompare(b));
7260
stringTree.insert('banana');

data-structures/trie/trie.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,28 @@ describe('Trie', () => {
1212
expect(trie.search('apple')).toBe(true);
1313
expect(trie.search('app')).toBe(false);
1414
});
15-
1615
test('should handle case-insensitive search', () => {
1716
trie.insert('Apple');
1817
expect(trie.search('apple')).toBe(true);
1918
expect(trie.search('APPLE')).toBe(true);
2019
});
21-
2220
test('should check for prefix existence', () => {
2321
trie.insert('apple');
2422
expect(trie.startsWith('app')).toBe(true);
2523
expect(trie.startsWith('appl')).toBe(true);
2624
expect(trie.startsWith('apz')).toBe(false);
2725
});
28-
2926
test('should find all words with a given prefix', () => {
3027
trie.insert('apple');
3128
trie.insert('app');
3229
trie.insert('application');
3330
trie.insert('banana');
34-
3531
expect(trie.findWords('app').sort()).toEqual(
3632
['app', 'apple', 'application'].sort()
3733
);
3834
expect(trie.findWords('ban')).toEqual(['banana']);
3935
expect(trie.findWords('cat')).toEqual([]);
4036
});
41-
4237
test('should throw an error for empty string insertion', () => {
4338
expect(() => trie.insert('')).toThrowError('Cannot insert an empty string');
4439
});

data-structures/union-find/unionFind.test.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,88 +12,68 @@ describe('UnionFind', () => {
1212
expect(uf.getSize(1)).toBe(1);
1313
expect(uf.connected(1, 1)).toBe(true);
1414
});
15-
1615
test('should throw an error when find is called on a non-existent element', () => {
1716
expect(() => uf.find(1)).toThrowError('Element 1 not found in any set');
1817
});
19-
2018
test('should connect two elements using union', () => {
2119
uf.makeSet(1);
2220
uf.makeSet(2);
2321
uf.union(1, 2);
24-
2522
expect(uf.connected(1, 2)).toBe(true);
2623
expect(uf.getSize(1)).toBe(2);
2724
expect(uf.getSize(2)).toBe(2);
2825
});
29-
3026
test('should not connect elements that are already in the same set', () => {
3127
uf.makeSet(1);
3228
uf.makeSet(2);
3329
uf.union(1, 2);
3430
const initialSize = uf.getSize(1);
35-
3631
uf.union(1, 2); // Repeated union
3732
expect(uf.getSize(1)).toBe(initialSize); // Size remains unchanged
3833
});
39-
4034
test('should handle multiple unions correctly', () => {
4135
uf.makeSet(1);
4236
uf.makeSet(2);
4337
uf.makeSet(3);
44-
4538
uf.union(1, 2);
4639
uf.union(2, 3);
47-
4840
expect(uf.connected(1, 3)).toBe(true);
4941
expect(uf.getSize(1)).toBe(3);
5042
});
51-
5243
test('should optimize find with path compression', () => {
5344
uf.makeSet(1);
5445
uf.makeSet(2);
5546
uf.makeSet(3);
56-
5747
uf.union(1, 2);
5848
uf.union(2, 3);
59-
6049
const rootBefore = uf.find(1);
6150
uf.find(3);
6251
const rootAfter = uf.find(1);
63-
6452
expect(rootBefore).toBe(rootAfter);
6553
expect(uf.find(2)).toBe(rootAfter);
6654
});
67-
6855
test('should return false for connected when elements are not in the same set', () => {
6956
uf.makeSet(1);
7057
uf.makeSet(2);
71-
7258
expect(uf.connected(1, 2)).toBe(false);
7359
});
74-
7560
test('should throw an error when getSize is called on a non-existent element', () => {
7661
expect(() => uf.getSize(1)).toThrowError('Element 1 not found in any set');
7762
});
78-
7963
test('should correctly manage set sizes', () => {
8064
uf.makeSet(1);
8165
uf.makeSet(2);
8266
uf.makeSet(3);
83-
8467
uf.union(1, 2);
8568
uf.union(2, 3);
86-
8769
expect(uf.getSize(1)).toBe(3);
8870
expect(uf.getSize(2)).toBe(3);
8971
expect(uf.getSize(3)).toBe(3);
9072
});
91-
9273
test('should work with non-number types', () => {
9374
const ufString = new UnionFind<string>();
9475
ufString.makeSet('a');
9576
ufString.makeSet('b');
96-
9777
ufString.union('a', 'b');
9878
expect(ufString.connected('a', 'b')).toBe(true);
9979
expect(ufString.getSize('a')).toBe(2);

0 commit comments

Comments
 (0)