forked from mgechev/javascript-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsize-balanced-tree.spec.js.skip
More file actions
174 lines (164 loc) · 5.23 KB
/
Copy pathsize-balanced-tree.spec.js.skip
File metadata and controls
174 lines (164 loc) · 5.23 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use strict';
var mod = require('../../src/data-structures/size-balanced-tree.js');
var Node = mod.Node;
var Nil = mod.Nil;
var SBTree = mod.SBTree;
var updateChild = mod.updateChild;
describe('Node', function () {
it('should be a constructor function', function () {
expect(typeof Node).toBe('function');
});
it('should be a construct properly', function () {
var node = new Node(10, Nil, Nil, Nil, 1);
expect(node.value).toBe(10);
expect(node.left).toBe(Nil);
expect(node.right).toBe(Nil);
expect(node.parent).toBe(Nil);
expect(node.size).toBe(1);
});
it('should reference children/parent properly', function () {
var root = new Node(10, Nil, Nil, Nil, 1);
var left = new Node(5, root, Nil, Nil, 1);
var right = new Node(15, root, Nil, Nil, 1);
root.left = left;
root.right = right;
expect(root.value).toBe(10);
expect(root.left).toBe(left);
expect(root.right).toBe(right);
expect(root.parent).toBe(Nil);
expect(right.parent).toBe(root);
expect(left.parent).toBe(root);
expect(right.size).toBe(1);
expect(left.size).toBe(1);
expect(root.size).toBe(1);
root.updateSize();
expect(root.size).toBe(3);
});
});
describe('SBTree', function () {
it('should be a constructor function', function () {
expect(typeof SBTree).toBe('function');
});
it('should start with null root', function () {
expect(new SBTree()._root).toBe(Nil);
});
it('should insert and remove correctly', function () {
var sTree = new SBTree();
expect(sTree.size).toBe(0);
sTree.insert(0, 10);
expect(sTree.size).toBe(1);
sTree.remove(0);
expect(sTree.size).toBe(0);
expect(sTree._root).toBe(Nil);
});
function checkNil() {
expect(Nil.size).toBe(0);
expect(Nil.left).toBe(Nil);
expect(Nil.right).toBe(Nil);
expect(Nil.parent).toBe(Nil);
expect(Nil.value).toBe(null);
}
it('test updateChild', function () {
checkNil();
var root = new Node(10, Nil, Nil, Nil, 1);
var left = new Node(5, root, Nil, Nil, 1);
var right = new Node(15, root, Nil, Nil, 1);
var leftLeft = new Node(10, left, Nil, Nil, 1);
left.left = leftLeft;
left.updateSize();
root.left = left;
root.right = right;
root.updateSize();
expect(root.size).toBe(4);
updateChild(left, leftLeft);
expect(leftLeft.parent).toBe(root);
expect(root.left).toBe(leftLeft);
expect(root.left.size).toBe(1);
checkNil();
});
// Returns a random integer between min (included) and max (excluded)
// Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// Returns a random integer between min (included) and max (included)
// Using Math.round() will give you a non-uniform distribution!
function getRandomIntInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
it('push and get 100000 elements, remove the array by always remove the first/last element', function () {
var sTree = new SBTree();
for (var i = 0; i < 200000; ++i) {
sTree.push(i);
}
checkNil();
for (var i = 0; i < 200000; ++i) {
var node = sTree.get(i);
expect(node.value).toBe(i);
}
for (var i = 0; i < 200000; ++i) {
expect(sTree.get(0).value).toBe(i);
var node = sTree.remove(0); // Always remove the first element;
expect(node.value).toBe(i);
}
checkNil();
expect(sTree._root).toBe(Nil);
var count = 10000;
for (var i = 0; i < count; ++i) {
sTree.insert(0, i);
}
for (var i = 0; i < count; ++i) {
var node = sTree.remove(count - i - 1); // Always remove the last element;
expect(node.value).toBe(i);
expect(sTree.size).toBe(count - i - 1);
}
checkNil();
var expectedArray = [];
for (var i = 0; i < 100000; ++i) {
var newPos = getRandomIntInclusive(0, sTree.size);
sTree.insert(newPos, i);
expectedArray.splice(newPos, 0, i);
}
expect(sTree.size).toBe(expectedArray.length);
for (var i = 0; i < sTree.size; ++i) {
var node = sTree.get(i);
expect(node.value).toBe(expectedArray[i]);
}
for (var i = 0; i < 90000; ++i) {
var removedPos = getRandomInt(0, sTree.size);
sTree.remove(removedPos);
expectedArray.splice(removedPos, 1);
}
for (var i = 0; i < sTree.size; ++i) {
var node = sTree.get(i);
expect(node.value).toBe(expectedArray[i]);
}
checkNil();
});
it('test getIndex', function () {
var sTree = new SBTree();
for (var i = 0; i < 10000; ++i) {
var key = i.toString();
sTree.push(key);
}
for (var i = 0; i < 100; ++i) {
var item = sTree.get(i);
expect(item.value).toBe(i.toString());
expect(sTree.getIndex(item)).toBe(i);
}
});
it('test binary search', function () {
var sTree = new SBTree();
for (var i = 0; i < 10000; ++i) {
sTree.push(i);
}
var cmp = function (a, b) {
return a - b;
}
expect(sTree.binarySearch(cmp, 10.5)).toBe(11)
expect(sTree.binarySearch(cmp, 0)).toBe(1)
expect(sTree.binarySearch(cmp, -1)).toBe(0)
expect(sTree.binarySearch(cmp, 9999)).toBe(10000)
expect(sTree.binarySearch(cmp, 10000)).toBe(10000)
});
});