-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree.js
More file actions
34 lines (32 loc) · 819 Bytes
/
tree.js
File metadata and controls
34 lines (32 loc) · 819 Bytes
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
function getRandomInt(low,high) {
return Math.floor(Math.random()*(high-low+1))+low;
}
function createNode(data) {
var node = {
data: data,
children: [],
addChild: function(data) {
var childNode = createNode(data);
return this.addChildNode(childNode);
},
addChildNode: function(childNode) {
this.children.push(childNode);
return childNode;
},
getRandomChild: function() {
if (this.children.length != 0) {
var randomInt = getRandomInt(0, this.children.length-1);
return this.children[randomInt];
} else {
return undefined;
}
},
hasChildren: function() {
return this.children.length > 0;
}
};
return node;
}
module.exports = {
createNode: createNode
};