Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/heaps/mutable_binary_heap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ function _make_mutable_binary_heap(ord::Ordering, ty::Type{T}, values) where T
@inbounds nodemap[i] = i
end

for i = 1 : n
_heap_bubble_up!(ord, nodes, nodemap, i)
for i = (n >> 1) : -1 : 1
_heap_bubble_down!(ord, nodes, nodemap, i)
end
return nodes, nodemap
end
Expand Down
20 changes: 19 additions & 1 deletion test/test_mutable_binheap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ end
@test first(h) == 1
@test isequal(list_values(h), vs)
@test isequal(heap_values(h), [1, 2, 3, 4, 7, 9, 10, 14, 8, 16])
@test verify_heap(h)
@test sizehint!(h, 100) === h
end

Expand All @@ -111,7 +112,8 @@ end
@test !isempty(h)
@test first(h) == 16
@test isequal(list_values(h), vs)
@test isequal(heap_values(h), [16, 14, 10, 8, 7, 3, 9, 1, 4, 2])
@test isequal(heap_values(h), [16, 14, 10, 8, 7, 9, 3, 2, 4, 1])
@test verify_heap(h)
@test sizehint!(h, 100) === h
end

Expand All @@ -123,9 +125,25 @@ end
@test first(h) == (2, 1)
@test isequal(list_values(h), vs2)
@test isequal(heap_values(h), [(2, 1), (4, 2), (3, 3), (1, 4), (10, 7), (6, 9), (7, 10), (8, 14), (9, 8), (5, 16)])
@test verify_heap(h)
@test sizehint!(h, 100) === h
end

@testset "linear time heap building (#639)" begin
for len in (0, 1, 2, 15, 100)
arr = rand(1:1000, len)
h = MutableBinaryMinHeap(arr)
@test length(h) == len
@test verify_heap(h)
@test isequal(list_values(h), arr)

h_max = MutableBinaryMaxHeap(arr)
@test length(h_max) == len
@test verify_heap(h_max)
@test isequal(list_values(h_max), arr)
end
end

@testset "hmin / push! / pop!" begin
hmin = MutableBinaryMinHeap{Int}()
@test length(hmin) == 0
Expand Down
Loading