forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst-insertion.test.js
51 lines (43 loc) · 1.7 KB
/
bst-insertion.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
const BinarySearchTree = require('./index');
describe('Binary Search Tree', () => {
let bst;
let rootsLeftChild, rootsRightChild;
let rootsLeftChildsLeftChild, rootsLeftChildsRightChild;
let rootsRightChildsLeftChild, rootsRightChildsRightChild;
describe('Creates a binary search tree', () => {
it('should create a bst with root 100', () => {
bst = new BinarySearchTree(100);
expect(bst.root.value).toEqual(100);
});
it('should add element 20 to the left of root node', () => {
bst.add(20);
rootsLeftChild = bst.root.leftChild;
expect(rootsLeftChild.value).toEqual(20);
});
it('should add element 500 to the right of root node', () => {
bst.add(500);
rootsRightChild = bst.root.rightChild;
expect(rootsRightChild.value).toEqual(500);
});
it('should add element 10 to the left of root"s left child', () => {
bst.add(10);
rootsLeftChildsLeftChild = bst.root.leftChild.leftChild;
expect(rootsLeftChildsLeftChild.value).toEqual(10);
});
it('should add element 30 to the right of root"s left child', () => {
bst.add(30);
rootsLeftChildsRightChild = bst.root.leftChild.rightChild;
expect(rootsLeftChildsRightChild.value).toEqual(30);
});
it('should add element 400 to the left of root"s right child', () => {
bst.add(400);
rootsRightChildsLeftChild = bst.root.rightChild.leftChild;
expect(rootsRightChildsLeftChild.value).toEqual(400);
});
it('should add element 600 to the right of root"s right child', () => {
bst.add(600);
rootsRightChildsRightChild = bst.root.rightChild.rightChild;
expect(rootsRightChildsRightChild.value).toEqual(600);
});
});
});