-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScapegoatTree.js
335 lines (276 loc) · 8.17 KB
/
ScapegoatTree.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
'use strict'
const BinaryTree = require("./BinaryTree");
const Queue = require("./LinkedQueue");
function ScapegoatTreeNode(value) {
this.val = value;
this.exist = true;
this.nodeNum = 1;
this.deletedNodeNum = 0;
this.right = this.left = null;
}
module.exports = class ScapegoatTree extends BinaryTree {
constructor(alpha = 0.667) {
super();
this._alpha = alpha;
}
static Node(value) {
return new ScapegoatTreeNode(value);
}
add(value) {
let valueRoot = this._root;
let valueParent = null;
let nodeToInsert = ScapegoatTree.Node(value);
let alpha = this._alpha;
if (!valueRoot) {
this._root = nodeToInsert;
return this;
}
// if the entire tree is unbalanced
let rootNeedReconstruct = _insert(valueRoot, valueParent, nodeToInsert, alpha);
if (rootNeedReconstruct) {
let res = _reconstruct(valueRoot);
this._root = res;
}
return this;
}
delete(value) {
let cNode = this._root;
let valueRoot = this._root;
// find the boolean node with value node
if (this.has(value)) {
while (cNode) {
if (value === cNode.val) {
cNode.exist = false;
++cNode.deletedNodeNum;
break;
}
else if (value < cNode.val) {
++cNode.deletedNodeNum;
cNode = cNode.left;
}
else if (value > cNode.val) {
++cNode.deletedNodeNum;
cNode = cNode.right;
}
}
if (valueRoot.deletedNodeNum / valueRoot.nodeNum >= 0.5) {
let res = _reconstruct(valueRoot);
this._root = res;
}
return true;
}
else {
return false;
}
}
has(value) {
let cNode = this._root;
while (cNode) {
if (value === cNode.val) {
break;
}
else if (value < cNode.val) {
cNode = cNode.left;
}
else if (value > cNode.val) {
cNode = cNode.right;
}
}
// need to ignore the node that turned into false (deleted)
if (cNode && cNode.exist) {
return true;
}
else {
return false;
}
}
*inOrder() {
yield* _inOrder(this._root);
}
*preOrder() {
yield* _preOrder(this._root);
}
*postOrder() {
yield* _postOrder(this._root);
}
*levelOrder() {
if(!this._root){
return;
}
let queue = new Queue(), node = null;
queue.enqueue(this._root);
while (queue.length) {
node = queue.dequeue();
if (node.left)
queue.enqueue(node.left);
if (node.right)
queue.enqueue(node.right);
if (node.exist) {
yield node.val;
}
}
}
depth(val) {
if (undefined === val || null === val) {
return 0;
}
let queue = new Queue(), e = null;
queue.enqueue({ depth: 0, node: this._root });
while (queue.length) {
e = queue.dequeue();
if (val === e.node.val) {
if (e.node.exist) {
return e.depth;
}
else {
return 0;
}
}
if (e.node.left)
queue.enqueue({ node: e.node.left, depth: e.depth + 1 });
if (e.node.right)
queue.enqueue({ node: e.node.right, depth: e.depth + 1 });
}
}
toArray() {
return [..._inOrder(this._root)];
}
clear() {
this._root = null;
}
get root() {
return this._root;
}
set root(value) {
this._root = value;
}
}
function _insert(valueRoot, valueParent, nodeToInsert, alpha) {
// find the position to insert
if (!valueRoot) {
if (nodeToInsert.val < valueParent.val) {
valueParent.left = nodeToInsert;
}
else {
valueParent.right = nodeToInsert;
}
return false;
}
let childNeedReconstruct;
// to find the subtree to insert, if find the same value, turn the boolean node into true (in case that it has been deleted)
if (nodeToInsert.val < valueRoot.val) {
++valueRoot.nodeNum;
childNeedReconstruct = _insert(valueRoot.left, valueRoot, nodeToInsert, alpha);
}
else if (nodeToInsert.val > valueRoot.val) {
++valueRoot.nodeNum;
childNeedReconstruct = _insert(valueRoot.right, valueRoot, nodeToInsert, alpha);
}
else {
valueRoot.exist = true;
return false;
}
// calculate the root tree node num and two children tree node num
let thisNum;
let leftNum;
let rightNum;
if (valueRoot) {
thisNum = valueRoot.nodeNum;
}
else {
thisNum = 0;
}
if (valueRoot.left) {
leftNum = valueRoot.left.nodeNum;
}
else {
leftNum = 0;
}
if (valueRoot.right) {
rightNum = valueRoot.right.nodeNum;
}
else {
rightNum = 0;
}
// if children tree node num bigger than alpha*root tree node num, need to be reconstructed
let thisNeedReconstruct = false;
if (leftNum > alpha * thisNum || rightNum > alpha * thisNum) {
thisNeedReconstruct = true;
}
// if child tree need and itself dont need, start reconstruct
if (!thisNeedReconstruct && childNeedReconstruct) {
let leftRes = _reconstruct(valueRoot.left);
let rightRes = _reconstruct(valueRoot.right);
valueRoot.left = leftRes;
valueRoot.right = rightRes;
return false;
}
// if neither need, still not need
else if (!thisNeedReconstruct && !childNeedReconstruct) {
return false;
}
// if itself need, stay need
else if (thisNeedReconstruct) {
return true;
}
}
function _constructFromArray(valueArr) {
let valueRoot;
// return the empty node
if (valueArr.length === 0) {
valueRoot = null;
}
// return the node without children
else if (valueArr.length === 1) {
valueRoot = new ScapegoatTreeNode(valueArr[0]);
}
else {
// find the middle point to keep balance
let middlePoint = Math.floor(valueArr.length / 2);
// get the middle value
let currentRootVal = valueArr[middlePoint];
// construct the root node
valueRoot = new ScapegoatTreeNode(currentRootVal);
valueRoot.nodeNum = valueArr.length;
// slice the array into 2 pieces according to midddle point
let leftValArr = valueArr.slice(0, middlePoint);
let rightValArr = valueArr.slice(middlePoint + 1);
// get the left and right children node
valueRoot.left = _constructFromArray(leftValArr);
valueRoot.right = _constructFromArray(rightValArr);
}
return valueRoot;
}
function _reconstruct(valueRoot) {
// flatten the tree
let valueArr = [..._inOrder(valueRoot)];
let res = _constructFromArray(valueArr);
return res;
}
function* _inOrder(node) {
if (null === node)
return;
yield* _inOrder(node.left);
if (node.exist) {
yield node.val;
}
yield* _inOrder(node.right);
}
function* _preOrder(node) {
if (null === node)
return;
if (node.exist) {
yield node.val;
}
yield* _preOrder(node.left);
yield* _preOrder(node.right);
}
function* _postOrder(node) {
if (null === node)
return;
yield* _postOrder(node.left);
yield* _postOrder(node.right);
if (node.exist) {
yield node.val;
}
}