forked from andreaferretti/paths-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree-utils.js
119 lines (106 loc) · 2.98 KB
/
tree-utils.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
import { treeHeight, buildTree, collect, setHeight } from '../dist/node/tree-utils.js'
import { range } from '../dist/node/ops.js'
import expect from 'expect.js'
let substrings = (word) => {
let l = word.length
if (l === 1) { return [] }
else {
return range(0, l).map((i) =>
(i === 0) ?
word.substring(1, l) :
word.substring(0, i) + word.substring(i + 1, l)
)
}
}
describe('the tree height function', () => {
it('should return 1 for a single node tree', () => {
expect(treeHeight({ name: 'root' })).to.be(1)
})
it('should return the expected height', () => {
let tree = {
name: 1,
children: [
{
name: 2,
children: [
{
name: 4,
children: [
{
name: 6,
children: [
{ name: 7 }
]
}
]
},
{ name: 5 }
]
},
{
name: 3,
children: [
{ name: 8 },
{ name: 9 }
]
}
]
}
expect(treeHeight(tree)).to.be(5)
})
})
describe('the build tree function', () => {
it('should return an object with the "children" property', () => {
let data = {
name: 'a',
descendants: ['b', 'c']
}
let tree = buildTree(data, (x) => x.descendants)
expect(tree).to.have.property('children')
})
it('should populate the "level" property', () => {
let data = {
name: 'a',
descendants: ['b', 'c']
}
let tree = buildTree(data, (x) => x.descendants)
expect(tree.level).to.be(0)
expect(tree.children.map((c) => c.level)).to.eql([1, 1])
})
it('should take into account a function to compute the children', () => {
let data = 'hello'
let tree = buildTree(data, substrings)
expect(treeHeight(tree)).to.be(5)
})
})
describe('the collect function', () => {
let tree = {
name: 'a',
children: [
{ name: 'b' },
{ name: 'c', children: [{name: 'd'}, {name: 'e'}]}
]
}
it('should accumulate a function along the edges', () => {
let list = collect(tree, (p, c) => c)
expect(list.map((x) => x.name)).to.eql(['b', 'c', 'd', 'e'])
})
it('take the parent into account', () => {
let list = collect(tree, (p, c) => p)
expect(list.map((x) => x.name)).to.eql(['a', 'a', 'c', 'c'])
})
})
describe('the set height function', () => {
it('should assign distinct consecutive numbers on each level', () => {
let tree = buildTree('hello', substrings)
setHeight(tree)
let pairs = collect(tree, (p, c) => [c.level, c.height])
let heightsAtLevel2 = pairs.filter(([l, h]) => l === 2).map(([l, h]) => h)
expect(heightsAtLevel2).to.eql(range(0, 20))
})
it('should return the maximum heights found on each level', () => {
let tree = buildTree('hello', substrings)
let heights = setHeight(tree)
expect(heights).to.eql([1 - 1, 5 - 1, 20 - 1, 60 - 1, 120 - 1])
})
})