Toc
  1. Q:
  2. A:
Toc
0 results found
bbcfive
js creates a random tree
2020/06/02 Algorithm tree

Q:

完成函数genData(count),它返回count条树结构的节点数据。形如:
avatar

A:

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
// generate name
function genName(nameLen) {
var nameStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var name = '';
for(var i = 0; i < nameLen; i++) {
// get 0-25
var rand = Math.floor(Math.random()*26);
name+=nameStr[rand];
}
return name;
}

// swap elements in arr
function swapArr(arr, i1, i2) {
arr[i1] = arr.splice(i2, 1, arr[i1])[0];
return arr;
}

function genData(count) {
var objArr = [], obj = {}, rand = 0;
var rootName = genName(5);
var rootObj = {id: 0, name: rootName, parent: null};
objArr.push(rootObj);
for (var c = 1; c < count; c++) {
var parentNum = Math.floor(Math.random()*c);
obj = {id: c, name: genName(5), parent: parentNum}
objArr.push(obj);
}
// iteration function, make objArr listed by parentNum from low to high
for(var m = 0; m < objArr.length - 1; m++) {
for (var n = 0; n < objArr.length - m - 1; n ++) {
if (objArr[n].parent > objArr[n + 1].parent ) {
swapArr(objArr, n, n + 1);
}
}
}
return objArr;
}
本文作者:bbcfive
版权声明:本文首发于bbcfive的博客,转载请注明出处!