-
Notifications
You must be signed in to change notification settings - Fork 0
/
aaa.txt
75 lines (69 loc) · 1.92 KB
/
aaa.txt
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
class TreeSplitter {
constructor(root, options) {
this.root = root;
this.options = options;
this.left = this.hierarchy(root.data, options, true);
this.right = this.hierarchy(root.data, options, true);
this.treeSize = root.children.length;
this.rightTreeSize = Math.round(this.treeSize / 2);
this.splitTrees();
}
hierarchy(data, options, rootOnly) {
options = util.assign({}, DEFAULT_OPTIONS, options);
const root = new Node(data, options);
const nodes = [ root ];
let node;
if (!isolated && !data.collapsed) {
while (node = nodes.shift()) {
if (!node.data.collapsed) {
const children = options.getChildren(node.data);
const length = children ? children.length : 0;
node.children = new Array(length);
if (children && length) {
for (let i = 0; i < length; i++) {
const child = new Node(children[i], options);
node.children[i] = child;
nodes.push(child);
child.parent = node;
child.depth = node.depth + 1;
}
}
}
}
}
return root;
}
getSide(child, index) {
if (this.options.getSide) {
return this.options.getSide(child, index);
}
return index < this.rightTreeSize ? 'right' : 'left';
}
splitTrees() {
for (let i = 0; i < this.treeSize; i++) {
const child = this.root.children[i];
const side = this.getSide(child, i);
if (side === 'right') {
this.right.children.push(child);
} else {
this.left.children.push(child);
}
}
this.assignSide(this.left, 'left');
this.assignSide(this.right, 'right');
}
assignSide(tree, side) {
tree.eachNode(node => {
if (!node.isRoot()) {
node.side = side;
}
});
}
getTrees() {
return {
left: this.left,
right: this.right
};
}
}
module.exports = TreeSplitter;