-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinarySearchTree.spec.ts
159 lines (123 loc) · 5.02 KB
/
binarySearchTree.spec.ts
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import BinarySearchTree from '../src/binarySearchTree';
let newBinarySearchTree: BinarySearchTree;
beforeEach(() => {
newBinarySearchTree = new BinarySearchTree();
});
describe('create a new binary search tree', () => {
it('should create the tree with an empty root', () => {
expect(newBinarySearchTree.root).toBeNull();
});
});
describe('insert an item into the tree', () => {
it('should set the item as the root if the tree is empty', () => {
newBinarySearchTree.insert(10);
expect(newBinarySearchTree.root?.value).toEqual(10);
});
it('should returned the updated tree after insertion', () => {
let returnedTree = newBinarySearchTree.insert(10);
expect(returnedTree.root?.value).toEqual(10);
expect(returnedTree).toEqual(newBinarySearchTree);
});
it('should insert lesser values to the left of the parent node', () => {
newBinarySearchTree.insert(10);
newBinarySearchTree.insert(5);
newBinarySearchTree.insert(4);
expect(newBinarySearchTree.root?.left?.value).toEqual(5);
expect(newBinarySearchTree.root?.left?.left?.value).toEqual(4);
});
it('should insert greater values to the right of the parent node', () => {
newBinarySearchTree.insert(10);
newBinarySearchTree.insert(15);
newBinarySearchTree.insert(40);
expect(newBinarySearchTree.root?.right?.value).toEqual(15);
expect(newBinarySearchTree.root?.right?.right?.value).toEqual(40);
});
it('should do nothing if the value already exists', () => {
newBinarySearchTree.insert(10);
newBinarySearchTree.insert(10);
expect(newBinarySearchTree.root?.value).toEqual(10);
expect(newBinarySearchTree.root?.left).toBeNull();
expect(newBinarySearchTree.root?.right).toBeNull();
});
});
describe('check if a value is in the tree', () => {
it('should return false if the tree is empty', () => {
expect(newBinarySearchTree.includes(10)).toEqual(false);
});
it('should return false if the value does not exist in the tree', () => {
newBinarySearchTree.insert(10);
newBinarySearchTree.insert(11);
expect(newBinarySearchTree.includes(12)).toEqual(false);
});
it('should return true if the value exists in the tree', () => {
newBinarySearchTree.insert(10);
newBinarySearchTree.insert(11);
newBinarySearchTree.insert(4);
expect(newBinarySearchTree.includes(11)).toEqual(true);
});
});
describe('get all values in a tree', () => {
beforeEach(() => {
newBinarySearchTree.insert(10);
newBinarySearchTree.insert(6);
newBinarySearchTree.insert(15);
newBinarySearchTree.insert(3);
newBinarySearchTree.insert(8);
newBinarySearchTree.insert(20);
});
afterEach(() => {
newBinarySearchTree = new BinarySearchTree();
});
describe('breath first search', () => {
it('should return an empty array if the tree is empty', () => {
newBinarySearchTree = new BinarySearchTree();
expect(newBinarySearchTree.valuesBFS()).toHaveLength(0);
});
it('should return an array with the number of items in the tree', () => {
expect(newBinarySearchTree.valuesBFS()).toHaveLength(6);
});
it('should return the values in the correct order', () => {
let values = newBinarySearchTree.valuesBFS();
expect(values).toEqual([10, 6, 15, 3, 8, 20]);
});
});
describe('depth first search pre-order', () => {
it('should return an empty array if the tree is empty', () => {
newBinarySearchTree = new BinarySearchTree();
expect(newBinarySearchTree.valuesDFSPreOrder()).toHaveLength(0);
});
it('should return an array with the number of items in the tree', () => {
expect(newBinarySearchTree.valuesDFSPreOrder()).toHaveLength(6);
});
it('should return the values in the correct order', () => {
let values = newBinarySearchTree.valuesDFSPreOrder();
expect(values).toEqual([10, 6, 3, 8, 15, 20]);
});
});
describe('depth first search post-order', () => {
it('should return an empty array if the tree is empty', () => {
newBinarySearchTree = new BinarySearchTree();
expect(newBinarySearchTree.valuesDFSPostOrder()).toHaveLength(0);
});
it('should return an array with the number of items in the tree', () => {
expect(newBinarySearchTree.valuesDFSPostOrder()).toHaveLength(6);
});
it('should return the values in the correct order', () => {
let values = newBinarySearchTree.valuesDFSPostOrder();
expect(values).toEqual([3, 8, 6, 20, 15, 10]);
});
});
describe('depth first search in-order', () => {
it('should return an empty array if the tree is empty', () => {
newBinarySearchTree = new BinarySearchTree();
expect(newBinarySearchTree.valuesDFSInOrder()).toHaveLength(0);
});
it('should return an array with the number of items in the tree', () => {
expect(newBinarySearchTree.valuesDFSInOrder()).toHaveLength(6);
});
it('should return the values in the correct order', () => {
let values = newBinarySearchTree.valuesDFSInOrder();
expect(values).toEqual([3, 6, 8, 10, 15, 20]);
});
});
});