From d87b18a56e23cf1d9ac8fbdbdeb78db5dbe1d8bb Mon Sep 17 00:00:00 2001 From: Slava Kim Date: Thu, 20 Feb 2014 22:38:56 -0800 Subject: [PATCH] Fix building heap from array --- packages/binary-heap/max-heap.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/binary-heap/max-heap.js b/packages/binary-heap/max-heap.js index 37fc90c941..349dde8030 100644 --- a/packages/binary-heap/max-heap.js +++ b/packages/binary-heap/max-heap.js @@ -48,7 +48,8 @@ _.extend(MaxHeap.prototype, { self._heapIdx.set(o.id, i); }); - for (var i = parentIdx(data.length); i >= 0; i--) + // start from the first non-leaf - the parent of the last leaf + for (var i = parentIdx(data.length - 1); i >= 0; i--) self._downHeap(i); },