1 function Heap(maxSize, compare)
3 this._maxSize = maxSize;
4 this._compare = compare;
6 this._values = new Array(this._maxSize);
11 // This is a binary heap represented in an array. The root element is stored
12 // in the first element in the array. The root is followed by its two children.
13 // Then its four grandchildren and so on. So every level in the binary heap is
14 // doubled in the following level. Here is an example of the node indices and
15 // how they are related to their parents and children.
16 // ===========================================================================
18 // PARENT -1 0 0 1 1 2 2
19 // LEFT 1 3 5 7 9 11 13
20 // RIGHT 2 4 6 8 10 12 14
21 // ===========================================================================
22 _parentIndex: function(i)
24 return i > 0 ? Math.floor((i - 1) / 2) : -1;
27 _leftIndex: function(i)
29 var leftIndex = i * 2 + 1;
30 return leftIndex < this._size ? leftIndex : -1;
33 _rightIndex: function(i)
35 var rightIndex = i * 2 + 2;
36 return rightIndex < this._size ? rightIndex : -1;
39 // Return the child index that may violate the heap property at index i.
40 _childIndex: function(i)
42 var left = this._leftIndex(i);
43 var right = this._rightIndex(i);
45 if (left != -1 && right != -1)
46 return this._compare(this._values[left], this._values[right]) > 0 ? left : right;
48 return left != -1 ? left : right;
58 return this._size ? this._values[0] : NaN;
63 if (this._size == this._maxSize) {
64 // If size is bounded and the new value can be a parent of the top()
65 // if the size were unbounded, just ignore the new value.
66 if (this._compare(value, this.top()) > 0)
70 this._values[this._size++] = value;
71 this._bubble(this._size - 1);
79 this._values[0] = this._values[--this._size];
85 // Fix the heap property at index i given that parent is the only node that
86 // may violate the heap property.
87 for (var pi = this._parentIndex(i); pi != -1; i = pi, pi = this._parentIndex(pi)) {
88 if (this._compare(this._values[pi], this._values[i]) > 0)
91 this._values.swap(pi, i);
97 // Fix the heap property at index i given that each of the left and the right
98 // sub-trees satisfies the heap property.
99 for (var ci = this._childIndex(i); ci != -1; i = ci, ci = this._childIndex(ci)) {
100 if (this._compare(this._values[i], this._values[ci]) > 0)
103 this._values.swap(ci, i);
109 var out = "Heap[" + this._size + "] = [";
110 for (var i = 0; i < this._size; ++i) {
111 out += this._values[i];
112 if (i < this._size - 1)
118 values: function(size) {
119 // Return the last "size" heap elements values.
120 var values = this._values.slice(0, this._size);
121 return values.sort(this._compare).slice(0, Math.min(size, this._size));
126 createMinHeap: function(maxSize)
128 return new Heap(maxSize, function(a, b) { return b - a; });
131 createMaxHeap: function(maxSize) {
132 return new Heap(maxSize, function(a, b) { return a - b; });