Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. function BinaryHeap(scoreFunction){
  2. this.content = [];
  3. this.scoreFunction = scoreFunction;
  4. }
  5.  
  6. BinaryHeap.prototype = {
  7. push: function(element) {
  8. // Add the new element to the end of the array.
  9. this.content.push(element);
  10. // Allow it to bubble up.
  11. this.bubbleUp(this.content.length - 1);
  12. },
  13.  
  14. pop: function() {
  15. // Store the first element so we can return it later.
  16. var result = this.content[0];
  17. // Get the element at the end of the array.
  18. var end = this.content.pop();
  19. // If there are any elements left, put the end element at the
  20. // start, and let it sink down.
  21. if (this.content.length > 0) {
  22. this.content[0] = end;
  23. this.sinkDown(0);
  24. }
  25. return result;
  26. },
  27.  
  28. peek: function() {
  29. return this.content[0];
  30. },
  31.  
  32. remove: function(node) {
  33. var length = this.content.length;
  34. // To remove a value, we must search through the array to find
  35. // it.
  36. for (var i = 0; i < length; i++) {
  37. if (this.content[i] != node) continue;
  38. // When it is found, the process seen in 'pop' is repeated
  39. // to fill up the hole.
  40. var end = this.content.pop();
  41. // If the element we popped was the one we needed to remove,
  42. // we're done.
  43. if (i == length - 1) break;
  44. // Otherwise, we replace the removed element with the popped
  45. // one, and allow it to float up or sink down as appropriate.
  46. this.content[i] = end;
  47. this.bubbleUp(i);
  48. this.sinkDown(i);
  49. break;
  50. }
  51. },
  52.  
  53. size: function() {
  54. return this.content.length;
  55. },
  56.  
  57. bubbleUp: function(n) {
  58. // Fetch the element that has to be moved.
  59. var element = this.content[n], score = this.scoreFunction(element);
  60. // When at 0, an element can not go up any further.
  61. while (n > 0) {
  62. // Compute the parent element's index, and fetch it.
  63. var parentN = Math.floor((n + 1) / 2) - 1,
  64. parent = this.content[parentN];
  65. // If the parent has a lesser score, things are in order and we
  66. // are done.
  67. if (score >= this.scoreFunction(parent))
  68. break;
  69.  
  70. // Otherwise, swap the parent with the current element and
  71. // continue.
  72. this.content[parentN] = element;
  73. this.content[n] = parent;
  74. n = parentN;
  75. }
  76. },
  77.  
  78. sinkDown: function(n) {
  79. // Look up the target element and its score.
  80. var length = this.content.length,
  81. element = this.content[n],
  82. elemScore = this.scoreFunction(element);
  83.  
  84. while(true) {
  85. // Compute the indices of the child elements.
  86. var child2N = (n + 1) * 2, child1N = child2N - 1;
  87. // This is used to store the new position of the element,
  88. // if any.
  89. var swap = null;
  90. // If the first child exists (is inside the array)...
  91. if (child1N < length) {
  92. // Look it up and compute its score.
  93. var child1 = this.content[child1N],
  94. child1Score = this.scoreFunction(child1);
  95. // If the score is less than our element's, we need to swap.
  96. if (child1Score < elemScore)
  97. swap = child1N;
  98. }
  99. // Do the same checks for the other child.
  100. if (child2N < length) {
  101. var child2 = this.content[child2N],
  102. child2Score = this.scoreFunction(child2);
  103. if (child2Score < (swap == null ? elemScore : child1Score))
  104. swap = child2N;
  105. }
  106.  
  107. // No need to swap further, we are done.
  108. if (swap == null) break;
  109.  
  110. // Otherwise, swap and continue.
  111. this.content[n] = this.content[swap];
  112. this.content[swap] = element;
  113. n = swap;
  114. }
  115. }
  116. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement