-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.js
33 lines (29 loc) · 791 Bytes
/
tree.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
let divideBranch = function(number,node){
return (number<node) ? 'left' : 'right';
};
let insertBranches = function(obj,number){
if(obj.node){
let branch = divideBranch(number,obj.node);
if(!obj[branch]){
obj[branch] = {};
}
return insertBranches(obj[branch],number);
}
obj.node = number;
}
let createTree = function(array){
let insertUnitTree = function(obj,num){
insertBranches(obj,num);
return obj;
}
return array.reduce(insertUnitTree,{})
};
let buildTree = function(array,count = 0,tree = {node:undefined}){
if(count > array.length-1){
return tree;
}
insertBranches(tree,array[count]);
return buildTree(array,count+1,tree);
};
console.log(buildTree(process.argv.slice(2)));
// console.log(createTree(process.argv.slice(2)));