forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst-insertion.test.js
68 lines (57 loc) · 2.34 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const BinarySearchTree = require('./index');
describe('Binary Search Tree', () => {
let bst;
let rootsLeftChild;
let rootsRightChild;
let rootsLeftChildsLeftChild;
let rootsLeftChildsRightChild;
let rootsRightChildsLeftChild;
let 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);
});
});
describe('Check insertion was as expected', () => {
it('Inorder traversal of the created bst should be [ 10, 20, 30, 100, 400, 500, 600 ]', () => {
expect(bst.traverseInorder()).toEqual([10, 20, 30, 100, 400, 500, 600]);
});
it('Preorder traversal of the created bst should be [ 100, 20, 10, 30, 500, 400, 600 ]', () => {
expect(bst.traversePreorder()).toEqual([100, 20, 10, 30, 500, 400, 600]);
});
it('Postorder traversal of the created bst should be [ 10, 30, 20, 400, 600, 500, 100 ]', () => {
expect(bst.traversePostorder()).toEqual([10, 30, 20, 400, 600, 500, 100]);
});
});
});