-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises.test.js
114 lines (102 loc) · 3.41 KB
/
exercises.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
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
const { expect } = require('@jest/globals');
const { BinaryTree, isUnivalTree, invert, secondMinimumNode } = require('./exercises.js');
describe('Binary Tree', () => {
it('can get its value', () => {
let tree1 = new BinaryTree(4);
expect(tree1.getRootValue()).toBe(4);
let tree2 = new BinaryTree(2);
expect(tree2.getRootValue()).toBe(2);
})
it('can set its value', () => {
let tree = new BinaryTree(4);
expect(tree.getRootValue()).toBe(4);
tree.setRootValue(2);
expect(tree.getRootValue()).toBe(2);
})
it('can insert a left child and return the created node', () => {
let tree = new BinaryTree(4);
let newNode = tree.insertLeft(2);
expect(newNode instanceof BinaryTree).toBe(true);
expect(tree.left.getRootValue()).toBe(2);
})
it('can insert a right child and return the created node', () => {
let tree = new BinaryTree(4);
let newNode = tree.insertRight(7);
expect(newNode instanceof BinaryTree).toBe(true);
expect(tree.right.getRootValue()).toBe(7);
})
it('can get the value of its left child', () => {
let tree = new BinaryTree(4);
tree.insertLeft(2);
expect(tree.getLeftChildValue()).toBe(2);
})
it('can get the value of its right child', () => {
let tree = new BinaryTree(4);
tree.insertRight(7);
expect(tree.getRightChildValue()).toBe(7);
})
})
describe('Univalued Binary Tree', () => {
it("returns true when given a univalued tree", () => {
let tree = new BinaryTree(4);
let left = tree.insertLeft(4);
left.insertLeft(4);
left.insertRight(4);
let right = tree.insertRight(4);
right.insertLeft(4);
expect(isUnivalTree(tree)).toBe(true);
})
it("returns false when given a univalued tree", () => {
let tree = new BinaryTree(4);
let left = tree.insertLeft(4);
left.insertLeft(4);
left.insertRight(5);
let right = tree.insertRight(4);
right.insertLeft(4);
expect(isUnivalTree(tree)).toBe(false);
})
})
describe('Invert', () => {
it("correctly inverts binary tree", () => {
let tree = new BinaryTree(4);
let oldLeft = tree.insertLeft(2);
oldLeft.insertLeft(1);
oldLeft.insertRight(3);
let oldRight = tree.insertRight(7);
oldRight.insertLeft(6);
oldRight.insertRight(9);
invert(tree)
expect(tree.getRootValue()).toBe(4);
expect(tree.getLeftChildValue()).toBe(7);
expect(tree.getRightChildValue()).toBe(2);
expect(oldRight.getLeftChildValue()).toBe(9);
expect(oldRight.getRightChildValue()).toBe(6);
expect(oldLeft.getLeftChildValue()).toBe(3);
expect(oldLeft.getRightChildValue()).toBe(1);
})
})
// remove the .skip in order to run this test!
describe.skip('Second Minimum Node', () => {
it("works for a valid example", () => {
let tree = new BinaryTree(2);
let left = tree.insertLeft(2);
let right = tree.insertRight(5);
right.insertLeft(5);
right.insertRight(7);
expect(secondMinimumNode(tree)).toBe(5);
let tree2 = new BinaryTree(2);
left = tree2.insertLeft(2);
right = tree2.insertRight(2);
left.insertLeft(2);
left.insertRight(3);
right.insertLeft(2);
right.insertRight(4);
expect(secondMinimumNode(tree2)).toBe(3);
})
it("works for an invalid example", () => {
let tree = new BinaryTree(2);
tree.insertLeft(2);
tree.insertRight(2);
expect(secondMinimumNode(tree)).toBe(-1);
})
})