-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.js
527 lines (473 loc) · 12.2 KB
/
Tree.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import { freeze } from './.internal/freeze.js'
import { makeInstanceOf } from './makeInstanceOf.js'
/**
* Construct a general tree.
*
* @arg {Object} config - Configuration object.
* @arg {number, string} config.key - A unique identifier.
* @arg {number, string} config.parent - The unique identifier of this tree's parent.
* @arg {Tree[]} config.children - Zero or more trees.
* @arg {*} config.* Zero or more custom properties.
*
* @return {Tree} A new instance of Tree.
*/
function Tree (config) {
if (!(this instanceof Tree)) {
return makeInstanceOf(Tree, arguments)
}
config = castObject(config)
var key = 'key' in config ? castId(config.key) : null
var parent = 'parent' in config ? castId(config.parent) : null
var children = 'children' in config ? List(config.children, key) : List()
var invalidKeys = ['key', 'parent', 'children', 'size']
this.key = key
this.parent = parent
this.children = children.value
this.size = key !== null || parent !== null || children.length > 0
? 1 + children.size
: 0
Object.keys(config).forEach(function (key) {
var value = config[key]
if (invalidKeys.indexOf(key) > -1 || typeof value === 'undefined') {
return
}
this[key] = value
}, this)
freeze(this, Tree)
}
/**
* Add a subtree.
*
* @arg {Tree} descendant - The subtree to add.
* @return {Tree} A new instance of Tree containing the descendant.
*
* @since 1.0.0
*/
Tree.prototype.add = function (descendant) {
if (!(descendant instanceof Tree) || descendant.isEmpty()) {
return this
}
var children
var me = this.delete(descendant.key)
var parent = this.get(descendant.parent)
if (parent === null) {
parent = this
}
while (parent) {
children = List(parent.children).toMap()
children[descendant.key] = descendant
children = Object.keys(children).reduce(function (output, key) {
output.push(children[key])
return output
}, [])
descendant = cloneTree(parent, { children: children })
parent = me.get(descendant.parent)
}
return descendant
}
/**
* Remove a subtree from this tree.
*
* @arg {number, string} key - The key of the tree to be deleted.
* @return {Tree}
*
* @since 1.0.0
*/
Tree.prototype.delete = function (key) {
var children
var newParent
var tree
var parent
// Return an empty tree when root is requested.
if (key === this.key) {
return new this.constructor()
}
tree = this.get(key)
// The requested tree cannot be found. Return this.
if (tree === null) {
return this
}
parent = this.get(tree.parent)
while (parent) {
children = parent.children.reduce(function (c, child) {
if (tree.key !== key && tree.key === child.key) {
c.push(tree) // Push the modified child.
} else if (child.key !== key) {
c.push(child) // Push a reference to the original child.
}
return c
}, [])
tree = cloneTree(parent, { children: children })
newParent = this.get(tree.parent)
if (newParent === null || newParent.key === parent.key) {
break
} else {
parent = newParent
}
}
return tree
}
/**
* Execute a given function once for each tree.
*
* @param {function} func - The function to execute.
* @param {object} thisArg - Value to use as `this` when executing callback.
* @return {undefined}
*
* @since 1.0.0
*/
Tree.prototype.forEach = function (func, thisArg) {
var stop = func.call(thisArg, this, this.key)
if (!stop) {
for (var i = 0; i < this.children.length; i++) {
this.children[i].forEach(func, thisArg)
}
}
}
/**
* Get reference to a subtree by key.
*
* @arg {number, string} key - The `key` value of the tree to return.
* @return {Tree|null} The requested tree, if it exists; null otherwise.
*
* @since 1.0.0
*/
Tree.prototype.get = function (key) {
var output = null
this.forEach(function (tree) {
if (tree.key === key) {
output = tree
return true // break
}
return false // continue
})
return output
}
/**
* Does this tree have a tree with a given key?
*
* @arg {number, string} key - The `key` of the tree to find.
* @return {boolean} `true` if the tree exists; `false` otherwise.
*
* @since 1.1.0
*/
Tree.prototype.has = function (key) {
var output = false
this.forEach(function (tree) {
if (tree.key === key) {
output = true
return true // break
}
return false // continue
})
return output
}
/**
* Is this tree empty?
*
* @return {boolean} `true` if empty; `false` otherwise.
*/
Tree.prototype.isEmpty = function () {
return this.size === 0 && Object.keys(this).length === 4
}
/**
* Recusively sort all children.
*
* @arg {object} options Optional sort configuration.
* @arg {comparator} [options.comparator] Custom sort function.
* @arg {boolean} [options.deep = true] Should all trees be sorted?
*
* @return {Tree}
*/
Tree.prototype.sort = function (options) {
options = castObject(options)
options.deep = !('deep' in options && !options.deep)
var children = List(this.children)
var sorted = children.sort(options.comparator)
if (options.deep) {
sorted = sorted.reduce(function (output, tree) {
output.push(tree.sort(options))
return output
}, [])
}
return children.equals(sorted) ? this : cloneTree(this, { children: sorted })
}
/**
* Create a new tree from an array of trees.
*
* @arg {Tree[]} [trees = []] - A list of Tree instances.
* @arg {object} [options = {}] - Optional named parameters.
* @arg {number} [options.height = 0] - Tree cannot grow beyond this value.
* @arg {comparator} [options.comparator] Optional sort function.
*
* @return {Tree} A composite tree containing all trees represented in the
* `trees` parameter.
*
* @since 1.0.0
*/
Tree.fromArray = function (trees, options) {
trees = Array.isArray(trees) ? trees : []
options = castObject(options)
options.height = 'height' in options ? options.height : 0
options.comparator = 'comparator' in options ? options.comparator : undefined
var assembled = List(trees).assemble(options.height, options.comparator)
if (assembled.length === 1) {
return assembled.value[0]
} else {
return Tree({ children: assembled })
}
}
Object.freeze(Tree)
/**
* Construct a list of General Trees.
*
* @arg {Tree[]} trees
* @arg {Null|Number|String} parent
*
* @return {List}
* @prop {Number} length An integer representing the total number of valid
* trees passed in the `list` parameter and available in the `value` property.
* The value does not include subtrees.
* @prop {Number} size Total number of valid trees in the list including
* subtrees. This value will always be greater than or equal to the value
* of length.
* @prop {Tree[]} value A list of trees.
*
* @private
* @since 1.0.0
*/
function List (trees, parent) {
if (!(this instanceof List)) {
return new List(trees, parent)
}
if (trees instanceof List) {
trees = trees.value
} else if (!Array.isArray(trees)) {
trees = []
}
var size = 0
this.value = trees.reduce(function (v, tree) {
if (tree instanceof Tree) {
tree = typeof parent !== 'undefined' && parent !== tree.parent
? cloneTree(tree, { parent: parent })
: tree
v.push(tree)
size = size + tree.size
}
return v
}, [])
this.size = size
this.length = this.value.length
}
/**
* Assemble.
*
* @arg {Number} height Optional
* @arg {Function} sort Optional.
* @return {List}
*/
List.prototype.assemble = function (height, sort) {
var assembled = []
// Sort in descending order by parent key.
var sorted = this.flatten().sort(function (a, b) {
if (a.parent === null || b.parent === null) {
return -1
}
return b.parent - a.parent
})
var map = List(sorted).toMap()
Object.keys(sorted).forEach(function (i) {
var key = sorted[i].key
var tree = map[key]
var ancestry = determineAncestry(tree, map, height)
if (typeof map[ancestry[0]] !== 'undefined') {
map[ancestry[0]] = map[ancestry[0]].add(tree)
delete (map[tree.key])
}
})
Object.keys(map).forEach(function (key) {
assembled.push(map[key])
})
return new List(assembled)
}
List.prototype.flatten = function () {
var trees = []
this.value.forEach(function (tree) {
if (tree.size === 1) {
trees.push(tree)
} else {
tree.forEach(function (subtree) {
if (subtree.size === 1) {
trees.push(subtree)
} else {
trees.push(cloneTree(subtree, { children: [] }))
}
})
}
})
return trees
}
List.prototype.forEach = function (func, thisArg) {
return this.value.forEach(func, thisArg)
}
List.prototype.get = function (index) {
return this.value[index]
}
List.prototype.sort = function (sort) {
if (typeof sort !== 'function') {
sort = function (a, b) {
if (a.key < b.key) {
return -1
}
if (a.key > b.key) {
return 1
}
return 0
}
}
var sorted
if (this.length < 2) {
return this
} else {
sorted = List(this.value.concat().sort(sort))
return this.equals(sorted) ? this : sorted
}
}
List.prototype.reduce = function (func, initial) {
return List(this.value.reduce(func, initial))
}
/**
* Does this list equal a given list?
*
* @arg {Mixed} aught Any value that might be equal to this.
* @arg {Object} options Optional.
* @prop {String} mode When 'strict' both lists must have trees in the same
* order. When 'lenient', order is not considered. Default: 'strict'.
*
* @return {Boolean} True if aught equals `this`, false otherwise.
*/
List.prototype.equals = function (aught, options) {
options = castObject(options)
if (
!(aught instanceof List) ||
this.length !== aught.length ||
this.size !== aught.size
) {
return false
}
var i
switch (options.mode) {
case 'lenient' :
for (i = 0; i < this.value.length; i++) {
if (aught.value.indexOf(this.value[i]) === -1) {
return false
}
}
break
case 'strict' :
default :
for (i = 0; i < this.value.length; i++) {
if (this.value[i] !== aught.value[i]) {
return false
}
}
break
}
return true
}
/**
* Convert value property to an object whose keys represent the keys of
* trees and whose values are instances of GeneralTree.
*
* @return {Object} map
*/
List.prototype.toMap = function () {
return this.value.reduce(function (map, tree) {
if (typeof map[tree.key] !== 'undefined') {
if (tree.isEmpty()) {
return map
}
throw new TypeError('Duplicate tree.')
}
map[tree.key] = tree
return map
}, Object.create(null))
}
/**
* Return a a value appropriate for use as the
* value of the `key` or `parent` property of a Tree instance.
*
* @arg {*} key Any value. If value is an identifier it will be returned. All
* other values, including NaN, will be cast to null.
*
* @return {identifier}
*
* @private
* @since 1.0.0
*/
function castId (key) {
if (
typeof key === 'string' ||
(
typeof key === 'number' &&
key === Number(key) &&
key !== Infinity &&
key !== -Infinity
)
) {
return key
} else {
return null
}
}
function castObject (o) {
return typeof o === 'object' && o !== null ? o : Object.create(null)
}
/**
* Create a derivitive tree.
*
* @arg {Tree} tree
* @arg {config} [override]
* @return {Tree} A clone of the current instance with potentially .
*
* @private
* @since 1.0.0
*/
function cloneTree (tree, override) {
override = castObject(override)
var props = {
children: tree.children,
key: tree.key,
parent: tree.parent
}
Object.keys(tree).forEach(function (key) {
props[key] = tree[key]
}, tree)
Object.keys(override).forEach(function (key) {
props[key] = override[key]
})
return Tree(props)
}
function determineAncestry (tree, map, height) {
tree = castObject(tree)
var ancestry = []
var parent = tree.parent
var depth = height - 1
if (depth < 1) {
return [parent]
}
while (map[parent]) {
ancestry.push(parent)
parent = map[parent].parent
}
if (ancestry.length > depth) {
return ancestry.reduce(function (output, ancestor, i) {
if (i >= ancestry.length - depth) {
output.push(ancestor)
}
return output
}, [])
}
return ancestry
}
export { Tree }