-
Notifications
You must be signed in to change notification settings - Fork 1
/
TreeControl.js
executable file
·289 lines (269 loc) · 8.06 KB
/
TreeControl.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
/**
* 一个通用的树结构操作器,对源数据的结构没有要求
* @author yinhunfeixue
* @email [email protected]
*/
class TreeControl {
/**
* 创建树控制器实例
* @param {String|Function} dataGetter 获取结点值的方法,为字符串或(node)=>object的函数
* @param {String|Function} childrenGetter 获取结点子结点列表的方法,为字符串或(node)=>object的函数
*/
constructor(dataGetter = "value", childrenGetter = "children", childrenCreater = 'children') {
if (!dataGetter) {
throw new Error('dataGetter need value');
}
else if (!childrenGetter) {
throw new Error('childrenGetter need value');
}
else {
this.dataGetter = dataGetter;
this.childrenGetter = childrenGetter;
this.childrenCreater = childrenCreater;
}
}
/**
* 搜索满足指定条件的第一个结点
* @param {Array} tree 树结点的数据
* @param {*} equalFunction 匹配函数,格式为(node, index, parentNode)=>bool
*/
search(tree, equalFunction) {
let chain = this._searchChainInner(tree, equalFunction);
return chain && chain.length ? chain[chain.length - 1] : null;
}
/**
* 搜索满足指定条件的第一个结点的父结点
* @param {Array} tree 树
* @param {*} equalFunction 匹配函数,格式为(node, index, parentNode)=>bool
*/
searchParent(tree, equalFunction) {
let chain = this._searchChainInner(tree, equalFunction);
if (chain && chain.length >= 2) {
return chain[chain.length - 2];
}
return null;
}
/**
* 获取满足指定条件的第一个结点在父结点子列表中的位置,如果无父结点,或结点不存在,返回-1
* @param {*} tree 树
* @param {*} equalFunction 匹配函数,格式为(node, index, parentNode)=>bool
*
* @return {Number} 指定条件的结点所在的位置
*/
getIndex(tree, equalFunction) {
let parent = this.searchParent(tree, equalFunction);
if (parent) {
let children = this.getChildren(parent);
for (let i = 0; i < children.length; i++) {
if (equalFunction(children[i], i, parent)) {
return i;
}
}
}
return -1;
}
addAt(tree, equalFunction, child, index = -1) {
let node = this.search(tree, equalFunction);
if (node) {
let children = this.getChildren(node);
if (!children) {
children = this._createEmptyChildren(node);
}
if (children) {
let realIndex = Math.max(0, Math.min(children.length, index));
children[realIndex] = child;
}
}
}
remove(tree, equalFunction) {
this._removeInner(tree, equalFunction);
}
/**
*
* @param {Array} tree
* @param {*} equalFunction
*/
_removeInner(tree, equalFunction, parent = null) {
if (tree) {
//遍历结点
for (let i = 0; i < tree.length; i++) {
let node = tree[i];
//如果当前结点符合被删除的条件,则删除;不符合,则递归子结点
if (equalFunction(node, i, parent)) {
tree.splice(i, 1);
i--;
}
else {
this._removeInner(this.getChildren(node), equalFunction, node);
}
}
}
}
/**
* 搜索满足条件的第一个结点,并返回从一级结点到指定结点的数组,第一项是一级结点,最后一项是符合条件的结点
* @param {Array} tree 树
* @param {Function} equalFunction 匹配函数,格式为(node, index, parentNode)=>bool
*/
searchChain(tree, equalFunction) {
return this._searchChainInner(tree, equalFunction);
}
/**
* 遍历树结点,并对每个结点执行回调函数
* @param {Array} tree 树
* @param {Function} forEachFunction 回调函数,格式为(node, index, parentNode)=>void
*/
forEach(tree, forEachFunction) {
this._forEachInner(tree, forEachFunction);
}
/**
* 查找所有符合条件的结点,并返回符合条件结点的一维数组
* @param {Array} tree 树
* @param {Function} equalFunction 匹配函数,格式为(node, index, parentNode)=>bool
*/
find(tree, equalFunction) {
let result = [];
this.forEach(
tree,
(node, i, parent) => {
if (equalFunction(node, i, parent)) {
result.push(node);
}
}
);
return result;
}
/**
* 计算树的结点总数
* @param {Array} tree 树
*/
count(tree) {
let result = 0;
this.forEach(tree, () => {
result++;
});
return result;
}
/**
* 遍历树,并创建和原结构一致的新树。新树的结点为原树结点调用函数处理后的值
*
* **注意**,新树不会自动创建子结点,需要在mapFunction中,把返回值和参数中的newChildren进行关联,例如result.children = newChildren
*
* @param {*} tree
* @param {*} mapFunction 格式为(node, index, oldParent, newChildren)=>Object
*/
map(tree, mapFunction) {
return this._mapInner(tree, mapFunction);
}
/**
*
* @param {*} tree
* @param {*} mapFunction
* @param {*} parent
*
* @private
*/
_mapInner(tree, mapFunction, parent = null) {
//循环树结点,并先递归子树,获取用mapFunction创建的新子树
//子树递归完成后,用mapFunction对当前结点创建新结点,并放到新树中
//把子树放到新结点的子列表中
if (tree) {
let result = [];
for (let i = 0; i < tree.length; i++) {
let node = tree[i];
let children = this.map(this.getChildren(node), mapFunction, node);
let newNode = mapFunction(node, i, parent, children);
result.push(newNode);
}
return result;
}
return null;
}
/**
* 内部用于递归搜索结点链的函数
* @param {*} tree 树
* @param {*} equalFunction 匹配函数,格式为(node, index, parentNode)=>bool
* @param {*} parent 父结点
*
* @private
*
* @return {Array} 从根结点当符合条件的结点的数组
*/
_searchChainInner(tree, equalFunction, parent = null) {
if (tree) {
//循环树,如果有结点符合条件,则放到数组中返回
//如果结点不符合条件,但是有子结点,则递归子结点,如果从子结点中找到结点,把当前结点放到子结果中,一起返回
for (let i = 0; i < tree.length; i++) {
let node = tree[i];
if (equalFunction(node, i, parent)) {
return [node];
}
else {
let children = this.getChildren(node);
if (children) {
let childResult = this._searchChainInner(children, equalFunction, node);
if (childResult) {
childResult.unshift(node);
return childResult;
}
}
}
}
}
return null;
}
/**
* 遍历树(内部函数,勿用)
* @param {*} tree
* @param {*} forEachFunction 要对结点进行操作的函数,格式为(node, index, parentNode)=>void
* @param {*} parent
*
* @private
*/
_forEachInner(tree, forEachFunction, parent = null) {
if (tree) {
//遍历结点,对结点执行操作,并递归子结点
for (let i = 0; i < tree.length; i++) {
let node = tree[i];
forEachFunction(node, i, parent);
this._forEachInner(this.getChildren(node), forEachFunction);
}
}
return null;
}
/**
* 获取结点的值
* @param {*} node
*/
getNodeData(node) {
if (this.dataGetter instanceof Function) {
return this.dataGetter(node);
}
else {
return node[this.dataGetter];
}
}
/**
* 获取结点的子结点列表
* @param {*} node
*
* @return {Array}
*/
getChildren(node) {
if (this.childrenGetter instanceof Function) {
return this.childrenGetter(node);
}
else {
return node[this.childrenGetter];
}
}
_createEmptyChildren(node) {
if (this.childrenCreater instanceof Function) {
return this.childrenCreater(node);
}
else {
node[this.childrenCreater] = [];
return node[this.childrenCreater];
}
}
}
module.exports = TreeControl;