Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. const a = [0,[[2,[3,3]],[[3,[4,4]],[3,[4,[[6,6],5]]]]]]
  2.  
  3.  
  4. const findBranchSize = R.pipe(R.flatten, R.length)
  5.  
  6. const compareBranches = R.useWith(R.gte, [findBranchSize, findBranchSize])
  7.  
  8. const findDeepestNode = (tree) => {
  9. let i = 0;
  10. let path = [];
  11. let currentBranch = tree
  12. while((R.length(currentBranch[0]) || R.length(currentBranch[1]))) {
  13. let deepestIndex = compareBranches(currentBranch[0], currentBranch[1]) ? 0 : 1;
  14. path.push(deepestIndex)
  15. currentBranch = currentBranch[deepestIndex]
  16. i++
  17. }
  18. return path
  19. }
  20.  
  21.  
  22. const data = [
  23. { id: 1, size: 20 },
  24. [
  25. [
  26. { id: 2, size: 19 },
  27. [
  28. [
  29. { id: 3, size: 16 },
  30. [
  31. [
  32. { id: 4, size: 5 },
  33. [
  34. [
  35. { id: 5, size: 3 },
  36. [
  37. { id: 6, size: 3 },
  38. [
  39. { id: 7, size: 18 },
  40. { id: 8, size: 17 }
  41. ]
  42. ]
  43. ],
  44. { id: 9, size: 12 }
  45. ]
  46. ],
  47. [
  48. { id: 13, size: 3 },
  49. [
  50. [
  51. { id: 17, size: 3 },
  52. [
  53. { id: 18, size: 3 },
  54. [
  55. { id: 19, size: 18 },
  56. { id: 20, size: 17 }
  57. ]
  58. ]
  59. ],
  60. [
  61. { id: 15, size: 18 },
  62. { id: 16, size: 17 }
  63. ]
  64. ]
  65. ],
  66. ]
  67. ],
  68. { id: 11, size: 13 }
  69. ]
  70. ],
  71. { id: 12, size: 7 }
  72. ]
  73. ];
  74.  
  75.  
  76.  
  77. const checkCluster = R.pipe(R.flatten, R.map(R.prop('size')), R.sum, R.gte(40))
  78.  
  79. const splitTreeBranches = tree => {
  80. const clusters = []
  81. let currentTree = tree;
  82. let i = 0;
  83. while(currentTree && (i < 5)) {
  84. if (checkCluster([currentTree])) {
  85. clusters.push(currentTree)
  86. currentTree = null;
  87. } else {
  88. console.log(currentTree)
  89. let path = findDeepestNode(currentTree)
  90.  
  91. let currentBranch = R.path(path)(currentTree)
  92. let j = 0;
  93. let lastPath, lastBranch;
  94. while(checkCluster([currentBranch]) && j < 10) {
  95. lastPath = path
  96. lastBranch = currentBranch
  97. path = R.init(path);
  98. currentBranch = R.path(path)(currentTree)
  99. }
  100. clusters.push(flatten([lastBranch]))
  101. currentTree = R.dissocPath(lastPath)(currentTree)
  102. }
  103. i++;
  104. }
  105. return clusters
  106. }
  107.  
  108. splitTreeBranches(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement